1: <?php
2:
3:
4:
5: namespace app\controllers;
6:
7:
8:
9:
10:
11:
12:
13:
14: 15: 16: 17: 18: 19:
20: class PersonCart {
21:
22:
23: protected $messageText = array("parentsnotmerged" => "Lenking kan ikke utføres. Lenk foreldrene først.");
24: private $session;
25: private $cart;
26: private $todelete;
27: private $message;
28: public $debug = array();
29: private $user;
30:
31: public function setUser($user) {
32: $this->user = $user;
33: }
34:
35: public function setSession($s) {
36: $this->session = $s;
37: }
38:
39: public function getMessage() {
40: return $this->message;
41: }
42:
43: private function cartHasPerson($cart, $id) {
44: foreach ($cart as $entry) {
45: if (array_key_exists('id', $entry) && $entry['id'] == $id) {
46: return true;
47: }
48: }
49: return false;
50: }
51:
52:
53: public function addToCart($type, $id, $score) {
54:
55: $pcm = new PersonControllerMaps();
56: $facade = new PersonFacade($type, $id);
57: $current = $facade->getCurrent();
58:
59: $currenttype = $current["type"];
60: $currentid = $current["id"];
61: $cart = array();
62: $person = $facade->getPerson();
63:
64: if ($this->session->has('cart')) {
65: $cart = $this->session->get('cart');
66: if ($this->cartHasPerson($cart, $currentid) != 'true') {
67: $cart[] = array("type" => $currenttype, "id" => $currentid, "score" => $score, "person" => $person);
68: }
69: } else {
70: $cart = array();
71: $cart[] = array("type" => $currenttype, "id" => $currentid, "score" => $score, "person" => $person);
72: }
73:
74: $this->session->put('cart', $cart);
75: return $cart;
76: }
77:
78: public function getCart() {
79: if ($this->session->has('cart')) {
80: return $this->session->get('cart');
81: } else {
82: return NULL;
83: }
84: }
85:
86: public function emptyCart() {
87: if ($this->session->has('cart')) {
88: $this->session->remove('cart');
89: }
90: }
91:
92: private function getIndexes($request, $key) {
93:
94: $result = array();
95: foreach ($request as $item) {
96: if (strpos($item, $key) !== false) {
97: $result[] = substr($item, strlen($key));
98: }
99: }
100: return $result;
101: }
102:
103: private function removeFromMerging($mergingbox, $indx) {
104: $m = array();
105: $size1 = count($mergingbox);
106: $size2 = count($indx);
107: for ($ii = 0; $ii < $size1; $ii++) {
108:
109: for ($jj = 0; $jj < $size2; $jj++) {
110: if ($ii == $indx[$jj]) {
111: unset($mergingbox[$ii]);
112:
113: }
114: }
115: }
116: $mergingbox = array_values($mergingbox);
117: return $mergingbox;
118: }
119:
120: private function availableForMerging() {
121: if ($this->cart != NULL && !empty($this->cart) && count($this->cart) > 1) {
122: return true;
123: } else {
124: return false;
125: }
126: }
127:
128:
129:
130:
131: private function getEntryArray($item, $user) {
132:
133: $type = $item['type'];
134: $pcm = new PersonControllerMaps();
135: switch ($type) {
136: case $pcm->types['hbr']:
137: $person = new Person();
138: $data = $person->getPersonByPFID($item["id"]);
139: $res = array();
140: if (!empty($data) && !empty($data['pfids']) && !empty($data['types'])) {
141: $i = 0;
142: foreach ($data['pfids'] as $pfid) {
143: $newitem = array('id' => $pfid, 'type' => $data['types'][$i]);
144: $getentry = $this->getEntryArray($newitem, $user);
145: if (is_array($getentry) && count($getentry) > 0) {
146: $res[] = $getentry[0];
147: }
148: }
149: }
150: if ($this->todelete == NULL) {
151: $this->todelete = array();
152: }
153: $this->todelete[] = $person;
154: return $res;
155: break;
156: case $pcm->types['da']:
157: $daquery = new DAQuery();
158: $entries = $daquery->getDAPerson($item["id"]);
159: return array(array("id" => $entries->individual->autoid, "pfid" => $entries->individual->id, "type" => $type, "score" => $item['score'],
160: "marker" => $user["marker"], "user_id" => $user["id"]));
161: break;
162: case $pcm->types['avis']:
163: $avisquery = new AvisQuery();
164: $entries = $avisquery->getEntries($item["id"]);
165: return array(array("id" => $entries->autoid, "pfid" => $entries->pfid, "type" => $type, "score" => $item['score'],
166: "marker" => $user["marker"], "user_id" => $user["id"]));
167: break;
168: }
169: return NULL;
170: }
171:
172: private function deleteOld($newid) {
173: if (!empty($this->todelete)) {
174: foreach ($this->todelete as $item) {
175: $item->setUser($this->user);
176: $item->delete($newid);
177: }
178: }
179: }
180:
181: private function parentsMerged() {
182: if ($this->cart == NULL)
183: $this->cart = $this->session->get('cart');
184: if ($this->cart == NULL)
185: return false;
186: $parents = array("mor" => array(), "far" => array());
187: foreach ($this->cart as $entry) {
188: if (!empty($entry["person"]["family"]["Foreldre og søsken"])) {
189: foreach ($entry["person"]["family"]["Foreldre og søsken"] as $family) {
190: foreach ($family as $member) {
191: if (array_key_exists($member[0], $parents)) {
192: $parents[$member[0]][] = $member[1]['pfid'];
193: }
194: }
195: }
196: }
197: }
198: if (count($parents["mor"]) < 2 && count($parents["far"]) < 2)
199: return true;
200: else {
201: foreach ($parents as $parent) {
202: $person = new Person();
203: if (count($parent) > 1 && $person->belongToCommon($parent) !== true) {
204: $this->addMessage($this->messageText["parentsnotmerged"]);
205: return false;
206: }
207: }
208: }
209: return true;
210: }
211:
212: private function addMessage($message) {
213: if ($this->message == NULL) {
214: $this->message = array();
215: }
216: $this->message[] = $message;
217: }
218:
219: public function automerge($cluster, $user) {
220:
221: $this->cart = array();
222: foreach ($cluster['entries'] as $entry) {
223: $this->cart[] = array("id" => $entry[0], "score" => $entry[1], "type" => "daid");
224: }
225: $person = new Person();
226: $person->setUser($user);
227: $indx = 0;
228: $this->mergeCart($indx, $person);
229: }
230:
231: private function getPersonforCartItem($item)
232: {
233:
234: $pcm = new PersonControllerMaps();
235: $type = $item["type"];
236: $person = new Person();
237: if ($type == $pcm->types['hbr']) {
238: $person->getPersonByPFID($item['id']);
239: } else if ($type == $pcm->types['da']) {
240: $daquery = new DAQuery();
241: $entries = $daquery->getDAPerson($item['id']);
242: $persondata = $daquery->getPersonfromFK($entries);
243: $person->setFieldsFromAPI($persondata);
244: } else if ($type == $pcm->types['avis']) {
245: $avisquery = new AvisQuery();
246: $entries = $avisquery->getEntries($item['id']);
247: $persondata = $avisquery->getPersonfromFK($entries);
248: $person->setFieldsFromAPI($persondata);
249: }
250: return $person;
251: }
252:
253: private function collectPersonData()
254: {
255: if ($this->cart == NULL)
256: $this->cart = $this->session->get('cart');
257: if($this->cart != NULL)
258: {
259: foreach($this->cart as $index => $item)
260: {
261:
262: $this->cart[$indx]["person"] = $this->getPersonforCartItem($item);
263: }
264: }
265: }
266:
267: private function mergeCartNew($indx, $person)
268: {
269: if ($this->cart == NULL)
270: $this->cart = $this->session->get('cart');
271: $this->collectPersonData();
272: $person->populatePersonFromCart($this->cart, $indx);
273:
274: }
275:
276: private function mergeCart($indx, $person)
277: {
278:
279: if ($this->cart == NULL)
280: $this->cart = $this->session->get('cart');
281: $pcm = new PersonControllerMaps();
282: $type = $this->cart[$indx]["type"];
283: if ($type == $pcm->types['hbr']) {
284:
285: $person->getPersonByPFID($this->cart[$indx]["id"]);
286: for ($ii = 0; $ii < count($this->cart); $ii++) {
287: if ($ii != $indx) {
288: $person->addEntries($this->getEntryArray($this->cart[$ii], $person->getUser()));
289: }
290: }
291: $person->saveEntries();
292: } else if ($type == $pcm->types['da']) {
293: $daquery = new DAQuery();
294: $entries = $daquery->getDAPerson($this->cart[$indx]["id"]);
295: $persondata = $daquery->getPersonfromFK($entries);
296: $person->setFieldsFromAPI($persondata);
297: for ($ii = 0; $ii < count($this->cart); $ii++) {
298: $person->addEntries($this->getEntryArray($this->cart[$ii], $person->getUser()));
299: }
300: $newid = $person->save();
301: } else if ($type == $pcm->types['avis']) {
302: $avisquery = new AvisQuery();
303: $entries = $avisquery->getEntries($this->cart[$indx]["id"]);
304: $persondata = $avisquery->getPersonfromFK($entries);
305: $person->setFieldsFromAPI($persondata);
306: for ($ii = 0; $ii < count($this->cart); $ii++) {
307: $person->addEntries($this->getEntryArray($this->cart[$ii], $person->getUser()));
308: }
309: $newid = $person->save();
310: return $newid;
311: }else {
312:
313: }
314: }
315:
316: public function merge($request, $user) {
317: if ($user["id"] == NULL)
318: return;
319: $this->user = $user;
320: $key = 'main';
321: $indx = -1;
322: if (array_key_exists($key, $request)) {
323: $indx = $request[$key];
324: } else {
325: return "main person undefined";
326: }
327: $todelete = array();
328: $person = new Person();
329: $person->setUser($user);
330:
331: if ($this->cart == NULL)
332: $this->cart = $this->session->get('cart');
333:
334: if ($this->parentsMerged() === true) {
335: $newid = $this->mergeCart($indx, $person);
336:
337:
338:
339: $this->deleteOld($newid);
340:
341:
342: $this->emptyCart();
343:
344: }
345: }
346:
347: public function removeFromCart($request) {
348:
349: $key = 'mergecheck';
350: if ($this->session->has('cart')) {
351: $cart = $this->session->get('cart');
352: $inds = $this->getIndexes($request, $key);
353: $cart = $this->removeFromMerging($cart, $inds);
354: if (empty($cart)) {
355: $this->session->remove('cart');
356: } else {
357: $this->session->put('cart', $cart);
358: }
359: } else {
360: return NULL;
361: }
362: }
363:
364: }
365: