/** * Defines the column name for use with single table inheritance. Can be overridden in subclasses. */ public function setInheritanceColumn($column_name) { if (!$this->_ActiveRecord->hasColumn($column_name)) { trigger_error(Ak::t('Could not set "%column_name" as the inheritance column as this column is not available on the database.', array('%column_name' => $column_name)) . ' ' . AkDebug::getFileAndNumberTextForError(1), E_USER_NOTICE); return false; } elseif ($this->_ActiveRecord->getColumnType($column_name) != 'string') { trigger_error(Ak::t('Could not set %column_name as the inheritance column as this column type is "%column_type" instead of "string".', array('%column_name' => $column_name, '%column_type' => $this->_ActiveRecord->getColumnType($column_name))) . ' ' . AkDebug::getFileAndNumberTextForError(1), E_USER_NOTICE); return false; } else { $this->_ActiveRecord->_inheritanceColumn = $column_name; return true; } }
public function init($options = array()) { $default_options = array('servers' => array('localhost:11211'), 'lifeTime' => 0); $options = array_merge($default_options, $options); $this->_lifeTime = $options['lifeTime']; if (empty($options['servers'])) { trigger_error('Need to provide at least 1 server', E_USER_ERROR); return false; } $this->_memcache = new MemCachedClient(is_array($options['servers']) ? $options['servers'] : array($options['servers'])); $ping = $this->_memcache->get('ping'); if (!$ping) { if ($this->_memcache->errno == ERR_NO_SOCKET) { if (empty($options['silent_mode'])) { trigger_error("Could not connect to MemCache daemon. " . AkDebug::getFileAndNumberTextForError(1), E_USER_WARNING); } return false; } $this->_memcache->set('ping', 1); } return true; }
protected function _validateCalculationOptions($options = array()) { $invalid_options = array_diff(array_keys($options), $this->_calculation_options); if (!empty($invalid_options)) { trigger_error(Ak::t('%options are not valid calculation options.', array('%options' => join(', ', $invalid_options))) . AkDebug::getFileAndNumberTextForError(1), E_USER_ERROR); } }
/** * Finder methods must instantiate through this method to work with the single-table inheritance model and * eager loading associations. * That makes it possible to create objects of different types from the same table. */ public function &instantiate($record, $set_as_new = true, $call_after_instantiate = true) { $inheritance_column = $this->_ActiveRecord->getInheritanceColumn(); if (!empty($record[$inheritance_column])) { $inheritance_column = $record[$inheritance_column]; $inheritance_model_name = AkInflector::camelize($inheritance_column); @(require_once AkInflector::toModelFilename($inheritance_model_name)); if (!class_exists($inheritance_model_name)) { trigger_error($this->_ActiveRecord->t("The single-table inheritance mechanism failed to locate the subclass: '%class_name'. " . "This error is raised because the column '%column' is reserved for storing the class in case of inheritance. " . "Please rename this column if you didn't intend it to be used for storing the inheritance class " . "or overwrite #{self.to_s}.inheritance_column to use another column for that information.", array('%class_name' => $inheritance_model_name, '%column' => $this->_ActiveRecord->getInheritanceColumn())) . AkDebug::getFileAndNumberTextForError(1), E_USER_ERROR); } } $model_name = isset($inheritance_model_name) ? $inheritance_model_name : $this->_ActiveRecord->getModelName(); $object = new $model_name(array('init' => false)); $object->_newRecord = $set_as_new; $object->setConnection($this->_ActiveRecord->getConnection()); $object->beforeInstantiate($record); $object->init(array('attributes', $record)); if ($call_after_instantiate) { $object->afterInstantiate(); $object->notifyObservers('afterInstantiate'); } return $object; }
public function &_getActAsInstance($class_name, $options) { if (!class_exists($class_name)) { if (substr($class_name, 0, 2) == 'Ak') { include_once AK_ACTIVE_RECORD_DIR . DS . 'behaviours' . DS . AkInflector::underscore(substr($class_name, 2)) . '.php'; } else { include_once AK_APP_PLUGINS_DIR . DS . AkInflector::underscore($class_name) . DS . 'lib' . DS . $class_name . '.php'; } } if (!class_exists($class_name)) { trigger_error(Ak::t('The class %class used for handling an "act_as %class" does not exist', array('%class' => $class_name)) . AkDebug::getFileAndNumberTextForError(1), E_USER_ERROR); $false = false; return $false; } else { $ActAsInstance = new $class_name($this, $options); return $ActAsInstance; } }
public function writeCache($config, $namespace, $environment = AK_ENVIRONMENT, $force = false) { if (!$force && !$this->_useWriteCache($environment)) { return false; } $key = $this->_getCacheKey($namespace, $environment); Ak::setStaticVar($key, $config); $var_export = var_export($config, true); $cache = <<<CACHE <?php /** * Auto-generated config cache from {$namespace} in environment {$environment} */ \$config = {$var_export}; return \$config; CACHE; $cache_file_name = $this->getCacheFileName($namespace, $environment); if (!AkFileSystem::file_put_contents($cache_file_name, $cache, array('base_path' => AkConfig::getCacheBasePath()))) { trigger_error(Ak::t('Could not create config cache file %file', array('%file' => $cache_file_name)) . ' ' . AkDebug::getFileAndNumberTextForError(1), E_USER_ERROR); return false; } else { return true; } }
public function execute($sql, $message = 'SQL') { if (is_array($sql)) { $sql_string = array_shift($sql); $bindings = $sql; } else { $sql_string = $sql; } $this->_log($message . ': ' . $sql_string); $result = isset($bindings) ? $this->connection->Execute($sql_string, $bindings) : $this->connection->Execute($sql_string); if (!$result) { $error_message = '[' . $this->connection->ErrorNo() . '] ' . $this->connection->ErrorMsg(); $this->_log('SQL Error: ' . $error_message); if ($this->debug || AK_DEBUG) { trigger_error("Tried '{$sql_string}'. Got: {$error_message}." . AkDebug::getFileAndNumberTextForError(4), E_USER_NOTICE); } } return $result; }
public function addCombinedAttributeConfiguration($attribute) { $args = is_array($attribute) ? $attribute : func_get_args(); $columns = array_slice($args, 2); $invalid_columns = array(); foreach ($columns as $colum) { if (!$this->_ActiveRecord->hasAttribute($colum)) { $invalid_columns[] = $colum; } } if (!empty($invalid_columns)) { trigger_error(Ak::t('There was an error while setting the composed field "%field_name", the following mapping column/s "%columns" do not exist', array('%field_name' => $args[0], '%columns' => join(', ', $invalid_columns))) . AkDebug::getFileAndNumberTextForError(1), E_USER_ERROR); } else { $attribute = array_shift($args); $this->_ActiveRecord->_combinedAttributes[$attribute] = $args; $this->composeCombinedAttribute($attribute); } }
/** * @deprecated * @uses AkDebug::getFileAndNumberTextForError */ static function getFileAndNumberTextForError($levels = 0) { Ak::deprecateMethod(__METHOD__, 'AkDebug::getFileAndNumberTextForError()'); return AkDebug::getFileAndNumberTextForError($levels); }
/** * Validates that the specified attribute matches the length restrictions supplied. Only one option can be used at a time: * * class Person extends ActiveRecord * { * public function validate() * { * $this->validatesLengthOf('first_name', array('maximum'=>30)); * $this->validatesLengthOf('last_name', array('maximum'=>30,'message'=> "less than %d if you don't mind")); * $this->validatesLengthOf('last_name', array('within'=>array(7, 32))); * $this->validatesLengthOf('last_name', array('in'=>array(6, 20), 'too_long' => "pick a shorter name", 'too_short' => "pick a longer name")); * $this->validatesLengthOf('fav_bra_size', array('minimum'=>1, 'too_short'=>"please enter at least %d character")); * $this->validatesLengthOf('smurf_leader', array('is'=>4, 'message'=>"papa is spelled with %d characters... don't play me.")); * } * } * * NOTE: Be aware that $this->validatesLengthOf('field', array('is'=>5)); Will match a string containing 5 characters (Ie. "Spain"), an integer 5, and an array with 5 elements. You must supply additional checking to check for appropriate types. * * Configuration options: * <tt>minimum</tt> - The minimum size of the attribute * <tt>maximum</tt> - The maximum size of the attribute * <tt>is</tt> - The exact size of the attribute * <tt>within</tt> - A range specifying the minimum and maximum size of the attribute * <tt>in</tt> - A synonym(or alias) for :within * <tt>allow_null</tt> - Attribute may be null; skip validation. * * <tt>too_long</tt> - The error message if the attribute goes over the maximum (default "is" "is too long (max is %d characters)") * <tt>too_short</tt> - The error message if the attribute goes under the minimum (default "is" "is too short (min is %d characters)") * <tt>wrong_length</tt> - The error message if using the "is" method and the attribute is the wrong size (default "is" "is the wrong length (should be %d characters)") * <tt>message</tt> - The error message to use for a "minimum", "maximum", or "is" violation. An alias of the appropriate too_long/too_short/wrong_length message */ public function validatesLengthOf($attribute_names, $options = array()) { // Merge given options with defaults. $default_options = array('too_long' => $this->_Model->getDefaultErrorMessageFor('too_long'), 'too_short' => $this->_Model->getDefaultErrorMessageFor('too_short'), 'wrong_length' => $this->_Model->getDefaultErrorMessageFor('wrong_length'), 'allow_null' => false); $range_options = array(); foreach ($options as $k => $v) { if (in_array($k, array('minimum', 'maximum', 'is', 'in', 'within'))) { $range_options[$k] = $v; $option = $k; $option_value = $v; } } // Ensure that one and only one range option is specified. switch (count($range_options)) { case 0: trigger_error(Ak::t('Range unspecified. Specify the "within", "maximum", "minimum, or "is" option.') . AkDebug::getFileAndNumberTextForError(1), E_USER_ERROR); return false; break; case 1: $options = array_merge($default_options, $options); break; default: trigger_error(Ak::t('Too many range options specified. Choose only one.') . AkDebug::getFileAndNumberTextForError(1), E_USER_ERROR); return false; break; } switch ($option) { case 'within': case 'in': if (empty($option_value) || !is_array($option_value) || count($option_value) != 2 || !is_numeric($option_value[0]) || !is_numeric($option_value[1])) { trigger_error(Ak::t('%option must be a Range (array(min, max))', array('%option', $option)) . AkDebug::getFileAndNumberTextForError(1), E_USER_ERROR); return false; } $attribute_names = Ak::toArray($attribute_names); foreach ($attribute_names as $attribute_name) { $value = @$this->_Model->{$attribute_name}; if (!empty($option['allow_null']) && is_null($value) || Ak::size($value) < $option_value[0]) { $this->_Model->addError($attribute_name, sprintf($options['too_short'], $option_value[0])); } elseif (!empty($option['allow_null']) && is_null($value) || Ak::size($value) > $option_value[1]) { $this->_Model->addError($attribute_name, sprintf($options['too_long'], $option_value[1])); } } break; case 'is': case 'minimum': case 'maximum': if (empty($option_value) || !is_numeric($option_value) || $option_value <= 0) { trigger_error(Ak::t('%option must be a nonnegative Integer', array('%option', $option_value)) . AkDebug::getFileAndNumberTextForError(1), E_USER_ERROR); return false; } // Declare different validations per option. $validity_checks = array('is' => '==', 'minimum' => '>=', 'maximum' => '<='); $message_options = array('is' => 'wrong_length', 'minimum' => 'too_short', 'maximum' => 'too_long'); $message = sprintf(!empty($options['message']) ? $options['message'] : $options[$message_options[$option]], $option_value); $attribute_names = Ak::toArray($attribute_names); foreach ($attribute_names as $attribute_name) { $value = @$this->_Model->{$attribute_name}; if (!$options['allow_null'] && is_null($value) || eval('return !(' . Ak::size($value) . " {$validity_checks[$option]} {$option_value});")) { $this->_Model->addError($attribute_name, $message); } } break; default: break; } return true; }