_select) { return $this->_select; } parent::getListSelect(); $this->_select->join( $this->_getPair($this->_registryAlias), "`{$this->_registryAlias}`.`tableAlias` = `{$this->_tableAlias}`.`tableAlias` " . "AND `{$this->_registryAlias}`.`fieldName` = `{$this->_tableAlias}`.`fieldName`", array('pageQuery') ); $this->_select->where('`fileRecordExists` != "y" OR `userFileExists` != "y"'); $this->_select->order('fileName'); return $this->_select; } protected function _prepareList(&$list) { foreach ($list as &$value) { $value['primaryKey'] = unserialize($value['primaryKey']); $value['viewUrl'] = ($value['pageQuery']) ? $this->_getItemViewUrl($value, $value['pageQuery']) : null; } return parent::_prepareList($list); } protected function _addDependenciesFromDb(array &$data) { parent::_addDependenciesFromDb($data); $data['primaryKey'] = unserialize($data['primaryKey']); return $this; } protected function _getItemViewUrl($row, $pageQuery) { $viewUrl = null; if (($pageUrl = $this->_getUrlByPageQuery($pageQuery))) { $params = array('action' => 'edit') + $row['primaryKey']; $viewUrl = $pageUrl . '?' . http_build_query($params); } return $viewUrl; } protected function _getUrlByPageQuery($pageQuery) { if (!isset($this->_pageQuery2Url[$pageQuery])) { $this->_pageQuery2Url[$pageQuery] = Qs_SiteMap::find($pageQuery, 'url'); } return $this->_pageQuery2Url[$pageQuery]; } public function fix(array $selectedPrimary) { $table = $this->_getTable(); foreach ($selectedPrimary as $primaryKey) { $id = $primaryKey['id']; if (($data = $this->_getFromDb($id)) && empty($data['status'])) { $status = null; if ('n' === $data['userFileExists']) { $status = $this->_resetApplicationField( $data['tableAlias'], $data['fieldName'], $data['primaryKey'] ); } else if ('n' === $data['fileRecordExists']) { $status = $this->_createFileRecord( $data['tableAlias'], $data['fieldName'], $data['fileName'], $data['primaryKey'] ); } if ($status) { $table->update(array('status' => $status), array('id = ?' => $id)); } } } return true; } protected function _resetApplicationField($tableAlias, $fieldName, $primaryKey) { $affectedRows = $this->_getTable($tableAlias)->update( array($fieldName => ''), Qs_Db::getWhereSql($primaryKey) ); return ($affectedRows) ? self::STATUS_OK : self::STATUS_FAIL; } protected function _createFileRecord($tableAlias, $fieldName, $fileName, $primaryKey) { $fullName = WWW_PATH . '/' . Qs_ImageFs::WEB_PATH . '/' . $fileName; if (($newFileName = $this->_getTransferAdapter()->insertFile($fullName))) { $affectedRows = $this->_getTable($tableAlias)->update( array($fieldName => $newFileName), Qs_Db::getWhereSql($primaryKey) ); return ($affectedRows) ? self::STATUS_OK : self::STATUS_FAIL; } return self::STATUS_FAIL; } /** * @return Qs_File_Transfer_Adapter_Db */ protected function _getTransferAdapter() { if (null === $this->_transferAdapter) { $this->_transferAdapter = new Qs_File_Transfer_Adapter_Db(); } return $this->_transferAdapter; } }