Esempio n. 1
0
 /**
  * Handles all method calls for columns, related records and hook callbacks
  * 
  * Dynamically handles `get`, `set`, `prepare`, `encode` and `inspect`
  * methods for each column in this record. Method names are in the form
  * `verbColumName()`.
  * 
  * This method also handles `associate`, `build`, `count`, `has`, and `link`
  * verbs for records in many-to-many relationships; `build`, `count`, `has`
  * and `populate` verbs for all related records in one-to-many relationships
  * and `create`, `has` and `populate` verbs for all related records in
  * one-to-one relationships, and the `create` verb for all related records
  * in many-to-one relationships.
  * 
  * Method callbacks registered through fORM::registerActiveRecordMethod()
  * will be delegated via this method.
  * 
  * @param  string $method_name  The name of the method called
  * @param  array  $parameters   The parameters passed
  * @return mixed  The value returned by the method called
  */
 public function __call($method_name, $parameters)
 {
     $class = get_class($this);
     if (!isset(self::$callback_cache[$class][$method_name])) {
         if (!isset(self::$callback_cache[$class])) {
             self::$callback_cache[$class] = array();
         }
         $callback = fORM::getActiveRecordMethod($class, $method_name);
         self::$callback_cache[$class][$method_name] = $callback ? $callback : FALSE;
     }
     if ($callback = self::$callback_cache[$class][$method_name]) {
         return call_user_func_array($callback, array($this, &$this->values, &$this->old_values, &$this->related_records, &$this->cache, $method_name, $parameters));
     }
     if (!isset(self::$method_name_cache[$method_name])) {
         list($action, $subject) = fORM::parseMethod($method_name);
         if (in_array($action, array('get', 'encode', 'prepare', 'inspect', 'set'))) {
             $subject = fGrammar::underscorize($subject);
         } else {
             if (in_array($action, array('build', 'count', 'inject', 'link', 'list', 'tally'))) {
                 $subject = fGrammar::singularize($subject);
             }
             $subject = fORM::getRelatedClass($class, $subject);
         }
         self::$method_name_cache[$method_name] = array('action' => $action, 'subject' => $subject);
     } else {
         $action = self::$method_name_cache[$method_name]['action'];
         $subject = self::$method_name_cache[$method_name]['subject'];
     }
     switch ($action) {
         // Value methods
         case 'get':
             return $this->get($subject);
         case 'encode':
             if (isset($parameters[0])) {
                 return $this->encode($subject, $parameters[0]);
             }
             return $this->encode($subject);
         case 'prepare':
             if (isset($parameters[0])) {
                 return $this->prepare($subject, $parameters[0]);
             }
             return $this->prepare($subject);
         case 'inspect':
             if (isset($parameters[0])) {
                 return $this->inspect($subject, $parameters[0]);
             }
             return $this->inspect($subject);
         case 'set':
             if (sizeof($parameters) < 1) {
                 throw new fProgrammerException('The method, %s(), requires at least one parameter', $method_name);
             }
             return $this->set($subject, $parameters[0]);
             // Related data methods
         // Related data methods
         case 'associate':
             if (sizeof($parameters) < 1) {
                 throw new fProgrammerException('The method, %s(), requires at least one parameter', $method_name);
             }
             $records = $parameters[0];
             $route = isset($parameters[1]) ? $parameters[1] : NULL;
             list($subject, $route, $plural) = self::determineSubject($class, $subject, $route);
             if ($plural) {
                 fORMRelated::associateRecords($class, $this->related_records, $subject, $records, $route);
             } else {
                 fORMRelated::associateRecord($class, $this->related_records, $subject, $records, $route);
             }
             return $this;
         case 'build':
             if (isset($parameters[0])) {
                 return fORMRelated::buildRecords($class, $this->values, $this->related_records, $subject, $parameters[0]);
             }
             return fORMRelated::buildRecords($class, $this->values, $this->related_records, $subject);
         case 'count':
             if (isset($parameters[0])) {
                 return fORMRelated::countRecords($class, $this->values, $this->related_records, $subject, $parameters[0]);
             }
             return fORMRelated::countRecords($class, $this->values, $this->related_records, $subject);
         case 'create':
             if (isset($parameters[0])) {
                 return fORMRelated::createRecord($class, $this->values, $this->related_records, $subject, $parameters[0]);
             }
             return fORMRelated::createRecord($class, $this->values, $this->related_records, $subject);
         case 'has':
             $route = isset($parameters[0]) ? $parameters[0] : NULL;
             list($subject, $route, ) = self::determineSubject($class, $subject, $route);
             return fORMRelated::hasRecords($class, $this->values, $this->related_records, $subject, $route);
         case 'inject':
             if (sizeof($parameters) < 1) {
                 throw new fProgrammerException('The method, %s(), requires at least one parameter', $method_name);
             }
             if (isset($parameters[1])) {
                 return fORMRelated::setRecordSet($class, $this->related_records, $subject, $parameters[0], $parameters[1]);
             }
             return fORMRelated::setRecordSet($class, $this->related_records, $subject, $parameters[0]);
         case 'link':
             if (isset($parameters[0])) {
                 fORMRelated::linkRecords($class, $this->related_records, $subject, $parameters[0]);
             } else {
                 fORMRelated::linkRecords($class, $this->related_records, $subject);
             }
             return $this;
         case 'list':
             if (isset($parameters[0])) {
                 return fORMRelated::getPrimaryKeys($class, $this->values, $this->related_records, $subject, $parameters[0]);
             }
             return fORMRelated::getPrimaryKeys($class, $this->values, $this->related_records, $subject);
         case 'populate':
             $route = isset($parameters[0]) ? $parameters[0] : NULL;
             list($subject, $route, ) = self::determineSubject($class, $subject, $route);
             fORMRelated::populateRecords($class, $this->related_records, $subject, $route);
             return $this;
         case 'tally':
             if (sizeof($parameters) < 1) {
                 throw new fProgrammerException('The method, %s(), requires at least one parameter', $method_name);
             }
             if (isset($parameters[1])) {
                 return fORMRelated::setCount($class, $this->related_records, $subject, $parameters[0], $parameters[1]);
             }
             return fORMRelated::setCount($class, $this->related_records, $subject, $parameters[0]);
             // Error handler
         // Error handler
         default:
             throw new fProgrammerException('Unknown method, %s(), called', $method_name);
     }
 }
Esempio n. 2
0
 /**
  * Uploads a file
  * 
  * @internal
  * 
  * @param  fActiveRecord $object            The fActiveRecord instance
  * @param  array         &$values           The current values
  * @param  array         &$old_values       The old values
  * @param  array         &$related_records  Any records related to this record
  * @param  array         &$cache            The cache array for the record
  * @param  string        $method_name       The method that was called
  * @param  array         $parameters        The parameters passed to the method
  * @return fFile  The uploaded file
  */
 public static function upload($object, &$values, &$old_values, &$related_records, &$cache, $method_name, $parameters)
 {
     $class = get_class($object);
     list($action, $subject) = fORM::parseMethod($method_name);
     $column = fGrammar::underscorize($subject);
     $existing_temp_file = FALSE;
     // Try to upload the file putting it in the temp dir incase there is a validation problem with the record
     try {
         $upload_dir = self::$file_upload_columns[$class][$column];
         $temp_dir = self::prepareTempDir($upload_dir);
         if (!fUpload::check($column)) {
             throw new fExpectedException('Please upload a file');
         }
         $uploader = self::setUpFUpload($class, $column);
         $file = $uploader->move($temp_dir, $column);
         // If there was an eror, check to see if we have an existing file
     } catch (fExpectedException $e) {
         // If there is an existing file and none was uploaded, substitute the existing file
         $existing_file = fRequest::get('existing-' . $column);
         $delete_file = fRequest::get('delete-' . $column, 'boolean');
         $no_upload = $e->getMessage() == self::compose('Please upload a file');
         if ($existing_file && $delete_file && $no_upload) {
             $file = NULL;
         } elseif ($existing_file) {
             $file_path = $upload_dir->getPath() . $existing_file;
             $file = fFilesystem::createObject($file_path);
             $current_file = $values[$column];
             // If the existing file is the same as the current file, we can just exit now
             if ($current_file && $current_file instanceof fFile && $file->getPath() == $current_file->getPath()) {
                 return;
             }
             $existing_temp_file = TRUE;
         } else {
             $file = NULL;
         }
     }
     // Assign the file
     fActiveRecord::assign($values, $old_values, $column, $file);
     // Perform the file upload inheritance
     if (!empty(self::$column_inheritence[$class][$column])) {
         foreach (self::$column_inheritence[$class][$column] as $other_column) {
             if ($file) {
                 // Image columns will only inherit if it is an fImage object
                 if (!$file instanceof fImage && isset(self::$image_upload_columns[$class]) && array_key_exists($other_column, self::$image_upload_columns[$class])) {
                     continue;
                 }
                 $other_upload_dir = self::$file_upload_columns[$class][$other_column];
                 $other_temp_dir = self::prepareTempDir($other_upload_dir);
                 if ($existing_temp_file) {
                     $other_file = fFilesystem::createObject($other_temp_dir->getPath() . $file->getName());
                 } else {
                     $other_file = $file->duplicate($other_temp_dir, FALSE);
                 }
             } else {
                 $other_file = $file;
             }
             fActiveRecord::assign($values, $old_values, $other_column, $other_file);
             if (!$existing_temp_file && $other_file) {
                 self::processImage($class, $other_column, $other_file);
             }
         }
     }
     // Process the file
     if (!$existing_temp_file && $file) {
         self::processImage($class, $column, $file);
     }
     return $file;
 }
Esempio n. 3
0
 /**
  * Sets the timezone column and then tries to objectify the related timestamp column
  *
  * @internal
  *
  * @param  fActiveRecord $object            The fActiveRecord instance
  * @param  array         &$values           The current values
  * @param  array         &$old_values       The old values
  * @param  array         &$related_records  Any records related to this record
  * @param  array         &$cache            The cache array for the record
  * @param  string        $method_name       The method that was called
  * @param  array         $parameters        The parameters passed to the method
  * @return fActiveRecord  The record object, to allow for method chaining
  */
 public static function setTimezoneColumn($object, &$values, &$old_values, &$related_records, &$cache, $method_name, $parameters)
 {
     list($action, $subject) = fORM::parseMethod($method_name);
     $column = fGrammar::underscorize($subject);
     $class = get_class($object);
     if (!isset($parameters[0])) {
         throw new fProgrammerException('The method, %s(), requires at least one parameter', $method_name);
     }
     fActiveRecord::assign($values, $old_values, $column, $parameters[0]);
     // See if we can make an fTimestamp object out of the values
     self::objectifyTimestampWithTimezone($values, $old_values, self::$timezone_columns[$class][$column], $column);
     return $object;
 }
 /**
  * Figures out what filter to pass to fRequest::filter() for the specified related class
  *
  * @internal
  *
  * @param  string $class          The class name of the main class
  * @param  string $related_class  The related class being filtered for
  * @param  string $route          The route to the related class
  * @return string  The prefix to filter the request fields by
  */
 public static function determineRequestFilter($class, $related_class, $route)
 {
     $table = fORM::tablize($class);
     $schema = fORMSchema::retrieve($class);
     $related_table = fORM::tablize($related_class);
     $relationship = fORMSchema::getRoute($schema, $table, $related_table, $route);
     $route_name = fORMSchema::getRouteNameFromRelationship('one-to-many', $relationship);
     $primary_keys = $schema->getKeys($related_table, 'primary');
     $first_pk_column = $primary_keys[0];
     $filter_class = fGrammar::pluralize(fGrammar::underscorize($related_class));
     $filter_class_with_route = $filter_class . '{' . $route_name . '}';
     $pk_field = $filter_class . '::' . $first_pk_column;
     $pk_field_with_route = $filter_class_with_route . '::' . $first_pk_column;
     if (!fRequest::check($pk_field) && fRequest::check($pk_field_with_route)) {
         $filter_class = $filter_class_with_route;
     }
     return $filter_class . '::';
 }
