1: <?php
2:
3:
4: 5: 6: 7: 8: 9:
10:
11: namespace app\controllers;
12:
13: use \mako\database\ConnectionManager;
14: use \mako\database\Connection;
15: use \mako\application\Application;
16: use \PDO;
17: use app\models\Reference;
18: use app\models\DeletedLink;
19: use app\models\DeletedPerson;
20:
21: class Person {
22:
23: private $autoid;
24: private $newautoid;
25: private $personid;
26: private $firstname;
27: private $familyname;
28: private $middlename;
29: private $maidenname;
30: private $patronymic;
31: private $gender;
32: private $birthdate;
33: private $birthplace;
34: private $birthplaceMunId;
35: private $deathdate;
36: private $deathplace;
37: private $deathplaceMunId;
38: private $fatherId;
39: private $motherId;
40: private $places;
41: private $remarks;
42: private $references;
43: private $entries;
44: private $user;
45: private $fieldsToCopy = array('firstname', 'familyname', 'middlename', 'maidenname', 'patronymic', 'gender',
46: 'birthdate', 'birthplace', 'birthplaceMunId', 'deathdate', 'deathplace', 'deathplaceMunId');
47:
48:
49:
50: public function setUser($user) {
51: $this->user = $user;
52: }
53:
54: public function getUser() {
55: return $this->user;
56: }
57:
58: protected $dbName = 'ohbrutvikling';
59: protected $personMaps;
60:
61: 62: 63: 64: 65: 66: 67:
68: function __construct() {
69: $this->personMaps = new PersonMappings();
70: }
71:
72: public function get($field) {
73: if (property_exists($this, $field))
74: return $this->{$field};
75: else
76: return '';
77: }
78:
79: public function set($field, $value) {
80: if (property_exists($this, $field))
81: $this->{$field} = $value;
82: }
83:
84: public function addEntry($entry) {
85: if ($this->entries == NULL) {
86: $this->entries = array();
87: }
88: $this->entries[] = $entry;
89: }
90:
91: public function addEntries($entries) {
92: foreach ($entries as $entry) {
93: $this->addEntry($entry);
94: }
95: }
96:
97: public function belongToCommon($pfids) {
98:
99:
100: $id = NULL;
101:
102: foreach ($pfids as $pfid) {
103: $res = $this->getPersonForPFID($pfid);
104: if ($res != NULL) {
105: if ($id == NULL) {
106: $id = $res["id"];
107: } else {
108: if ($id != $res["id"])
109: return false;
110: }
111: }
112: else {
113: return false;
114: }
115: }
116: return true;
117: }
118:
119: public function setFieldsFromAPI($apidataset) {
120:
121: foreach ($apidataset as $key => $value) {
122:
123: if (array_key_exists($key, $this->personMaps->webDbMapping)) {
124:
125:
126:
127: $this->{$this->personMaps->webDbMapping[$key]} = $value;
128:
129: }
130: }
131: }
132:
133:
134: 135: 136: 137: 138:
139: public function getPersonForPFID($pfid) {
140:
141: $res = NULL;
142: $connection = $this->getConnection();
143: $link = $connection->first('SELECT * from `person_links` WHERE `pfid` = ?', array($pfid));
144: if ($link != NULL) {
145:
146: if (property_exists($link, 'person_autoid') && property_exists($link, 'pid'))
147: $res = array("id" => $link->person_autoid, "pid" => $link->pid);
148: }
149: return $res;
150: }
151:
152:
153: public function populatePersonFromCart($cart, $indx)
154: {
155:
156: foreach ($this->fieldsToCopy as $field)
157: {
158:
159: $all = array();
160: foreach ($cart as $indx=>$item)
161: {
162:
163: $val = $item->get($field);
164: if ((!in_array ($val , $all)) && !empty($val))
165: {
166: $all[] = $val;
167: }
168: }
169: }
170: }
171:
172: public function getDBRefs($pfid) {
173: return Reference::where('pfid', '=', $pfid)->all();
174: }
175:
176: private function getRefs($pfid) {
177:
178: $exist = array();
179: $refs = Reference::where('pfid', '=', $pfid)->all();
180: foreach ($refs as $ref) {
181: $exist[] = array("id" => $ref->id, "url" => $ref->url, "title" => $ref->title);
182: }
183: return array("exist" => $exist, "new" => array());
184:
185: }
186:
187:
188: private function getPerson($id) {
189:
190: $result = array();
191: $person = array();
192: $connection = $this->getConnection();
193: $pson = $connection->first('SELECT * from `person` WHERE `' . $id['id'] . '` = ?', array($id['value']));
194: if ($pson == NULL)
195: return array(NULL);
196:
197: foreach ($this->personMaps->webDbMapping as $key => $value) {
198: if ($pson != NULL && property_exists($pson, $value)) {
199: $person[$key] = $pson->{$value};
200: }
201: }
202: foreach ($this->personMaps->dbPersonFields as $key => $value) {
203: if (property_exists($pson, $value)) {
204: $this->{$key} = $pson->{$value};
205: }
206: }
207:
208: $person["references"] = $this->getRefs($person["pfid"]);
209:
210: $pfids = array();
211: $types = array();
212:
213: $res = $connection->all('SELECT * from `person_links` WHERE `' . $id['linksid'] . '` = ?', array($id['value']));
214: if ($res != NULL) {
215: foreach ($res as $r) {
216: $pfids[] = $r->pfid;
217: $types[] = $r->type;
218: $this->addEntry(array('autoid' => $r->autoid, 'pfid' => $r->pfid, 'type' => $r->type, 'exist' => true));
219: }
220: }
221: return array("person" => $person, "pfids" => $pfids, "types" => $types);
222: }
223:
224: public function getPersonByID($id) {
225: $res = $this->getPerson(array('id' => 'autoid', 'linksid' => 'person_autoid', 'value' => $id));
226: return $res;
227: }
228:
229: public function getPersonByPFID($pfid) {
230: $res = $this->getPerson(array('id' => 'personid', 'linksid' => 'pid', 'value' => $pfid));
231: return $res;
232: }
233:
234: private function getSavePersonSql() {
235: $sql = 'INSERT INTO person (';
236: $values = 'VALUES(';
237: $inserts = array();
238: foreach ($this->personMaps->dbPersonFields as $key => $value) {
239:
240: if (!empty($this->{$key})) {
241:
242: $sql .= $value . ', ';
243: $values .= '?,';
244: $inserts[] = $this->{$key};
245: }
246: }
247: $sql = trim($sql);
248: $sql = rtrim($sql, ",");
249: $values = trim($values);
250: $values = rtrim($values, ",");
251: $sql .= ') ' . $values . ')';
252: if (!empty($inserts)) {
253: return array("sql" => $sql, "values" => $inserts);
254: } else
255: return NULL;
256: }
257:
258: public function save() {
259: $last = -1;
260: $dbh = $this->getPDO();
261: $personsql = $this->getSavePersonSql();
262: if (empty($personsql)) {
263: return feil;
264: }
265: $stmt = $dbh->prepare($personsql["sql"]);
266: try {
267: $dbh->beginTransaction();
268: $stmt->execute($personsql["values"]);
269: $last = $dbh->lastInsertId();
270: $this->newautoid = $last;
271: foreach ($this->entries as $entry) {
272: if (!(array_key_exists("exist", $entry) && $entry["exist"] === true)) {
273: $stmt_mergings = $dbh->prepare("INSERT INTO person_links (person_autoid, person_entry_autoid, pfid, pid, type, user_id, score, marker) VALUES(?,?,?,?,?,?,?,?)");
274: $stmt_mergings->execute(array($last, $entry["id"], $entry["pfid"], $this->personid, $entry["type"], $entry["user_id"], $entry["score"], $entry["marker"]));
275: }
276: }
277: $dbh->commit();
278: } catch (PDOExecption $e) {
279: $dbh->rollback();
280: $last = -1;
281: print "Error!: " . $e->getMessage() . "</br>";
282: }
283: return $last;
284: }
285:
286: private function getAlteredFieldModel($field, $value, $id) {
287:
288: }
289:
290: private function getUpdateSql($fields) {
291: $res = "UPDATE person SET ";
292: foreach ($fields as $field) {
293:
294: $res .= $field . " = ?,";
295: }
296: $res = rtrim($res, ",");
297: $res .= " WHERE personid =?";
298: return $res;
299: }
300:
301: public function saveEntries() {
302:
303: $dbh = $this->getPDO();
304: try {
305: $dbh->beginTransaction();
306: foreach ($this->entries as $entry) {
307: if (!(array_key_exists("exist", $entry) && $entry["exist"] === true)) {
308: $stmt_mergings = $dbh->prepare("INSERT INTO person_links (person_autoid, person_entry_autoid, pfid, pid, type, user_id, marker) VALUES(?,?,?,?,?,?,?)");
309: $stmt_mergings->execute(array($this->autoid, $entry["id"], $entry["pfid"], $this->personid, $entry["type"], $entry["user_id"], $entry["marker"]));
310: }
311: }
312: $dbh->commit();
313: } catch (PDOExecption $e) {
314: $dbh->rollback();
315: print "Error!: " . $e->getMessage() . "</br>";
316: }
317: }
318:
319: public function update($fields, $values, $id) {
320:
321: if(count($fields) != count($values))
322: {
323: return "error message to be implemented";
324: }
325:
326: $values[] = $id;
327:
328: $sql = $this->getUpdateSql($fields);
329: try {
330: $dbh = $this->getPDO();
331: $stmt = $dbh->prepare($sql);
332: try {
333: $dbh->beginTransaction();
334:
335: for ($ii = 0; $ii < count($fields); $ii++)
336: {
337:
338: $model = $this->getAlteredFieldModel($fields[$ii], $values[$ii], $id);
339: }
340: $stmt->execute($values);
341: $dbh->commit();
342: } catch (PDOExecption $e) {
343: $dbh->rollback();
344: echo "Error!: " . $e->getMessage() . "<br>";
345: }
346: } catch (PDOExecption $e) {
347: echo "Error!: " . $e->getMessage() . "<br>";
348: }
349: }
350:
351: private function populateDeletePerson($delete, $newid) {
352: foreach ($this->personMaps->dbPersonFields as $key => $value) {
353:
354: if (!empty($this->{$key}) && $value != "autoid") {
355:
356: $delete->{$value} = $this->{$key};
357: }
358: }
359: $delete->userid = $this->user['id'];
360: $delete->old_personid = $this->autoid;
361:
362: $delete->new_personid = $newid;
363: return $delete;
364: }
365:
366: private function populateDeleteLink($delete) {
367: $link = new DeletedLink();
368: $link->person_entry_autoid = $delete['autoid'];
369: $link->pfid = $delete['pfid'];
370: $link->user_id = $this->user['id'];
371: $link->marker = $this->user['marker'];
372: $link->type = $delete['type'];
373: return $link;
374:
375: }
376:
377: public function delete($newid) {
378: $deleteperson = new DeletedPerson();
379: $deletedlinks = array();
380: $dbh = $this->getPDO();
381: $stmt = $dbh->prepare("DELETE FROM person WHERE autoid = ?");
382: try {
383: $dbh->beginTransaction();
384: $deleteperson = $this->populateDeletePerson($deleteperson, $newid);
385: $deleteperson->save();
386: $stmt->execute(array($this->autoid));
387:
388:
389: foreach ($this->entries as $entry) {
390: $deletelink = $this->populateDeleteLink($entry);
391: $deletelink->save();
392: $deletedlinks[] = $deletelink;
393: $stmt_mergings = $dbh->prepare("DELETE FROM person_links WHERE autoid = ?");
394: $stmt_mergings->execute(array($entry["autoid"]));
395:
396: }
397: $dbh->commit();
398: } catch (PDOExecption $e) {
399: $dbh->rollback();
400: $deleteperson->delete();
401: foreach ($deletedlinks as $link) {
402: $link->delete();
403: }
404: print "Error!: " . $e->getMessage() . "</br>";
405: }
406: }
407:
408: private function getPDO() {
409: $manager = Application::instance()->getContainer()->get('database');
410: $connection = $manager->connection($this->dbName);
411: $dbh = $connection->getPDO();
412: return $dbh;
413: }
414:
415: private function getConnection() {
416: $manager = Application::instance()->getContainer()->get('database');
417: $connection = $manager->connection($this->dbName);
418: return $connection;
419: }
420:
421: private function setDates() {
422:
423: if (!empty($this->fodt)) {
424: $dates = explode('.', $this->fodt);
425: if (count($dates) == 3) {
426: $this->bday = $dates[0];
427: $this->bmonth = $dates[1];
428: $this->byear = $dates[2];
429: } else if (count($dates) == 1 && strlen(trim($dates[0])) == 4) {
430: $this->byear = $dates[0];
431: }
432: } else {
433: if (!empty($this->alder) && $this->annonce != NULL) {
434: if (!empty($this->annonce->getDato())) {
435: $dates = explode('-', $this->annonce->getDato());
436: if (count($dates) == 3) {
437: $this->byear = $dates[0] - $this->alder;
438: }
439: }
440: }
441: }
442:
443: if (!empty($this->dodt)) {
444: $dates = explode('.', $this->dodt);
445: if (count($dates) == 3) {
446: $this->dday = $dates[0];
447: $this->dmonth = $dates[1];
448: $this->dyear = $dates[2];
449: } else if (count($dates) == 1 && strlen(trim($dates[0])) == 4) {
450: $this->dyear = $dates[0];
451: }
452: } else {
453: if (!empty($this->alder) && $this->annonce != NULL && $this->rolle = 'avdøde') {
454: if (!empty($this->annonce->getDato())) {
455: $dates = explode('-', $this->annonce->getDato());
456: if (count($dates) == 3) {
457: $this->dyear = $dates[0];
458: }
459: }
460: }
461: }
462: }
463:
464: public function validate() {
465: $validation = array();
466: $validation['result'] = true;
467: $validation['params'] = array();
468: $this->navn = trim($this->navn);
469: $this->etternavn = trim($this->etternavn);
470: if (empty($this->navn) && empty($this->etternavn)) {
471: $validation['result'] = false;
472: if (empty($this->navn)) {
473: $validation['params'][] = 'navn';
474: }
475: if (empty($this->etternavn)) {
476: $validation['params'][] = 'etternavn';
477: }
478: }
479: return $validation;
480: }
481:
482: public function getPersonFullName() {
483: $fulNavn = '';
484: 485: 486: 487: 488: 489: 490: 491: 492: 493:
494: return $fulNavn;
495: }
496:
497: }
498: