/** * Retrieves a value from the record and prepares it for output into html. * * Below are the transformations performed: * * - **varchar, char, text**: will run through fHTML::prepare(), if `TRUE` is passed the text will be run through fHTML::convertNewLinks() and fHTML::makeLinks() * - **boolean**: will return `'Yes'` or `'No'` * - **integer**: will add thousands/millions/etc. separators * - **float**: will add thousands/millions/etc. separators and takes 1 parameter to specify the number of decimal places * - **date, time, timestamp**: `format()` will be called on the fDate/fTime/fTimestamp object with the 1 parameter specified * - **objects**: the object will be converted to a string by `__toString()` or a `(string)` cast and then will be run through fHTML::prepare() * * @param string $column The name of the column to retrieve * @param mixed $formatting The formatting parameter, if applicable * @return string The formatted value for the column specified */ protected function prepare($column, $formatting = NULL) { $column_exists = array_key_exists($column, $this->values); $method_name = 'get' . fGrammar::camelize($column, TRUE); $method_exists = method_exists($this, $method_name); if (!$column_exists && !$method_exists) { throw new fProgrammerException('The column specified, %s, does not exist', $column); } if ($column_exists) { $class = get_class($this); $table = fORM::tablize($class); $schema = fORMSchema::retrieve($class); $column_info = $schema->getColumnInfo($table, $column); $column_type = $column_info['type']; // Ensure the programmer is calling the function properly if ($column_type == 'blob') { throw new fProgrammerException('The column specified, %s, can not be prepared because it is a blob column', $column); } if ($formatting !== NULL && in_array($column_type, array('integer', 'boolean'))) { throw new fProgrammerException('The column specified, %s, does not support any formatting options', $column); } // If the column doesn't exist, we are just pulling the // value from a get method, so treat it as text } else { $column_type = 'text'; } // Grab the value for empty value checking $value = $this->{$method_name}(); // Date/time objects if (is_object($value) && in_array($column_type, array('date', 'time', 'timestamp'))) { if ($formatting === NULL) { throw new fProgrammerException('The column specified, %s, requires one formatting parameter, a valid date() formatting string', $column); } return $value->format($formatting); } // Other objects if (is_object($value) && is_callable(array($value, '__toString'))) { $value = $value->__toString(); } elseif (is_object($value)) { $value = (string) $value; } // Ensure the value matches the data type specified to prevent mangling if ($column_type == 'boolean' && is_bool($value)) { return $value ? 'Yes' : 'No'; } if ($column_type == 'integer' && is_numeric($value)) { return number_format($value, 0, '', ','); } if ($column_type == 'float' && is_numeric($value)) { // If the user passed in a formatting value, use it if ($formatting !== NULL && is_numeric($formatting)) { $decimal_places = (int) $formatting; // If the column has a pre-defined number of decimal places, use that } elseif ($column_info['decimal_places'] !== NULL) { $decimal_places = $column_info['decimal_places']; // This figures out how many decimal places are part of the current value } else { $value_parts = explode('.', $value); $decimal_places = !isset($value_parts[1]) ? 0 : strlen($value_parts[1]); } return number_format($value, $decimal_places, '.', ','); } // Turn line-breaks into breaks for text fields and add links if ($formatting === TRUE && in_array($column_type, array('varchar', 'char', 'text'))) { return fHTML::makeLinks(fHTML::convertNewlines(fHTML::prepare($value))); } // Anything that has gotten to here is a string value, or is not the // proper data type for the column, so we just make sure it is marked // up properly for display in HTML return fHTML::prepare($value); }