protected function build()
 {
     parent::build();
     if ($this->human) {
         $this->joins[] = "  JOIN event_has_human ON event_has_human.event_id = event_information.id AND  event_has_human.human_id = :human_id AND event_has_human.removed_at IS NULL";
         $this->params['human_id'] = $this->human->getId();
     } else {
         if ($this->includeNoHumansOnly) {
             $this->joins[] = "  LEFT JOIN event_has_human ON event_has_human.event_id = event_information.id AND event_has_human.removed_at IS NULL";
             $this->where[] = "   event_has_human.added_at IS NULL ";
         }
     }
 }
 protected function build()
 {
     parent::build();
     if ($this->isMapItAreaOnly) {
         $this->joins[] = " JOIN area_mapit_information ON area_mapit_information.area_id = area_information.id ";
     }
     if ($this->human) {
         $this->joins[] = "  JOIN human_in_area ON human_in_area.area_id = area_information.id AND  human_in_area.human_id = :human_id AND human_in_area.removed_at IS NULL";
         $this->params['human_id'] = $this->human->getId();
     }
     if ($this->includeAreasWithNoEventsOnly) {
         $this->joins[] = " LEFT JOIN meetyournextmp_event_in_area  ON meetyournextmp_event_in_area.area_id = area_information.id ";
         $this->joins[] = " LEFT JOIN event_information ON event_information.id = meetyournextmp_event_in_area.event_id " . "AND event_information.is_deleted = '0' AND event_information.is_cancelled ='0'";
         $this->where[] = "  event_information.id IS NULL  ";
     }
 }
 public function setFromAppAndHumanAndArea(Application $app, HumanModel $humanModel, AreaModel $areaModel)
 {
     $this->human_id = $humanModel->getId();
     $this->email = $humanModel->getEmail();
     $this->subject = "Can you tell voters which hustings events you are attending?";
     $eventRepoBuilder = new EventRepositoryBuilder();
     $eventRepoBuilder->setIncludeDeleted(false);
     $eventRepoBuilder->setIncludeCancelled(false);
     $eventRepoBuilder->setAfterNow();
     $eventRepoBuilder->setArea($areaModel);
     $events = $eventRepoBuilder->fetchAll();
     $this->body_html = $app['twig']->render('email/humanEmail.html.twig', array('human' => $humanModel, 'area' => $areaModel, 'events' => $events, 'email' => $this->email));
     if ($app['config']->isDebug) {
         file_put_contents('/tmp/humanEmail.html', $this->body_html);
     }
     $this->body_text = $app['twig']->render('email/humanEmail.txt.twig', array('human' => $humanModel, 'area' => $areaModel, 'events' => $events, 'email' => $this->email));
     if ($app['config']->isDebug) {
         file_put_contents('/tmp/humanEmail.txt', $this->body_text);
     }
     $this->created_at = $app['timesource']->getDateTime();
 }
 public function markDuplicate(HumanModel $duplicatehuman, HumanModel $originalhuman, UserAccountModel $user = null)
 {
     global $DB;
     if ($duplicatehuman->getId() == $originalhuman->getId()) {
         return;
     }
     try {
         $DB->beginTransaction();
         $duplicatehuman->setIsDeleted(true);
         $duplicatehuman->setIsDuplicateOfId($originalhuman->getId());
         $this->humanDBAccess->update($duplicatehuman, array('is_deleted', 'is_duplicate_of_id'), $user);
         // Human At Event
         $statCheck = $DB->prepare("SELECT * FROM event_has_human WHERE human_id=:human_id AND " . " event_id=:event_id AND removed_at IS NULL ");
         $statAdd = $DB->prepare("INSERT INTO event_has_human (human_id,event_id,added_by_user_account_id,added_at,addition_approved_at) " . "VALUES (:human_id,:event_id,:added_by_user_account_id,:added_at,:addition_approved_at)");
         $erb = new EventRepositoryBuilder();
         $erb->sethuman($duplicatehuman);
         foreach ($erb->fetchAll() as $event) {
             // check event not already in list
             $statCheck->execute(array('human_id' => $originalhuman->getId(), 'event_id' => $event->getId()));
             if ($statCheck->rowCount() == 0) {
                 $statAdd->execute(array('human_id' => $originalhuman->getId(), 'event_id' => $event->getId(), 'added_by_user_account_id' => $user ? $user->getId() : null, 'added_at' => \TimeSource::getFormattedForDataBase(), 'addition_approved_at' => \TimeSource::getFormattedForDataBase()));
             }
         }
         $DB->commit();
     } catch (Exception $e) {
         $DB->rollBack();
     }
 }
 public function update(HumanModel $human, $fields, UserAccountModel $user = null)
 {
     $alreadyInTransaction = $this->db->inTransaction();
     // Make Information Data
     $fieldsSQL1 = array();
     $fieldsParams1 = array('id' => $human->getId());
     foreach ($fields as $field) {
         $fieldsSQL1[] = " " . $field . "=:" . $field . " ";
         if ($field == 'title') {
             $fieldsParams1['title'] = substr($human->getTitle(), 0, VARCHAR_COLUMN_LENGTH_USED);
         } else {
             if ($field == 'description') {
                 $fieldsParams1['description'] = $human->getDescription();
             } else {
                 if ($field == 'email') {
                     $fieldsParams1['email'] = $human->getEmail();
                 } else {
                     if ($field == 'twitter') {
                         $fieldsParams1['twitter'] = $human->getTwitter();
                     } else {
                         if ($field == 'party') {
                             $fieldsParams1['party'] = $human->getParty();
                         } else {
                             if ($field == 'image_url') {
                                 $fieldsParams1['image_url'] = $human->getImageUrl();
                             } else {
                                 if ($field == 'url') {
                                     $fieldsParams1['url'] = $human->getUrl();
                                 } else {
                                     if ($field == 'is_deleted') {
                                         $fieldsParams1['is_deleted'] = $human->getIsDeleted() ? 1 : 0;
                                     } else {
                                         if ($field == 'is_duplicate_of_id') {
                                             $fieldsParams1['is_duplicate_of_id'] = $human->getIsDuplicateOfId();
                                         }
                                     }
                                 }
                             }
                         }
                     }
                 }
             }
         }
     }
     // Make History Data
     $fieldsSQL2 = array('human_id', 'user_account_id', 'created_at', 'approved_at');
     $fieldsSQLParams2 = array(':human_id', ':user_account_id', ':created_at', ':approved_at');
     $fieldsParams2 = array('human_id' => $human->getId(), 'user_account_id' => $user ? $user->getId() : null, 'created_at' => $this->timesource->getFormattedForDataBase(), 'approved_at' => $this->timesource->getFormattedForDataBase());
     foreach ($this->possibleFields as $field) {
         if (in_array($field, $fields) || $field == 'title') {
             $fieldsSQL2[] = " " . $field . " ";
             $fieldsSQLParams2[] = " :" . $field . " ";
             if ($field == 'title') {
                 $fieldsParams2['title'] = substr($human->getTitle(), 0, VARCHAR_COLUMN_LENGTH_USED);
             } else {
                 if ($field == 'description') {
                     $fieldsParams2['description'] = $human->getDescription();
                 } else {
                     if ($field == 'image_url') {
                         $fieldsParams2['image_url'] = $human->getImageUrl();
                     } else {
                         if ($field == 'email') {
                             $fieldsParams2['email'] = $human->getEmail();
                         } else {
                             if ($field == 'twitter') {
                                 $fieldsParams2['twitter'] = $human->getTwitter();
                             } else {
                                 if ($field == 'party') {
                                     $fieldsParams2['party'] = $human->getParty();
                                 } else {
                                     if ($field == 'url') {
                                         $fieldsParams2['url'] = $human->getUrl();
                                     } else {
                                         if ($field == 'is_deleted') {
                                             $fieldsParams2['is_deleted'] = $human->getIsDeleted() ? 1 : 0;
                                         } else {
                                             if ($field == 'is_duplicate_of_id') {
                                                 $fieldsParams2['is_duplicate_of_id'] = $human->getIsDuplicateOfId();
                                             }
                                         }
                                     }
                                 }
                             }
                         }
                     }
                 }
             }
             $fieldsSQL2[] = " " . $field . "_changed ";
             $fieldsSQLParams2[] = " 0 ";
         } else {
             $fieldsSQL2[] = " " . $field . "_changed ";
             $fieldsSQLParams2[] = " -2 ";
         }
     }
     try {
         if (!$alreadyInTransaction) {
             $this->db->beginTransaction();
         }
         // Information SQL
         $stat = $this->db->prepare("UPDATE human_information  SET " . implode(",", $fieldsSQL1) . " WHERE id=:id");
         $stat->execute($fieldsParams1);
         // History SQL
         $stat = $this->db->prepare("INSERT INTO human_history (" . implode(",", $fieldsSQL2) . ") VALUES (" . implode(",", $fieldsSQLParams2) . ")");
         $stat->execute($fieldsParams2);
         if (!$alreadyInTransaction) {
             $this->db->commit();
         }
     } catch (Exception $e) {
         if (!$alreadyInTransaction) {
             $this->db->rollBack();
         }
         throw $e;
     }
 }
 public function fetchAll()
 {
     $this->buildStart();
     $this->build();
     $this->buildStat();
     $results = array();
     while ($data = $this->stat->fetch()) {
         $human = new HumanModel();
         $human->setFromDataBaseRow($data);
         $results[] = $human;
     }
     return $results;
 }