_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'), ['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 = []; if (null !== $this->_idCategory) { $key['idCategory'] = $this->_idCategory; } return $key; } protected function _getFromDb($key = null, $field = null) { $select = $this->getListSelect() ->reset(Zend_Db_Select::COLUMNS) ->columns(['name', 'changed', 'value', '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']) { $this->_prepareSelectValue($row['value'], $row['options']); } return [ 'values' => [ $row['name'] => $row['value'], ], 'changed' => [ $row['name'] => $row['changed'], ], ]; } } else { $rows = $this->_db->fetchAll($select); if (!empty($rows)) { $this->_prepareList($rows); $values = []; $changed = []; foreach ($rows as $row) { if ('select' == $row['fieldType']) { $this->_prepareSelectValue($row['value'], $row['options']); } $values[$row['name']] = $row['value']; $changed[$row['name']] = $row['changed']; } return ['values' => $values, 'changed' => $changed]; } } return false; } public function getData($field = null, $changed = false) { if (null === $this->_data && $data = $this->_getFromDb()) { $this->_data = $data['values']; $this->_changed = $data['changed']; } if (null === $this->_data) { return null; } if ($changed == true) { return Qs_Array::get($this->_changed, $field); } return Qs_Array::get($this->_data, $field); } protected function _prepareSelectValue(&$value, $options) { if (!empty($options)) { $options = $this->parseSelectOptions($options); if ($options['multiple']) { $value = explode("\n", $value); array_walk($value, [$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, [$this, '_trimOptions']); $options = array_combine($options, $options); $size = (int) $size; $size = (!$size) ? 1 : $size; $multiple = (int) $multiple; return ['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'), [] ); $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()['values']; if (is_array($fields)) { foreach (array_keys($fields) as $field) { if (array_key_exists($field, $data)) { if (is_array($data[$field])) { $data[$field] = implode("\n", $data[$field]); } $data[$field] = $this->_baseUrl2Tag($data[$field]); $this->_getTable()->updateByKey(['value' => $data[$field]], $field); } } } self::_initSettings(); return $this; } public function getCategories($force = false) { if ($force || $this->_getSuMode()) { self::$_categoriesList = []; } if (empty(self::$_categoriesList)) { $where = '1'; if (!$this->_getSuMode()) { $where .= " AND `sc`.`system` = 'n'"; } $select = $this->_db->select(); $select->from($this->_getSubPair('Category', 'sc'), ['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) { self::_initSettings(); 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 = []; $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() { if (null === self::$_settings) { self::$_settings = self::getInstance()->getData(); } if (null === self::$_settingsChanged) { self::$_settingsChanged = self::getInstance()->getData(null, true); } } 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 = []; 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 = []; foreach ($options['validators'] as $validator) { $validators[] = ['validator' => $validator, 'breakChainOnFailure' => true]; } $options['validators'] = $validators; } return $options; } public function getObjectId() { return $this->_idCategory; } }