_prepareTables(); $this->_fillFromApplicationFiles(); $this->_fillFromUserFiles(); return $this; } public function refreshStatuses() { $this->_updateAppFilesStatus(); $this->_updateUserFilesStatus(); $this->_updateRefreshTime(); return $this; } protected function _prepareTables() { foreach (array($this->_appFileTableAlias, $this->_userFileTableAlias) as $tableAlias) { $tableName = $this->_db->quoteIdentifier($this->_getTableName($tableAlias)); $sql = "TRUNCATE TABLE {$tableName};"; $this->_db->query($sql); } return $this; } protected function _fillFromApplicationFiles() { $table = $this->_getTable($this->_appFileTableAlias); $checkTables = $this->_getRegistryFileFields(); foreach ($checkTables as $tableAlias => $fields) { $list = $this->_getFieldsData($tableAlias, $fields); foreach ($list as $row) { $primaryKey = $row['_PRIMARY_']; unset($row['_PRIMARY_']); foreach ($row as $fieldName => $fieldValue) { if (null !== $fieldValue && '' !== $fieldValue) { $table->insert(array( 'tableAlias' => $tableAlias, 'fieldName' => $fieldName, 'primaryKey' => $primaryKey, 'fileName' => $fieldValue )); } } } } return $this; } protected function _fillFromUserFiles() { $excludedFiles = $this->getConfig('excludedUserFiles')->toArray(); $table = $this->_getTable($this->_userFileTableAlias); $iterator = new DirectoryIterator(WWW_PATH . '/' . Qs_ImageFs::WEB_PATH); /** @var $fileInfo SplFileInfo */ foreach ($iterator as $fileInfo) { $fileName = $fileInfo->getFilename(); if ($fileInfo->isFile() && !in_array($fileName, $excludedFiles, true)) { $table->insert(array('fileName' => $fileName)); } } return $this; } protected function _updateAppFilesStatus() { // check _File records $subSelect = $this->_db->select(); $subSelect->from($this->_getPair('File'), array(new Zend_Db_Expr('1'))); $subSelect->where("`File`.`nameFs` = `{$this->_appFileTableAlias}`.`fileName`"); $subSelect->limit(1); $sql = 'UPDATE ' . $this->_getQuotedTableName($this->_appFileTableAlias) . ' AS `' . $this->_appFileTableAlias . '` ' . 'SET `fileRecordExists` = "y" ' . 'WHERE EXISTS (' . $subSelect . ')'; $this->_db->query($sql); // check user files $subSelect = $this->_db->select(); $subSelect->from($this->_getPair($this->_userFileTableAlias), array(new Zend_Db_Expr('1'))); $subSelect->where("`{$this->_userFileTableAlias}`.`fileName` = `{$this->_appFileTableAlias}`.`fileName`"); $subSelect->limit(1); $sql = 'UPDATE ' . $this->_getQuotedTableName($this->_appFileTableAlias) . ' AS `' . $this->_appFileTableAlias . '` ' . 'SET `userFileExists` = "y" ' . 'WHERE EXISTS (' . $subSelect . ')'; $this->_db->query($sql); return $this; } protected function _updateUserFilesStatus() { // check application files $subSelect = $this->_db->select(); $subSelect->from($this->_getPair($this->_appFileTableAlias), array(new Zend_Db_Expr('1'))); $subSelect->where("`{$this->_appFileTableAlias}`.`fileName` = `{$this->_userFileTableAlias}`.`fileName`"); $subSelect->limit(1); $sql = 'UPDATE ' . $this->_getQuotedTableName($this->_userFileTableAlias) . ' AS `' . $this->_userFileTableAlias . '` ' . 'SET `appFileExists` = "y" ' . 'WHERE EXISTS (' . $subSelect . ')'; $this->_db->query($sql); return $this; } protected function _updateRefreshTime() { $this->_getTable('Settings')->update( array('value' => date('Y-m-d H:i:s')), array('name = ?' => self::REFRESH_TIME_FIELD) ); return $this; } public function getRefreshTime() { return App_Settings_Obj::get(self::REFRESH_TIME_FIELD); } protected function _getRegistryFileFields() { $result = array(); $select = $this->_db->select(); $select->from($this->_getPair(), array('tableAlias', 'fieldName')); $list = $this->_db->fetchAll($select); foreach ($list as $row) { isset($result[$row['tableAlias']]) or $result[$row['tableAlias']] = array(); if (false === array_search($row['fieldName'], $result[$row['tableAlias']])) { $result[$row['tableAlias']][] = $row['fieldName']; } } return $result; } protected function _getFieldsData($tableAlias, array $fields) { $primary = $this->_getTable($tableAlias)->getPrimary(); $fields = array_merge($primary, $fields); $select = $this->_db->select(); $select->from($this->_getPair($tableAlias), $fields); if (($condition = $this->_getEmptyCheckCondition($fields))) { $select->where($condition); } $result = array(); $primaryRow = array_fill_keys($primary, null); if (($list = $this->_db->fetchAll($select))) { foreach ($list as $row) { $primaryKey = array_intersect_key($row, $primaryRow); $resultRow = array_diff_key($row, $primaryRow); $resultRow['_PRIMARY_'] = serialize($primaryKey); $result[] = $resultRow; } } return $result; } protected function _getEmptyCheckCondition(array $fields) { $parts = array(); foreach ($fields as $name) { $parts[] = "{$name} != ''"; } return implode(' OR ', $parts); } }