1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222
<?php
namespace GeniBase\Storager;
use Gedcomx\Gedcomx;
use Gedcomx\Common\ExtensibleData;
use GeniBase\Util;
use GeniBase\DBase\GeniBaseInternalProperties;
use Gedcomx\Conclusion\Name;
use Gedcomx\Conclusion\Person;
use Gedcomx\Conclusion\Gender;
use Gedcomx\Conclusion\Fact;
class PersonStorager extends SubjectStorager
{
protected function getObject($o = null)
{
return new Person($o);
}
public function save($entity, ExtensibleData $context = null, $o = null)
{
if (! $entity instanceof ExtensibleData) {
$entity = $this->getObject($entity);
}
$o = $this->applyDefaultOptions($o, $entity);
$this->makeGbidIfEmpty($entity, $o);
$t_persons = $this->dbs->getTableName('persons');
$t_pgs = $this->dbs->getTableName('genders');
$ent = $entity->toArray();
$data = Util::arraySliceKeys($ent, 'id');
if (isset($ent['private'])) {
$data['private'] = (int) $ent['private'];
}
if (isset($ent['living'])) {
$data['living'] = (int) $ent['living'];
}
$_id = (int) $this->dbs->getLidForId($t_persons, $data['id']);
parent::save($entity, $context, $o);
if (! empty($_id)) {
$result = $this->dbs->getDb()->update(
$t_persons,
$data,
[
'_id' => $_id
]
);
} else {
$this->dbs->getDb()->insert($t_persons, $data);
$_id = (int) $this->dbs->getDb()->lastInsertId();
}
GeniBaseInternalProperties::setPropertyOf($entity, '_id', $_id);
if (isset($ent['gender'])) {
$tmp = $o;
if (! empty($tmp['makeId_name'])) {
$tmp['makeId_name'] = 'Gender: ' . $tmp['makeId_name'];
}
$this->newStorager(Gender::class)->save($ent['gender'], $entity, $tmp);
unset($tmp);
}
if (! empty($ent['names'])) {
$tmp = $o;
$tmp_cnt = 0;
foreach ($ent['names'] as $name) {
if (! empty($o['makeId_name'])) {
$tmp['makeId_name'] = 'Name-' . (++$tmp_cnt) . ': ' . $o['makeId_name'];
}
$this->newStorager(Name::class)->save($name, $entity, $tmp);
}
unset($tmp);
unset($tmp_cnt);
}
if (! empty($ent['facts'])) {
$tmp = $o;
$tmp_cnt = 0;
foreach ($ent['facts'] as $name) {
$tmp['makeId_name'] = 'Fact-' . (++$tmp_cnt) . ': ' . $entity->getId();
$this->newStorager(Fact::class)->save($name, $entity, $o);
}
unset($tmp);
unset($tmp_cnt);
}
return $entity;
}
protected function getSqlQueryParts()
{
$t_persons = $this->dbs->getTableName('persons');
$qparts = [
'fields' => [], 'tables' => [], 'bundles' => [],
];
$qparts['fields'][] = "pn.*";
$qparts['tables'][] = "$t_persons AS pn";
$qparts['bundles'][] = "";
$qp = parent::getSqlQueryParts();
$qp['bundles'][0] = "cn.id = pn.id";
$qparts = array_merge_recursive($qparts, $qp);
return $qparts;
}
protected function loadRaw(ExtensibleData $entity, $context, $o)
{
$q = $this->getSqlQuery();
$result = false;
if (! empty($_id = (int) GeniBaseInternalProperties::getPropertyOf($entity, '_id'))) {
$result = $this->dbs->getDb()->fetchAssoc("$q WHERE pn._id = ?", [$_id]);
} elseif (! empty($id = $entity->getId())) {
$result = $this->dbs->getDb()->fetchAssoc("$q WHERE pn.id = ?", [$id]);
}
return $result;
}
protected function processRaw($entity, $result)
{
if (! is_array($result)) {
return $result;
}
if (! empty($result['private'])) {
settype($result['private'], 'boolean');
}
if (! empty($result['living'])) {
settype($result['living'], 'boolean');
}
$entity = parent::processRaw($entity, $result);
if (! empty($r = $this->newStorager(Gender::class)->load(null, $entity))) {
$entity->setGender($r);
}
if (! empty($r = $this->newStorager(Name::class)->loadList($entity))) {
$entity->setNames($r);
}
if (! empty($r = $this->newStorager(Fact::class)->loadList($entity))) {
$entity->setFacts($r);
}
return $entity;
}
public function loadGedcomx($entity, $context = null, $o = null)
{
$gedcomx = new Gedcomx();
$o = $this->applyDefaultOptions($o);
if (false === $psn = $this->load($entity, $context, $o)) {
return false;
}
$gedcomx->addPerson($psn);
if ($o['loadCompanions']) {
$gedcomx->embed($this->loadGedcomxCompanions($psn));
}
return $gedcomx;
}
public function loadGedcomxCompanions(ExtensibleData $entity)
{
$gedcomx = parent::loadGedcomxCompanions($entity);
if (! empty($r = $entity->getGender())) {
$gedcomx->embed($this->newStorager($r)->loadGedcomxCompanions($r));
}
if (! empty($r = $entity->getNames())) {
foreach ($r as $v) {
$gedcomx->embed($this->newStorager($v)->loadGedcomxCompanions($v));
}
}
if (! empty($r = $entity->getFacts())) {
foreach ($r as $v) {
$gedcomx->embed($this->newStorager($v)->loadGedcomxCompanions($v));
}
}
return $gedcomx;
}
}