_prepareTables(); $this->_fillFromApplicationFiles(); $this->_fillFromUserFiles(); return $this; } public function refreshStatuses() { $this->_updateAppFilesStatus(); $this->_updateUserFilesStatus(); $this->_updateRefreshTime(); return $this; } protected function _prepareTables() { foreach ([$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([ '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(['fileName' => $fileName]); } } return $this; } protected function _updateAppFilesStatus() { // check _File records $subSelect = $this->_db->select(); $subSelect->from($this->_getPair('File'), [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), [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), [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( ['value' => date('Y-m-d H:i:s')], ['name = ?' => self::REFRESH_TIME_FIELD] ); return $this; } public function getRefreshTime() { return App_Settings_Obj::get(self::REFRESH_TIME_FIELD); } protected function _getRegistryFileFields() { $result = []; $select = $this->_db->select(); $select->from($this->_getPair(), ['tableAlias', 'fieldName']); $list = $this->_db->fetchAll($select); foreach ($list as $row) { isset($result[$row['tableAlias']]) or $result[$row['tableAlias']] = []; 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 = []; $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 = []; foreach ($fields as $name) { $parts[] = "{$name} != ''"; } return implode(' OR ', $parts); } }