Esempio n. 5
0
 /**
  * Takes a class name (or class) and turns it into a table name - Uses custom mapping if set
  *
  * @param  string $class  The class name
  * @return string  The table name
  */
 public static function tablize($class)
 {
     if (!isset(self::$class_table_map[$class])) {
         self::$class_table_map[$class] = fGrammar::underscorize(fGrammar::pluralize(preg_replace('#^.*\\\\#', '', $class)));
     }
     return self::$class_table_map[$class];
 }
 /**
  * Returns the metadata about a column including features added by this class
  *
  * @internal
  *
  * @param  fActiveRecord $object            The fActiveRecord instance
  * @param  array         &$values           The current values
  * @param  array         &$old_values       The old values
  * @param  array         &$related_records  Any records related to this record
  * @param  array         &$cache            The cache array for the record
  * @param  string        $method_name       The method that was called
  * @param  array         $parameters        The parameters passed to the method
  * @return mixed  The metadata array or element specified
  */
 public static function inspect($object, &$values, &$old_values, &$related_records, &$cache, $method_name, $parameters)
 {
     list($action, $subject) = fORM::parseMethod($method_name);
     $column = fGrammar::underscorize($subject);
     $class = get_class($object);
     $table = fORM::tablize($class);
     $db = fORMDatabase::retrieve($class, 'read');
     $schema = fORMSchema::retrieve($class);
     $info = $schema->getColumnInfo($table, $column);
     $element = isset($parameters[0]) ? $parameters[0] : NULL;
     $other_columns = self::$ordering_columns[$class][$column];
     // Retrieve the current max ordering index from the database
     $params = array("SELECT MAX(%r) FROM %r", $column, $table);
     if ($other_columns) {
         $params[0] .= " WHERE ";
         $params = self::addOtherFieldsWhereParams($schema, $params, $table, $other_columns, $values);
     }
     $max_value = (int) call_user_func_array($db->translatedQuery, $params)->fetchScalar();
     // If this is a new record, or in a new set, we need one more space in the ordering index
     if (self::isInNewSet($column, $other_columns, $values, $old_values)) {
         $max_value += 1;
     }
     $info['max_ordering_value'] = $max_value;
     $info['feature'] = 'ordering';
     fORM::callInspectCallbacks($class, $column, $info);
     if ($element) {
         return isset($info[$element]) ? $info[$element] : NULL;
     }
     return $info;
 }
 /**
  * Sets the money column and then tries to objectify it with an related currency column
  *
  * @internal
  *
  * @param  fActiveRecord $object            The fActiveRecord instance
  * @param  array         &$values           The current values
  * @param  array         &$old_values       The old values
  * @param  array         &$related_records  Any records related to this record
  * @param  array         &$cache            The cache array for the record
  * @param  string        $method_name       The method that was called
  * @param  array         $parameters        The parameters passed to the method
  * @return fActiveRecord  The record object, to allow for method chaining
  */
 public static function setMoneyColumn($object, &$values, &$old_values, &$related_records, &$cache, $method_name, $parameters)
 {
     list($action, $subject) = fORM::parseMethod($method_name);
     $column = fGrammar::underscorize($subject);
     $class = get_class($object);
     if (count($parameters) < 1) {
         throw new fProgrammerException('The method, %s(), requires at least one parameter', $method_name);
     }
     $value = $parameters[0];
     fActiveRecord::assign($values, $old_values, $column, $value);
     $currency_column = self::$money_columns[$class][$column];
     // See if we can make an fMoney object out of the values
     self::objectifyMoneyWithCurrency($values, $old_values, $column, $currency_column);
     if ($currency_column) {
         if ($value instanceof fMoney) {
             fActiveRecord::assign($values, $old_values, $currency_column, $value->getCurrency());
         }
     }
     return $object;
 }
 /**
  * @dataProvider underscorizeProvider
  */
 public function testUnderscorize($input, $output)
 {
     $this->assertEquals($output, fGrammar::underscorize($input));
 }
 /**
  * Sets the value for an email column, trimming the value if it is a valid email
  *
  * @internal
  *
  * @param  fActiveRecord $object            The fActiveRecord instance
  * @param  array         &$values           The current values
  * @param  array         &$old_values       The old values
  * @param  array         &$related_records  Any records related to this record
  * @param  array         &$cache            The cache array for the record
  * @param  string        $method_name       The method that was called
  * @param  array         $parameters        The parameters passed to the method
  * @return fActiveRecord  The record object, to allow for method chaining
  */
 public static function setEmailColumn($object, &$values, &$old_values, &$related_records, &$cache, $method_name, $parameters)
 {
     list($action, $subject) = fORM::parseMethod($method_name);
     $column = fGrammar::underscorize($subject);
     $class = get_class($object);
     if (count($parameters) < 1) {
         throw new fProgrammerException('The method, %s(), requires at least one parameter', $method_name);
     }
     $email = $parameters[0];
     if (preg_match('#^\\s*[a-z0-9\\.\'_\\-\\+]+@(?:[a-z0-9\\-]+\\.)+[a-z]{2,}\\s*$#iD', $email)) {
         $email = trim($email);
     }
     if ($email === '') {
         $email = NULL;
     }
     fActiveRecord::assign($values, $old_values, $column, $email);
     return $object;
 }
Esempio n. 10
0
 /**
  * Takes a class name (or class) and turns it into a table name - Uses custom mapping if set
  * 
  * @param  mixed $class  he class name or instance of the class
  * @return string  The table name
  */
 public static function tablize($class)
 {
     if (!isset(self::$class_table_map[$class])) {
         self::$class_table_map[$class] = fGrammar::underscorize(fGrammar::pluralize($class));
     }
     return self::$class_table_map[$class];
 }
Esempio n. 11
0
 private function hasProperty($property_name)
 {
     if (is_null($this->column_info)) {
         $this->column_info = $this->schema->getColumnInfo($this->table_name);
     }
     return isset($this->column_info[fGrammar::underscorize($property_name)]);
 }