/** * Return the array with the columns that could be exported * * @return array */ public function getColumns() { $aColumns = $this->_model->getColumns(); $relatedColumns = $this->_getRelatedColumns(); $availableColumns = array(); foreach ($aColumns as $name => $column) { $availableColumns[] = array('id' => $name, 'name' => $name, 'label' => $this->_model->getAttributeLabel($name)); } $availableColumns = array_merge($availableColumns, array_values($relatedColumns)); // Get the columnModel columns and the columns that are added through the formatColumns function. $cm = $this->getColumnModel(); $cm = $this->formatColumns($cm); $cmCols = $cm->getColumns(); $cmColumns = array(); foreach ($cmCols as $cmCol) { $cmColumns[] = array('id' => $cmCol->getDataIndex(), 'name' => $cmCol->getDataIndex(), 'label' => $cmCol->getLabel()); } $availableColumns = array_merge($availableColumns, array_values($cmColumns)); // Remove columns that are not exportable foreach ($this->notExportableColumns as $notExp) { foreach ($availableColumns as $key => $ac) { if (isset($ac['id']) && $ac['id'] === $notExp) { unset($availableColumns[$key]); } } } sort($availableColumns); return $availableColumns; }
/** * Returns the validation rules of the model. * @return array validation rules */ public function validate() { if (empty($this->name)) { $this->setValidationError('name', sprintf(\GO::t('attributeRequired'), 'name')); } if (empty($this->email)) { $this->setValidationError('email', sprintf(\GO::t('attributeRequired'), 'email')); } if (empty($this->message)) { $this->setValidationError('message', sprintf(\GO::t('attributeRequired'), 'message')); } if (!\GO\Base\Util\Validate::email($this->email)) { $this->setValidationError('email', \GO::t('invalidEmailError')); } return parent::validate(); }
/** * Generates input name for a model attribute. * Note, the attribute name may be modified after calling this method if the name * contains square brackets (mainly used in tabular input) before the real attribute name. * @param \GO\Base\Model $model the data model * @param string $attribute the attribute * @return string the input name */ private function _resolveName($model, $attribute) { if (($pos = strpos($attribute, '[')) !== false) { if ($pos !== 0) { // e.g. name[a][b] return $model->getModelName() . '[' . substr($attribute, 0, $pos) . ']' . substr($attribute, $pos); } if (($pos = strrpos($attribute, ']')) !== false && $pos !== strlen($attribute) - 1) { $sub = substr($attribute, 0, $pos + 1); $attribute = substr($attribute, $pos + 1); return $model->getModelName() . $sub . '[' . $attribute . ']'; } if (preg_match('/\\](\\w+\\[.*)$/', $attribute, $matches)) { $name = $model->getModelName() . '[' . str_replace(']', '][', trim(strtr($attribute, array('][' => ']', '[' => ']')), ']')) . ']'; $attribute = $matches[1]; return $name; } } return $model->getModelName() . '[' . $attribute . ']'; }
/** * Sets the named attribute value. It can also set BELONGS_TO and HAS_ONE * relations if you pass a ActiveRecord * * You may also use $this->AttributeName to set the attribute value. * * @param string $name the attribute name * @param mixed $value the attribute value. * @return boolean whether the attribute exists and the assignment is conducted successfully * @see hasAttribute */ public function setAttribute($name, $value, $format = false) { if ($this->loadingFromDatabase) { //skip fancy features when loading from the database. $this->_attributes[$name] = $value; return true; } if ($format) { $value = $this->formatInput($name, $value); } if (isset($this->columns[$name])) { if (GO::config()->debug) { if ($this->columns[$name]['gotype'] != 'file' && is_object($value) || is_array($value)) { throw new \Exception($this->className() . "::setAttribute : Invalid attribute value for " . $name . ". Type was: " . gettype($value)); } } $relationFieldName = $this->_getAclFk(); if ($name === $relationFieldName) { $aclWasOverwritten = $this->isAclOverwritten(); } //normalize CRLF to prevent issues with exporting to vcard etc. if (isset($this->columns[$name]['gotype']) && ($this->columns[$name]['gotype'] == 'textfield' || $this->columns[$name]['gotype'] == 'textarea')) { $value = \GO\Base\Util\String::normalizeCrlf($value, "\n"); } if ((!isset($this->_attributes[$name]) || (string) $this->_attributes[$name] !== (string) $value) && !$this->isModified($name)) { $this->_modifiedAttributes[$name] = isset($this->_attributes[$name]) ? $this->_attributes[$name] : false; // GO::debug("Setting modified attribute $name to ".$this->_modifiedAttributes[$name]); // GO::debugCalledFrom(5); } $this->_attributes[$name] = $value; // Set the ACL_ID if the relation acl FK changed and ACL is overwritten if ($name === $relationFieldName && !$aclWasOverwritten && $this->aclOverwrite() && $this->isModified($name)) { if (!empty($this->{$name})) { $modelWithAcl = $this->findRelatedAclModel(); if ($modelWithAcl) { $this->{$this->aclOverwrite()} = $modelWithAcl->findAclId(); } } } } else { if ($r = $this->getRelation($name)) { if ($r['type'] == self::BELONGS_TO || $r['type'] == self::HAS_ONE) { if ($value instanceof ActiveRecord) { $cacheKey = $this->_getRelatedCacheKey($r); $this->_relatedCache[$cacheKey] = $value; } else { throw new \Exception("Value for relation '" . $name . "' must be a ActiveRecord '" . gettype($value) . "' was given"); } } else { throw new \Exception("Can't set one to many relation!"); } } else { parent::__set($name, $value); } } return true; }
/** * Returns the static model of the specified AR class. * Every child of this class must override it. * * @return MessageAttachment the static model class */ public static function model($className = __CLASS__) { return parent::model($className); }
/** * * @param \GO\Base\Model $model * @return array formatted grid row key value array */ public function formatModel($model) { $oldLevel = error_reporting(E_ERROR); //suppress errors in the eval'd code $formattedRecord = array(); if ($model instanceof \GO\Base\Db\ActiveRecord) { $formattedRecord = $model->getAttributes($this->_modelFormatType); } $columns = $this->getColumns(); foreach ($columns as $column) { $column->setModelFormatType($this->_modelFormatType); $formattedRecord[$column->getDataIndex()] = $column->render($model); } error_reporting($oldLevel); if (isset($this->_formatRecordFunction)) { $formattedRecord = call_user_func($this->_formatRecordFunction, $formattedRecord, $model, $this); if (!$formattedRecord) { if (is_array($this->_formatRecordFunction)) { $str = $this->_formatRecordFunction[1]; } else { $str = $this->_formatRecordFunction; } throw new \Exception("Fatal error: {$str} should return the record"); } } return $formattedRecord; }
public function __get($name) { return isset($this->_attributes[$name]) ? $this->_attributes[$name] : parent::__get($name); }
/** * Clears the: * * 1. \GO::config()->cachedir folder. This folder contains mainly cached javascripts. * 2. \GO\Base\Model objects cached in memory for a single script run * 3. The permanent cache stored in \GO::cache() * */ public static function clearCache() { \GO::config()->getCacheFolder(false)->delete(); \GO::cache()->flush(); \GO\Base\Model::clearCache(); }