1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace kalanis\kw_pedigree\Storage\MultiTable; |
4
|
|
|
|
5
|
|
|
|
6
|
|
|
use kalanis\kw_mapper\MapperException; |
7
|
|
|
use kalanis\kw_mapper\Mappers; |
8
|
|
|
use kalanis\kw_mapper\Records\ARecord; |
9
|
|
|
use kalanis\kw_pedigree\Interfaces\ILike; |
10
|
|
|
use kalanis\kw_pedigree\PedigreeException; |
11
|
|
|
use kalanis\kw_pedigree\Storage\APedigreeRecord; |
12
|
|
|
|
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Class PedigreeItemMapper |
16
|
|
|
* @package kalanis\kw_pedigree\Storage\MultiTable |
17
|
|
|
* Need to be used over MySQL / MariaDB |
18
|
|
|
*/ |
19
|
|
|
class PedigreeItemMapper extends Mappers\Database\ADatabase implements ILike |
20
|
|
|
{ |
21
|
2 |
|
protected function setMap(): void |
22
|
|
|
{ |
23
|
2 |
|
$this->setSource('pedigree'); |
24
|
2 |
|
$this->setTable('kw_pedigree_upd'); |
25
|
2 |
|
$this->setRelation('id', 'kwp_id'); |
26
|
2 |
|
$this->setRelation('short', 'kwp_short'); |
27
|
2 |
|
$this->setRelation('name', 'kwp_name'); |
28
|
2 |
|
$this->setRelation('family', 'kwp_family'); |
29
|
2 |
|
$this->setRelation('birth', 'kwp_birth'); |
30
|
2 |
|
$this->setRelation('death', 'kwp_death'); |
31
|
2 |
|
$this->setRelation('successes', 'kwp_successes'); |
32
|
2 |
|
$this->setRelation('sex', 'kwp_sex'); |
33
|
2 |
|
$this->setRelation('text', 'kwp_text'); |
34
|
2 |
|
$this->addPrimaryKey('id'); |
35
|
2 |
|
$this->addForeignKey('parents', $this->getRelateRecordClass(), 'id', 'childId'); |
36
|
2 |
|
$this->addForeignKey('children', $this->getRelateRecordClass(), 'id', 'parentId'); |
37
|
2 |
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @param ARecord $record |
41
|
|
|
* @throws MapperException |
42
|
|
|
* @return bool |
43
|
|
|
*/ |
44
|
2 |
|
protected function beforeSave(ARecord $record): bool |
45
|
|
|
{ |
46
|
2 |
|
$text = strval($record->__get('text')); |
47
|
2 |
|
if (empty($text)) { |
48
|
2 |
|
$record->__set('text', ''); |
49
|
|
|
} |
50
|
|
|
|
51
|
2 |
|
$short = strval($record->__get('short')); |
52
|
2 |
|
$id = intval($record->__get('id')); |
53
|
2 |
|
if (!empty($id) && empty($short)) { |
54
|
|
|
// probably update |
55
|
1 |
|
return true; |
56
|
|
|
} |
57
|
2 |
|
if (empty($short)) { |
58
|
1 |
|
return false; |
59
|
|
|
} |
60
|
2 |
|
return true; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @param ARecord|PedigreeItemRecord $record |
65
|
|
|
* @throws MapperException |
66
|
|
|
* @return bool |
67
|
|
|
*/ |
68
|
2 |
|
public function beforeDelete(ARecord $record): bool |
69
|
|
|
{ |
70
|
2 |
|
$relateRecordClass = $this->getRelateRecordClass(); |
71
|
2 |
|
$relation = new $relateRecordClass(); |
72
|
|
|
/** @var PedigreeRelateRecord $relation */ |
73
|
2 |
|
$relation->childId = intval(strval($record->__get('id'))); |
74
|
2 |
|
$all = $relation->loadMultiple(); |
75
|
2 |
|
foreach ($all as $item) { |
76
|
1 |
|
$item->delete(); |
77
|
|
|
} |
78
|
2 |
|
$relation = new $relateRecordClass(); |
79
|
|
|
/** @var PedigreeRelateRecord $relation */ |
80
|
2 |
|
$relation->parentId = intval(strval($record->__get('id'))); |
81
|
2 |
|
$all = $relation->loadMultiple(); |
82
|
2 |
|
foreach ($all as $item) { |
83
|
1 |
|
$item->delete(); |
84
|
|
|
} |
85
|
2 |
|
return parent::beforeDelete($record); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @return string |
90
|
|
|
* @codeCoverageIgnore used another one for testing |
91
|
|
|
*/ |
92
|
1 |
|
protected function getRelateRecordClass(): string |
93
|
|
|
{ |
94
|
1 |
|
return PedigreeRelateRecord::class; |
95
|
|
|
} |
96
|
|
|
|
97
|
2 |
|
public function getLike(APedigreeRecord $record, string $what, ?string $sex = null): array |
98
|
|
|
{ |
99
|
2 |
|
$query = 'SELECT `%1$s` AS `id`, `%2$s` AS `short`, `%3$s` AS `name`, `%4$s` AS `family` FROM `%5$s` WHERE (`%3$s` LIKE :named1 OR `%4$s` LIKE :named1)'; |
100
|
2 |
|
$params = [':named1' => sprintf('%%%s%%', $what)]; |
101
|
2 |
|
if (!is_null($sex)) { |
102
|
1 |
|
$query .= ' AND `%6$s` = :sx1'; |
103
|
1 |
|
$params[':sx1'] = $sex; |
104
|
|
|
} |
105
|
2 |
|
$query .= ' ORDER BY `%4$s` ASC, `%3$s` ASC LIMIT 0, 30;'; |
106
|
|
|
|
107
|
2 |
|
$read = $this->getReadDatabase(); |
108
|
2 |
|
if (method_exists($read, 'query')) { |
109
|
2 |
|
$result = $read->query(sprintf($query, |
110
|
2 |
|
$this->relations['id'], |
111
|
2 |
|
$this->relations['short'], |
112
|
2 |
|
$this->relations['name'], |
113
|
2 |
|
$this->relations['family'], |
114
|
2 |
|
$this->getTable(), |
115
|
2 |
|
$this->relations['sex'] |
116
|
|
|
), $params); |
117
|
|
|
|
118
|
2 |
|
$items = []; |
119
|
2 |
|
foreach ($result as $line) { |
120
|
2 |
|
$item = clone $record; |
121
|
2 |
|
$item->loadWithData($line); |
122
|
2 |
|
$items[] = $item; |
123
|
|
|
} |
124
|
2 |
|
return $items; |
125
|
|
|
} else { |
126
|
|
|
// @codeCoverageIgnoreStart |
127
|
|
|
// when you set the DB which does not have a query method (LDAP) |
128
|
|
|
throw new PedigreeException('This mapper does not have query!'); |
129
|
|
|
} |
130
|
|
|
// @codeCoverageIgnoreEnd |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
|