public function getSummary()
 {
     $txt = '';
     if ($this->areaHistoryModel->getIsNew()) {
         $txt .= 'New! ' . "\n";
     }
     if ($this->areaHistoryModel->isAnyChangeFlagsUnknown()) {
         $txt .= $this->areaHistoryModel->getDescription();
     } else {
         if ($this->areaHistoryModel->getTitleChanged()) {
             $txt .= 'Title Changed. ' . "\n";
         }
         if ($this->areaHistoryModel->getDescriptionChanged()) {
             $txt .= 'Description Changed. ' . "\n";
         }
         if ($this->areaHistoryModel->getParentAreaIdChanged()) {
             $txt .= 'Parent Area Changed. ' . "\n";
         }
         if ($this->areaHistoryModel->getCountryIdChanged()) {
             $txt .= 'Country Changed. ' . "\n";
         }
         if ($this->areaHistoryModel->getIsDeletedChanged()) {
             $txt .= 'Deleted Changed: ' . ($this->areaHistoryModel->getIsDeleted() ? "Deleted" : "Restored") . "\n\n";
         }
     }
     return $txt;
 }
 function testSetChangedFlagsFromLast1()
 {
     $lastHistory = new AreaHistoryModel();
     $lastHistory->setFromDataBaseRow(array('area_id' => 1, 'title' => 'Cat', 'description' => '', 'user_account_id' => 1, 'created_at' => '2014-02-01 10:00:00', 'parent_area_id' => '', 'country_id' => 77, 'is_deleted' => 0, 'title_changed' => 0, 'description_changed' => 0, 'country_id_changed' => 0, 'parent_area_id_changed' => 0, 'is_deleted_changed' => 0));
     $areaHistory = new AreaHistoryModel();
     $areaHistory->setFromDataBaseRow(array('area_id' => 1, 'title' => 'Cat', 'description' => 'This area has no name', 'user_account_id' => 1, 'created_at' => '2014-02-01 12:00:00', 'parent_area_id' => 6, 'country_id' => 78, 'is_deleted' => 0, 'title_changed' => 0, 'description_changed' => 0, 'country_id_changed' => 0, 'parent_area_id_changed' => 0, 'is_deleted_changed' => 0));
     $areaHistory->setChangedFlagsFromLast($lastHistory);
     $this->assertEquals(false, $areaHistory->getTitleChanged());
     $this->assertEquals(true, $areaHistory->getDescriptionChanged());
     $this->assertEquals(true, $areaHistory->getCountryIdChanged());
     $this->assertEquals(true, $areaHistory->getParentAreaIdChanged());
     $this->assertEquals(false, $areaHistory->getIsDeletedChanged());
     $this->assertEquals(false, $areaHistory->getIsNew());
 }
 public function ensureChangedFlagsAreSet(AreaHistoryModel $areaHistory)
 {
     global $DB;
     // do we already have them?
     if (!$areaHistory->isAnyChangeFlagsUnknown()) {
         return;
     }
     // load last.
     $stat = $DB->prepare("SELECT * FROM area_history WHERE area_id = :id AND created_at < :at " . "ORDER BY created_at DESC");
     $stat->execute(array('id' => $areaHistory->getId(), 'at' => $areaHistory->getCreatedAt()->format("Y-m-d H:i:s")));
     if ($stat->rowCount() == 0) {
         $areaHistory->setChangedFlagsFromNothing();
     } else {
         while ($areaHistory->isAnyChangeFlagsUnknown() && ($lastHistoryData = $stat->fetch())) {
             $lastHistory = new AreaHistoryModel();
             $lastHistory->setFromDataBaseRow($lastHistoryData);
             $areaHistory->setChangedFlagsFromLast($lastHistory);
         }
     }
     // Save back to DB
     $sqlFields = array();
     $sqlParams = array('id' => $areaHistory->getId(), 'created_at' => $areaHistory->getCreatedAt()->format("Y-m-d H:i:s"), 'is_new' => $areaHistory->getIsNew() ? 1 : 0);
     if ($areaHistory->getTitleChangedKnown()) {
         $sqlFields[] = " title_changed = :title_changed ";
         $sqlParams['title_changed'] = $areaHistory->getTitleChanged() ? 1 : -1;
     }
     if ($areaHistory->getDescriptionChangedKnown()) {
         $sqlFields[] = " description_changed = :description_changed ";
         $sqlParams['description_changed'] = $areaHistory->getDescriptionChanged() ? 1 : -1;
     }
     if ($areaHistory->getCountryIdChangedKnown()) {
         $sqlFields[] = " country_id_changed = :country_id_changed ";
         $sqlParams['country_id_changed'] = $areaHistory->getCountryIdChanged() ? 1 : -1;
     }
     if ($areaHistory->getParentAreaIdChangedKnown()) {
         $sqlFields[] = " parent_area_id_changed = :parent_area_id_changed ";
         $sqlParams['parent_area_id_changed'] = $areaHistory->getParentAreaIdChanged() ? 1 : -1;
     }
     if ($areaHistory->getIsDuplicateOfIdChangedKnown()) {
         $sqlFields[] = " is_duplicate_of_id_changed  = :is_duplicate_of_id_changed ";
         $sqlParams['is_duplicate_of_id_changed'] = $areaHistory->getIsDuplicateOfIdChangedKnown() ? 1 : -1;
     }
     if ($areaHistory->getIsDeletedChangedKnown()) {
         $sqlFields[] = " is_deleted_changed = :is_deleted_changed ";
         $sqlParams['is_deleted_changed'] = $areaHistory->getIsDeletedChanged() ? 1 : -1;
     }
     $statUpdate = $DB->prepare("UPDATE area_history SET " . " is_new = :is_new, " . implode(" , ", $sqlFields) . " WHERE area_id = :id AND created_at = :created_at");
     $statUpdate->execute($sqlParams);
 }