public function save(Default_Model_AppContactOtherItem $value)
 {
     global $application;
     $data = array();
     if (!isnull($value->getAppID())) {
         $data['appid'] = $value->getAppID();
     }
     if (!isnull($value->getResearcherID())) {
         $data['researcherid'] = $value->getResearcherID();
     }
     if (!isnull($value->getItem())) {
         $data['item'] = $value->getItem();
     }
     if (!isnull($value->getNote())) {
         $data['note'] = $value->getNote();
     }
     $q1 = array('appid = ?', 'researcherid = ?', 'item = ?');
     $q2 = array($value->appid, $value->researcherid, $value->item);
     $select = $this->getDbTable()->select();
     for ($i = 0; $i < count($q1); $i++) {
         $select->where($q1[$i], $q2[$i]);
     }
     $new_entry = count($this->getDbTable()->fetchAll($select)) == 0;
     if ($new_entry) {
         $this->getDbTable()->insert($data);
     } else {
         $s = array();
         for ($i = 0; $i < count($q1); $i++) {
             $s[] = $this->getDbTable()->getAdapter()->quoteInto($q1[$i], $q2[$i]);
         }
         $this->getDbTable()->update($data, $s);
     }
 }
Example #2
0
 private function syncAppContactItems($appid, $data)
 {
     $collection = new Default_Model_AppContactItems();
     $collection->filter->appid->equals($appid);
     for ($i = $collection->count() - 1; $i >= 0; $i--) {
         $found = false;
         foreach ($data as $key => $value) {
             if (substr($key, 0, 6) === "cntpnt") {
                 $datum = json_decode($value, true);
                 if ($datum['itemtype'] === $collection->items[$i]->itemType && $datum['researcherid'] == $collection->items[$i]->researcherID && $datum['item'] == $collection->items[$i]->item && $datum['itemid'] == $collection->items[$i]->itemID) {
                     $found = true;
                     break;
                 }
             }
         }
         if (!$found) {
             $col2 = null;
             switch ($collection->items[$i]->itemType) {
                 case "vo":
                     $col2 = new Default_Model_AppContactVOs();
                     $col2->filter->appid->equals($appid)->and($col2->filter->void->equals($collection->items[$i]->itemID))->and($col2->filter->researcherid->equals($collection->items[$i]->researcherID));
                     if (count($col2->items) > 0) {
                         $col2->remove(0);
                     }
                     break;
                 case "middleware":
                     $col2 = new Default_Model_AppContactMiddlewares();
                     $col2->filter->appid->equals($appid)->and($col2->filter->appmiddlewareid->equals($collection->items[$i]->itemID))->and($col2->filter->researcherid->equals($collection->items[$i]->researcherID));
                     if (count($col2->items) > 0) {
                         $col2->remove(0);
                     }
                     break;
                 case "other":
                     $col2 = new Default_Model_AppContactOtherItems();
                     $col2->filter->appid->equals($appid)->and($col2->filter->item->equals($collection->items[$i]->item))->and($col2->filter->researcherid->equals($collection->items[$i]->researcherID));
                     if (count($col2->items) > 0) {
                         $col2->remove(0);
                     }
                     break;
             }
         }
     }
     $collection->refresh();
     foreach ($data as $key => $value) {
         if (substr($key, 0, 6) === "cntpnt") {
             $datum = json_decode($value, true);
             $found = false;
             for ($i = $collection->count() - 1; $i >= 0; $i--) {
                 if ($datum['itemtype'] === $collection->items[$i]->itemType && $datum['researcherid'] == $collection->items[$i]->researcherID && $datum['item'] == $collection->items[$i]->item && $datum['itemid'] == $collection->items[$i]->itemID) {
                     $found = true;
                     break;
                 }
             }
             if (!$found) {
                 $item = null;
                 switch ($datum['itemtype']) {
                     case "vo":
                         $item = new Default_Model_AppContactVO();
                         $item->void = $datum['itemid'];
                         break;
                     case "middleware":
                         $mws = new Default_Model_AppMiddlewares();
                         $mws->filter->appid->equals($appid);
                         $mwid = null;
                         for ($j = 0; $j <= count($mws->items); $j++) {
                             if ($datum['itemid'] == 5) {
                                 // custom middleware, check comment
                                 if ($mws->items[$j]->comment == $datum['item']) {
                                     $mwid = $mws->items[$j]->id;
                                     break;
                                 }
                             } else {
                                 // predefined middleware
                                 if ($mws->items[$j]->middlewareID == $datum['itemid']) {
                                     $mwid = $mws->items[$j]->id;
                                     break;
                                 }
                             }
                         }
                         if ($mwid !== null) {
                             $item = new Default_Model_AppContactMiddleware();
                             $item->appmiddlewareid = $mwid;
                         }
                         break;
                     case "other":
                         $item = new Default_Model_AppContactOtherItem();
                         $item->item = $datum['item'];
                         break;
                 }
                 if ($item !== null) {
                     $item->appid = $appid;
                     $item->researcherid = $datum['researcherid'];
                     $item->save();
                 } else {
                     error_log('warning: could not match to-be-inserted posted appContactItem to appropriate DB item. Possible data loss');
                     error_log('posted appContactItem data: ' . var_export($datum, true));
                 }
             }
         }
     }
 }