_suMode) { /** @var $doc App_Doc_Admin */ $doc = Zend_Registry::get('doc'); $this->_suMode = (bool) $doc->getAuth()->getSuMode(); } return $this->_suMode; } public function setIdCategory($id) { $this->_idCategory = (int) $id; return $this; } public function getAdminSectionEmails() { if (null === $this->_adminSectionEmails) { $select = $this->_db->select(); $select->from($this->_getPair('Admin'), array('email')); $select->where('`Admin`.`email` IS NOT NULL'); $this->_adminSectionEmails = $this->_db->fetchCol($select); $this->_adminSectionEmails = array_unique($this->_adminSectionEmails); sort($this->_adminSectionEmails); } return $this->_adminSectionEmails; } public function getPrimaryKey() { return $this->_idCategory; } public function getPrimaryKeyArray() { $key = array(); if (null !== $this->_idCategory) { $key['idCategory'] = $this->_idCategory; } return $key; } protected function _getFromDb($key = null, $changed = false) { $mainField = $changed ? 'changed' : 'value'; $mainField4Select = $mainField; if (!$changed && $this->_language && $this->_hasTranslation) { $mainField4Select = $this->_getFieldForSelect($mainField); } $select = $this->getListSelect() ->reset(Zend_Db_Select::COLUMNS) ->columns(array('name', $mainField4Select, 'fieldType', 'options'), $this->_tableAlias); if (null != $key) { $select->where("`{$this->_tableAlias}`.`name` = ?", $key); $row = $this->_db->fetchRow($select); if (!empty($row)) { $this->_arrayParseTag2Url($row); if ('select' == $row['fieldType'] && !$changed) { $this->_prepareSelectValue($row['value'], $row['options']); } return array($row['name'] => $row[$mainField]); } } else { $rows = $this->_db->fetchAll($select); if (!empty($rows)) { $this->_prepareList($rows); $values = array(); foreach ($rows as $row) { if ('select' == $row['fieldType']) { $this->_prepareSelectValue($row['value'], $row['options']); } $values[$row['name']] = $row[$mainField]; } return $values; } } return false; } protected function _getFieldForSelect($field) { return "IFNULL(NULLIF(`ct`.`{$field}`, ''), `{$this->_tableAlias}`.`{$field}`) AS {$field}"; } protected function _joinTranslation(Zend_Db_Select $select) { if ($this->_hasTranslation) { $select->joinLeft( Qs_Db::getPair($this->_getContentTable(), 'ct'), $this->_db->quoteInto("`ct`.`name` = `$this->_tableAlias`.`name` AND `ct`.`lang` = ?", $this->getLanguage()), null ); } return $select; } public function getData($field = null) { if (null === $this->_data && $data = $this->_getFromDb()) { $this->_data = $data; } return Qs_Array::get($this->_data, $field); } public function getChangedData($field = null) { if (null === $this->_changed && $changed = $this->_getFromDb(null, true)) { $this->_changed = $changed; } return Qs_Array::get($this->_changed, $field); } public function getData4Form() { $data = $this->getData(); if ($data && $this->_hasTranslation && $this->_contentFields) { foreach ($this->_contentFields as $element) { if (isset($data[$element])) { $data[$element] = $this->_prepareFieldData4Form($data[$element], $element); } } } return $data; } protected function _prepareFieldData4Form($defaultData, $element) { $defaultLanguage = Qs_Db_Language::getDefault(); $languageList = Qs_Db_Language::getList(); $contentData = $this->_getFieldContentTableData($element); $data = array(); foreach ($languageList as $language) { $languageName = $language['name']; $data[$element . '_' . $languageName] = array_key_exists($languageName, $contentData) ? $this->_tag2BaseUrl($contentData[$languageName][$this->_contentField]) : ($defaultLanguage == $languageName ? $defaultData : ''); } return $data; } protected function _getFieldContentTableData($element) { $select = $this->_db->select(); $select->from($this->_getPair($this->_getContentTable(), 'ct'), array('lang', '*')); $select->where('`ct`.`name` = ?', $element); return $this->_db->fetchAssoc($select); } protected function _prepareSelectValue(&$value, $options) { if (!empty($options)) { $options = $this->parseSelectOptions($options); if ($options['multiple']) { $value = explode("\n", $value); array_walk($value, array($this, '_trimOptions')); } } return $this; } public function parseSelectOptions($options) { $options = trim($options); $options = explode("\n", $options); list($size, $multiple) = explode('|', $options[0]); unset($options[0]); array_walk($options, array($this, '_trimOptions')); $options = array_combine($options, $options); $size = (int)$size; $size = (!$size) ? 1 : $size; $multiple = (int)$multiple; return array('size' => $size, 'multiple' => (bool) $multiple, 'options' => $options); } protected function _trimOptions(&$value, $key) { $value = trim($value); } public function getListSelect() { if (null !== $this->_select) { return $this->_select; } $select = parent::getListSelect(); $select->join( $this->_getSubPair('Category', 'sc'), '`sc`.`id` = ' . $this->_quoteField('idCategory'), array() ); $select->order("{$this->_tableAlias}.sorter"); return $select; } protected function _filter(Zend_Db_Select $select) { parent::_filter($select); if ($this->_idCategory) { $where = "`{$this->_tableAlias}`.`idCategory` = ?"; if (!$this->_getSuMode()) { $where .= " AND `{$this->_tableAlias}`.`system` = 'n'" . " AND `sc`.system = 'n'"; } $select->where($where, $this->_idCategory); } return $this; } public function update(array $data = null) { if (null === $data) { $data = $this->_data; } $fields = $this->_getFromDb(); $fields = ($fields && is_array($fields)) ? $fields : []; $defaultLanguage = Qs_Db_Language::getDefault(); $languageList = Qs_Db_Language::getList(); foreach (array_keys($fields) as $field) { if (!array_key_exists($field, $data)) { continue; } if (!in_array($field, $this->_contentFields)) { $this->_updateField($field, $data[$field]); continue; } foreach ($languageList as $language) { $langName = $language['name']; if (array_key_exists($field . '_' . $langName, $data[$field])) { if ($langName == $defaultLanguage) { $this->_updateField($field, $data[$field][$field . '_' . $defaultLanguage]); } else { $this->_updateTranslatedField($field, $langName, $data[$field][$field . '_' . $langName]); } } } } self::_initSettings(); return $this; } protected function _prepareFieldValue($value) { if (is_array($value)) { $value = implode("\n", $value); } return $this->_baseUrl2Tag($value); } protected function _updateField($field, $value) { $value = $this->_prepareFieldValue($value); $this->_getTable()->updateByKey(array('value' => $value), $field); return $this; } protected function _updateTranslatedField($field, $lang, $value) { $value = $this->_prepareFieldValue($value); $contentTableAlias = $this->_getContentTable(); $where = $this->_db->quoteInto('`name` = ?', $field) . ' AND ' . $this->_db->quoteInto('`lang` = ?', $lang); if (($row = $this->_getTable($contentTableAlias)->fetchRow($where))) { $row->setFromArray(['value' => $value]); $row->save(); } else { $this->_getTable($contentTableAlias)->insert([ 'name' => $field, 'lang' => $lang, 'value' => $value, ]); } return $this; } public function getCategories($force = false) { if ($force || $this->_getSuMode()) { self::$_categoriesList = array(); } if (empty(self::$_categoriesList)) { $where = '1'; if (!$this->_getSuMode()) { $where .= " AND `sc`.`system` = 'n'"; } $select = $this->_db->select(); $select->from($this->_getSubPair('Category', 'sc'), array('id', 'name')) ->where($where) ->order('sc.sorter'); self::$_categoriesList = $this->_db->fetchPairs($select); } return self::$_categoriesList; } public static function getAdminEmails() { return self::get('adminEmail', 'parseEmails'); } public static function getEmailFrom($fieldName) { $result = self::get($fieldName); if (empty($result)) { $result = self::get('siteFrom'); } return $result; } public static function getFormEmails($fieldName) { $emails = self::getAdminEmails(); $formEmails = self::get($fieldName, 'parseEmails'); if (is_array($formEmails) && !empty($formEmails)) { $emails = array_merge($emails, $formEmails); $emails = array_unique($emails); } return $emails; } public static function get($name, $action = null, $lang = null) { self::_initSettings($lang ? $lang : CURRENT_LANG); if (array_key_exists($name, self::$_settings)) { $value = self::$_settings[$name]; if (null !== $action && is_callable("App_Settings_Obj::{$action}")) { $value = App_Settings_Obj::$action($value); } return self::_tag2BaseUrl($value); } return false; } public static function set($field, $value) { self::getInstance()->updateFieldValue($field, $value); } public function updateFieldValue($field, $value) { $this->_getTable()->updateByKey(compact('value'), $field); return $this; } public static function getChanged($name) { self::_initSettings(); if (array_key_exists($name, self::$_settingsChanged)) { return self::$_settingsChanged[$name]; } return false; } public static function parseEmails($value) { $emails = array(); $value = preg_split("/[ ;,\n\r\t]+/", $value); if (!empty($value)) { $validator = new Zend_Validate_EmailAddress(); foreach ($value as $email) { $email = trim($email); if ($validator->isValid($email)) { $emails[] = $email; } } } $emails = array_unique($emails); return $emails; } protected static function _initSettings($language = null) { $instance = self::getInstance(); if ($language && $language !== $instance->getLanguage()) { $instance->setLanguage($language); $instance->_data = null; self::$_settings = null; } if (null === self::$_settings) { self::$_settings = $instance->getData(); } if (null === self::$_settingsChanged) { self::$_settingsChanged = $instance->getChangedData(); } } public function getFieldOptions($name) { $select = $this->_db->select(); $select->from($this->_getPair('SettingsOptions')); $select->where('`name` = ?', $name); $select->order('sorter'); $list = $this->_db->fetchAll($select); $options = array(); foreach ($list as $option) { switch ($option['type']) { case 'bool': $value = (bool) $option['value']; break; case 'int': $value = (int) $option['value']; break; case 'float': $value = (float) $option['value']; break; case 'double': $value = (double) $option['value']; break; default: $value = $option['value']; break; } $options[$option['variable']] = $value; } $options = Qs_Array::expand($options); if (!empty($options['validators'])) { $validators = array(); foreach ($options['validators'] as $validator) { $validators[] = array('validator' => $validator, 'breakChainOnFailure' => true); } $options['validators'] = $validators; } return $options; } public function getObjectId() { return $this->_idCategory; } protected function _getContentFields() { return $this->_hasTranslation ? $this->_contentFields : false; } }