protected function run()
 {
     $importURLHistoryRepo = new ImportURLHistoryRepository();
     $stat = $this->app['db']->prepare("SELECT * FROM import_url_history");
     $stat->execute();
     $count = 0;
     while ($data = $stat->fetch()) {
         $importURLHistory = new ImportURLHistoryModel();
         $importURLHistory->setFromDataBaseRow($data);
         $importURLHistoryRepo->ensureChangedFlagsAreSet($importURLHistory);
         ++$count;
     }
     return array('result' => 'ok', 'count' => $count);
 }
 public function getSummary()
 {
     $txt = '';
     if ($this->importURLHistoryModel->getIsNew()) {
         $txt .= 'New! ' . "\n";
     }
     if ($this->importURLHistoryModel->isAnyChangeFlagsUnknown()) {
         $txt .= $this->importURLHistoryModel->getTitle();
     } else {
         if ($this->importURLHistoryModel->getTitleChanged()) {
             $txt .= 'Title Changed. ' . "\n";
         }
         if ($this->importURLHistoryModel->getCountryIdChanged()) {
             $txt .= 'Country Changed. ' . "\n";
         }
         if ($this->importURLHistoryModel->getAreaIdChanged()) {
             $txt .= 'Area Changed. ' . "\n";
         }
         if ($this->importURLHistoryModel->getIsEnabledChanged()) {
             $txt .= 'Enabled Changed: ' . ($this->importURLHistoryModel->getIsEnabled() ? "Enabled" : "Disabled") . "\n\n";
         }
         if ($this->importURLHistoryModel->getExpiredAtChanged()) {
             $txt .= 'Expired Changed: ' . ($this->importURLHistoryModel->getExpiredAt() ? "Expired" : "Not Expired") . "\n\n";
         }
     }
     return $txt;
 }
 public function fetchAll()
 {
     global $DB, $app;
     $results = array();
     /////////////////////////// Events History
     if ($this->historyRepositoryBuilderConfig->getIncludeEventHistory()) {
         $where = array();
         $joins = array();
         $params = array();
         if ($this->historyRepositoryBuilderConfig->getEvent()) {
             $where[] = 'event_information.id=:event';
             $params['event'] = $this->historyRepositoryBuilderConfig->getEvent()->getId();
         }
         if ($this->historyRepositoryBuilderConfig->getGroup()) {
             // We use a seperate table here so if event is in 2 groups and we select events in 1 group that isn't the main group only,
             // the normal event_in_group table still shows the main group.
             $joins[] = " JOIN event_in_group AS event_in_group_select ON event_in_group_select.event_id = event_information.id " . "AND event_in_group_select.removed_at IS NULL AND event_in_group_select.group_id = :group_id ";
             $params['group_id'] = $this->historyRepositoryBuilderConfig->getGroup()->getId();
         }
         if ($this->historyRepositoryBuilderConfig->getSite()) {
             $where[] = 'event_information.site_id =:site';
             $params['site'] = $this->historyRepositoryBuilderConfig->getSite()->getId();
         }
         if ($this->historyRepositoryBuilderConfig->getVenue()) {
             $where[] = 'event_information.venue_id = :venue';
             $params['venue'] = $this->historyRepositoryBuilderConfig->getVenue()->getId();
         }
         if ($this->historyRepositoryBuilderConfig->getSince()) {
             $where[] = ' event_history.created_at >= :since ';
             $params['since'] = $this->historyRepositoryBuilderConfig->getSince()->format("Y-m-d H:i:s");
         }
         if ($this->historyRepositoryBuilderConfig->getNotUser()) {
             $where[] = 'event_history.user_account_id != :userid ';
             $params['userid'] = $this->historyRepositoryBuilderConfig->getNotUser()->getId();
         }
         if ($this->historyRepositoryBuilderConfig->getApi2app()) {
             $where[] = 'event_history.api2_application_id  = :api2app';
             $params['api2app'] = $this->historyRepositoryBuilderConfig->getApi2app()->getId();
         }
         if ($this->historyRepositoryBuilderConfig->getArea()) {
             $areaids = array($this->historyRepositoryBuilderConfig->getArea()->getId());
             $this->statAreas = $DB->prepare("SELECT area_id FROM cached_area_has_parent WHERE has_parent_area_id=:id");
             $this->statAreas->execute(array('id' => $this->historyRepositoryBuilderConfig->getArea()->getId()));
             while ($d = $this->statAreas->fetch()) {
                 $areaids[] = $d['area_id'];
             }
             $joins[] = " LEFT JOIN venue_information ON  event_information.venue_id = venue_information.id ";
             $where[] = ' (event_information.area_id IN (' . implode(",", $areaids) . ')  OR venue_information.area_id IN (' . implode(",", $areaids) . ') ) ';
         }
         if ($this->historyRepositoryBuilderConfig->getVenueVirtualOnly()) {
             // we check both on an OR, that way we get both
             // a) events that were not virtual and became virtual, we get their full history
             // b) events that were virtual and now aren't, we get some of their history
             $where[] = " ( event_information.is_virtual = '1' OR event_history.is_virtual = '1' )";
         }
         $sql = "SELECT event_history.*, group_information.title AS group_title,  group_information.id AS group_id,  event_information.slug AS event_slug, user_account_information.username AS user_account_username FROM event_history " . " LEFT JOIN user_account_information ON user_account_information.id = event_history.user_account_id " . " LEFT JOIN event_information ON event_information.id = event_history.event_id " . " LEFT JOIN event_in_group ON event_in_group.event_id = event_information.id AND event_in_group.removed_at IS NULL AND event_in_group.is_main_group = '1' " . " LEFT JOIN group_information ON group_information.id = event_in_group.group_id " . implode(" ", $joins) . ($where ? " WHERE " . implode(" AND ", $where) : "") . " ORDER BY event_history.created_at DESC LIMIT " . $this->historyRepositoryBuilderConfig->getLimit();
         //var_dump($sql); var_dump($params);
         $stat = $DB->prepare($sql);
         $stat->execute($params);
         while ($data = $stat->fetch()) {
             $eventHistory = new EventHistoryModel();
             $eventHistory->setFromDataBaseRow($data);
             $results[] = $eventHistory;
         }
     }
     /////////////////////////// Group History
     if ($this->historyRepositoryBuilderConfig->getIncludeGroupHistory()) {
         $where = array();
         $params = array();
         if ($this->historyRepositoryBuilderConfig->getEvent() && $this->historyRepositoryBuilderConfig->getEvent()->getGroupId()) {
             $where[] = 'group_information.id=:group';
             $params['group'] = $this->historyRepositoryBuilderConfig->getEvent()->getGroupId();
         } else {
             if ($this->historyRepositoryBuilderConfig->getGroup()) {
                 $where[] = 'group_information.id =:group';
                 $params['group'] = $this->historyRepositoryBuilderConfig->getGroup()->getId();
             }
         }
         if ($this->historyRepositoryBuilderConfig->getSite()) {
             $where[] = 'group_information.site_id =:site';
             $params['site'] = $this->historyRepositoryBuilderConfig->getSite()->getId();
         }
         if ($this->historyRepositoryBuilderConfig->getSince()) {
             $where[] = ' group_history.created_at >= :since ';
             $params['since'] = $this->historyRepositoryBuilderConfig->getSince()->format("Y-m-d H:i:s");
         }
         if ($this->historyRepositoryBuilderConfig->getNotUser()) {
             $where[] = 'group_history.user_account_id != :userid ';
             $params['userid'] = $this->historyRepositoryBuilderConfig->getNotUser()->getId();
         }
         if ($this->historyRepositoryBuilderConfig->getApi2app()) {
             $where[] = 'group_history.api2_application_id  = :api2app';
             $params['api2app'] = $this->historyRepositoryBuilderConfig->getApi2app()->getId();
         }
         $sql = "SELECT group_history.*, group_information.slug AS group_slug, user_account_information.username AS user_account_username FROM group_history " . " LEFT JOIN user_account_information ON user_account_information.id = group_history.user_account_id " . " LEFT JOIN group_information ON group_information.id = group_history.group_id " . ($where ? " WHERE " . implode(" AND ", $where) : "") . " ORDER BY group_history.created_at DESC LIMIT " . $this->historyRepositoryBuilderConfig->getLimit();
         //var_dump($sql); var_dump($params);
         $stat = $DB->prepare($sql);
         $stat->execute($params);
         while ($data = $stat->fetch()) {
             $groupHistory = new GroupHistoryModel();
             $groupHistory->setFromDataBaseRow($data);
             $results[] = $groupHistory;
         }
     }
     /////////////////////////// Venue History
     if ($this->historyRepositoryBuilderConfig->getIncludeVenueHistory()) {
         $where = array();
         $params = array();
         if ($this->historyRepositoryBuilderConfig->getEvent() && $this->historyRepositoryBuilderConfig->getEvent()->getVenueId()) {
             $where[] = 'venue_information.id=:venue';
             $params['venue'] = $this->historyRepositoryBuilderConfig->getEvent()->getVenueId();
         } else {
             if ($this->historyRepositoryBuilderConfig->getVenue()) {
                 $where[] = 'venue_information.id=:venue';
                 $params['venue'] = $this->historyRepositoryBuilderConfig->getVenue()->getId();
             }
         }
         if ($this->historyRepositoryBuilderConfig->getSite()) {
             $where[] = 'venue_information.site_id =:site';
             $params['site'] = $this->historyRepositoryBuilderConfig->getSite()->getId();
         }
         if ($this->historyRepositoryBuilderConfig->getSince()) {
             $where[] = ' venue_history.created_at >= :since ';
             $params['since'] = $this->historyRepositoryBuilderConfig->getSince()->format("Y-m-d H:i:s");
         }
         if ($this->historyRepositoryBuilderConfig->getNotUser()) {
             $where[] = 'venue_history.user_account_id != :userid ';
             $params['userid'] = $this->historyRepositoryBuilderConfig->getNotUser()->getId();
         }
         if ($this->historyRepositoryBuilderConfig->getApi2app()) {
             $where[] = 'venue_history.api2_application_id  = :api2app';
             $params['api2app'] = $this->historyRepositoryBuilderConfig->getApi2app()->getId();
         }
         $sql = "SELECT venue_history.*, venue_information.slug AS venue_slug, user_account_information.username AS user_account_username FROM venue_history " . " LEFT JOIN user_account_information ON user_account_information.id = venue_history.user_account_id " . " LEFT JOIN venue_information ON venue_information.id = venue_history.venue_id " . ($where ? " WHERE " . implode(" AND ", $where) : "") . " ORDER BY venue_history.created_at DESC LIMIT " . $this->historyRepositoryBuilderConfig->getLimit();
         //var_dump($sql); var_dump($params);
         $stat = $DB->prepare($sql);
         $stat->execute($params);
         while ($data = $stat->fetch()) {
             $venueHistory = new VenueHistoryModel();
             $venueHistory->setFromDataBaseRow($data);
             $results[] = $venueHistory;
         }
     }
     /////////////////////////// Area History
     if ($this->historyRepositoryBuilderConfig->getIncludeAreaHistory()) {
         $where = array();
         $params = array();
         $joins = array();
         if ($this->historyRepositoryBuilderConfig->getArea()) {
             // Will this produce dupes? No evidence so far but there was a note in EventRepositoryBuilder that said so.
             $joins[] = " LEFT JOIN cached_area_has_parent ON cached_area_has_parent.area_id = area_information.id ";
             $where[] = ' (area_information.id =:area OR cached_area_has_parent.has_parent_area_id =:area )';
             $params['area'] = $this->historyRepositoryBuilderConfig->getArea()->getId();
         }
         if ($this->historyRepositoryBuilderConfig->getSite()) {
             $where[] = 'area_information.site_id =:site';
             $params['site'] = $this->historyRepositoryBuilderConfig->getSite()->getId();
         }
         if ($this->historyRepositoryBuilderConfig->getSince()) {
             $where[] = ' area_history.created_at >= :since ';
             $params['since'] = $this->historyRepositoryBuilderConfig->getSince()->format("Y-m-d H:i:s");
         }
         if ($this->historyRepositoryBuilderConfig->getNotUser()) {
             $where[] = 'area_history.user_account_id != :userid ';
             $params['userid'] = $this->historyRepositoryBuilderConfig->getNotUser()->getId();
         }
         if ($this->historyRepositoryBuilderConfig->getApi2app()) {
             $where[] = 'area_history.api2_application_id  = :api2app';
             $params['api2app'] = $this->historyRepositoryBuilderConfig->getApi2app()->getId();
         }
         $sql = "SELECT area_history.*, area_information.slug AS area_slug, user_account_information.username AS user_account_username FROM area_history " . " LEFT JOIN user_account_information ON user_account_information.id = area_history.user_account_id " . " LEFT JOIN area_information ON area_information.id = area_history.area_id " . implode(" ", $joins) . ($where ? " WHERE " . implode(" AND ", $where) : "") . " ORDER BY area_history.created_at DESC LIMIT " . $this->historyRepositoryBuilderConfig->getLimit();
         //var_dump($sql); var_dump($params);
         $stat = $DB->prepare($sql);
         $stat->execute($params);
         while ($data = $stat->fetch()) {
             $areaHistory = new AreaHistoryModel();
             $areaHistory->setFromDataBaseRow($data);
             $results[] = $areaHistory;
         }
     }
     /////////////////////////// Tags History
     if ($this->historyRepositoryBuilderConfig->getIncludeTagHistory()) {
         $where = array();
         $params = array();
         if ($this->historyRepositoryBuilderConfig->getSite()) {
             $where[] = 'tag_information.site_id =:site';
             $params['site'] = $this->historyRepositoryBuilderConfig->getSite()->getId();
         }
         if ($this->historyRepositoryBuilderConfig->getSince()) {
             $where[] = ' tag_history.created_at >= :since ';
             $params['since'] = $this->historyRepositoryBuilderConfig->getSince()->format("Y-m-d H:i:s");
         }
         if ($this->historyRepositoryBuilderConfig->getNotUser()) {
             $where[] = 'tag_history.user_account_id != :userid ';
             $params['userid'] = $this->historyRepositoryBuilderConfig->getNotUser()->getId();
         }
         if ($this->historyRepositoryBuilderConfig->getApi2app()) {
             $where[] = 'tag_history.api2_application_id  = :api2app';
             $params['api2app'] = $this->historyRepositoryBuilderConfig->getApi2app()->getId();
         }
         $sql = "SELECT tag_history.*, tag_information.slug AS tag_slug, user_account_information.username AS user_account_username FROM tag_history " . " LEFT JOIN user_account_information ON user_account_information.id = tag_history.user_account_id " . " LEFT JOIN tag_information ON tag_information.id = tag_history.tag_id " . ($where ? " WHERE " . implode(" AND ", $where) : "") . " ORDER BY tag_history.created_at DESC LIMIT " . $this->historyRepositoryBuilderConfig->getLimit();
         //var_dump($sql); var_dump($params);
         $stat = $DB->prepare($sql);
         $stat->execute($params);
         while ($data = $stat->fetch()) {
             $tagHistory = new TagHistoryModel();
             $tagHistory->setFromDataBaseRow($data);
             $results[] = $tagHistory;
         }
     }
     /////////////////////////// Import URL History
     if ($this->historyRepositoryBuilderConfig->getIncludeImportURLHistory()) {
         $where = array();
         $params = array();
         if ($this->historyRepositoryBuilderConfig->getSite()) {
             $where[] = 'import_url_information.site_id =:site';
             $params['site'] = $this->historyRepositoryBuilderConfig->getSite()->getId();
         }
         if ($this->historyRepositoryBuilderConfig->getGroup()) {
             $where[] = 'import_url_information.group_id =:group';
             $params['group'] = $this->historyRepositoryBuilderConfig->getGroup()->getId();
         }
         if ($this->historyRepositoryBuilderConfig->getSince()) {
             $where[] = ' import_url_history.created_at >= :since ';
             $params['since'] = $this->historyRepositoryBuilderConfig->getSince()->format("Y-m-d H:i:s");
         }
         if ($this->historyRepositoryBuilderConfig->getNotUser()) {
             $where[] = 'import_url_history.user_account_id != :userid ';
             $params['userid'] = $this->historyRepositoryBuilderConfig->getNotUser()->getId();
         }
         if ($this->historyRepositoryBuilderConfig->getApi2app()) {
             $where[] = 'import_url_history.api2_application_id  = :api2app';
             $params['api2app'] = $this->historyRepositoryBuilderConfig->getApi2app()->getId();
         }
         $sql = "SELECT import_url_history.*, import_url_information.slug AS import_url_slug, " . "user_account_information.username AS user_account_username " . " FROM import_url_history " . " LEFT JOIN user_account_information ON user_account_information.id = import_url_history.user_account_id " . " LEFT JOIN import_url_information ON import_url_information.id = import_url_history.import_url_id " . ($where ? " WHERE " . implode(" AND ", $where) : "") . " ORDER BY import_url_history.created_at DESC LIMIT " . $this->historyRepositoryBuilderConfig->getLimit();
         //var_dump($sql); var_dump($params);
         $stat = $DB->prepare($sql);
         $stat->execute($params);
         while ($data = $stat->fetch()) {
             $tagHistory = new ImportURLHistoryModel();
             $tagHistory->setFromDataBaseRow($data);
             $results[] = $tagHistory;
         }
     }
     ////////////////////// Others!
     foreach ($app['extensions']->getExtensions() as $ext) {
         $results = array_merge($results, $ext->getHistoryRepositoryBuilderData($this->historyRepositoryBuilderConfig));
     }
     ////////////////////// Finally sort & truncate
     $usort = function ($a, $b) {
         if ($a->getCreatedAtTimeStamp() == $b->getCreatedAtTimeStamp()) {
             return 0;
         } else {
             if ($a->getCreatedAtTimeStamp() > $b->getCreatedAtTimeStamp()) {
                 return -1;
             } else {
                 return 1;
             }
         }
     };
     usort($results, $usort);
     array_slice($results, 0, $this->historyRepositoryBuilderConfig->getLimit());
     return $results;
 }
 public function ensureChangedFlagsAreSet(ImportURLHistoryModel $importurlhistory)
 {
     global $DB;
     // do we already have them?
     if (!$importurlhistory->isAnyChangeFlagsUnknown()) {
         return;
     }
     // load last.
     $stat = $DB->prepare("SELECT * FROM import_url_history WHERE import_url_id = :id AND created_at < :at " . "ORDER BY created_at DESC");
     $stat->execute(array('id' => $importurlhistory->getId(), 'at' => $importurlhistory->getCreatedAt()->format("Y-m-d H:i:s")));
     if ($stat->rowCount() == 0) {
         $importurlhistory->setChangedFlagsFromNothing();
     } else {
         while ($importurlhistory->isAnyChangeFlagsUnknown() && ($lastHistoryData = $stat->fetch())) {
             $lastHistory = new ImportURLHistoryModel();
             $lastHistory->setFromDataBaseRow($lastHistoryData);
             $importurlhistory->setChangedFlagsFromLast($lastHistory);
         }
     }
     // Save back to DB
     $sqlFields = array();
     $sqlParams = array('id' => $importurlhistory->getId(), 'created_at' => $importurlhistory->getCreatedAt()->format("Y-m-d H:i:s"), 'is_new' => $importurlhistory->getIsNew() ? 1 : 0);
     if ($importurlhistory->getTitleChangedKnown()) {
         $sqlFields[] = " title_changed = :title_changed ";
         $sqlParams['title_changed'] = $importurlhistory->getTitleChanged() ? 1 : -1;
     }
     if ($importurlhistory->getAreaIdChangedKnown()) {
         $sqlFields[] = " area_id_changed = :area_id_changed ";
         $sqlParams['area_id_changed'] = $importurlhistory->getAreaIdChanged() ? 1 : -1;
     }
     if ($importurlhistory->getCountryIdChangedKnown()) {
         $sqlFields[] = " country_id_changed = :country_id_changed ";
         $sqlParams['country_id_changed'] = $importurlhistory->getCountryIdChanged() ? 1 : -1;
     }
     if ($importurlhistory->getIsEnabledChangedKnown()) {
         $sqlFields[] = " is_enabled_changed = :is_enabled_changed ";
         $sqlParams['is_enabled_changed'] = $importurlhistory->getIsEnabledChanged() ? 1 : -1;
     }
     if ($importurlhistory->getExpiredAtChangedKnown()) {
         $sqlFields[] = " expired_at_changed  = :expired_at_changed ";
         $sqlParams['expired_at_changed'] = $importurlhistory->getExpiredAtChanged() ? 1 : -1;
     }
     if ($importurlhistory->getGroupIdChangedKnown()) {
         $sqlFields[] = " group_id_changed = :group_id_changed ";
         $sqlParams['group_id_changed'] = $importurlhistory->getGroupIdChanged() ? 1 : -1;
     }
     if ($importurlhistory->getIsManualEventsCreationChangedKnown()) {
         $sqlFields[] = " is_manual_events_creation_changed = :is_manual_events_creation_changed ";
         $sqlParams['is_manual_events_creation_changed'] = $importurlhistory->getIsManualEventsCreationChanged() ? 1 : -1;
     }
     $statUpdate = $DB->prepare("UPDATE import_url_history SET " . " is_new = :is_new, " . implode(" , ", $sqlFields) . " WHERE import_url_id = :id AND created_at = :created_at");
     $statUpdate->execute($sqlParams);
 }