/** * Prepares a file for output into HTML by returning filename or the web server path to the 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 void */ public static function prepare($object, &$values, &$old_values, &$related_records, &$cache, $method_name, $parameters) { list($action, $column) = fORM::parseMethod($method_name); if (sizeof($parameters) > 1) { throw new fProgrammerException('The column specified, %s, does not accept more than one parameter', $column); } $translate_to_web_path = empty($parameters[0]) ? FALSE : TRUE; $value = $values[$column]; if ($value instanceof fFile) { $path = $translate_to_web_path ? $value->getPath(TRUE) : $value->getName(); } else { $path = NULL; } return fHTML::prepare($path); }
/** * Prints a sortable column header `a` tag * * The a tag will include the CSS class `'sortable_column'` and the * direction being sorted, `'asc'` or `'desc'`. * * {{{ * #!php * fCRUD::printSortableColumn('name', 'Name'); * }}} * * would create the following HTML based on the page context * * {{{ * #!html * <!-- If name is the current sort column in the asc direction, the output would be --> * <a href="?sort=name&dir=desc" class="sorted_column asc">Name</a> * * <!-- If name is not the current sort column, the output would be --> * <a href="?sort-name&dir=asc" class="sorted_column">Name</a> * }}} * * @param string $column The column to create the sortable header for * @param string $column_name This will override the humanized version of the column * @return void */ public static function printSortableColumn($column, $column_name = NULL) { if ($column_name === NULL) { $column_name = fGrammar::humanize($column); } if (self::$sort_column == $column) { $sort = $column; $direction = self::$sort_direction == 'asc' ? 'desc' : 'asc'; } else { $sort = $column; $direction = 'asc'; } $columns = array_merge(array('sort', 'dir'), array_keys(self::$search_values)); $values = array_merge(array($sort, $direction), array_values(self::$search_values)); $url = fHTML::encode(fURL::get() . fURL::replaceInQueryString($columns, $values)); $css_class = self::$sort_column == $column ? ' ' . self::$sort_direction : ''; $column_name = fHTML::prepare($column_name); echo '<a href="' . $url . '" class="sortable_column' . $css_class . '">' . $column_name . '</a>'; }
/** * Prepares a money column by calling fMoney::format() * * @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 string The formatted monetary value */ public static function prepareMoneyColumn($object, &$values, &$old_values, &$related_records, &$cache, $method_name, $parameters) { list($action, $column) = fORM::parseMethod($method_name); if (empty($values[$column])) { return $values[$column]; } $value = $values[$column]; $remove_zero_fraction = FALSE; if (count($parameters)) { $remove_zero_fraction = $parameters[0]; } if ($value instanceof fMoney) { $value = $value->format($remove_zero_fraction); } return fHTML::prepare($value); }
/** * Gets a value from ::get() and passes it through fHTML::prepare() * * @param string $key The key to get the value of * @param string $cast_to Cast the value to this data type * @param mixed $default_value If the parameter is not set in the `DELETE`/`PUT` post data, `$_POST` or `$_GET`, use this value instead * @return string The prepared value */ public static function prepare($key, $cast_to = NULL, $default_value = NULL) { return fHTML::prepare(self::get($key, $cast_to, $default_value)); }
/** * 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); }
/** * Gets the value of an element and runs it through fHTML::prepare() * * @param string $element The element to get * @param mixed $default_value The value to return if the element has not been set * @return mixed The value of the element specified run through fHTML::prepare(), or the default value if it has not been set */ public function prepare($element, $default_value = NULL) { return fHTML::prepare($this->get($element, $default_value)); }
/** * Prints an `option` tag with the provided value, using the selected value to determine if the option should be marked as selected * * @param string $text The text to display in the option tag * @param string $value The value for the option * @param string $selected_value If the value is the same as this, the option will be marked as selected * @return void */ public static function printOption($text, $value, $selected_value = NULL) { $selected = FALSE; if ($value == $selected_value || is_array($selected_value) && in_array($value, $selected_value)) { $selected = TRUE; } echo '<option value="' . fHTML::encode($value) . '"'; if ($selected) { echo ' selected="selected"'; } echo '>' . fHTML::prepare($text) . '</option>'; }
"><?php echo fHTML::prepare($v->getName()); ?> </a></li> <?php } ?> </ul> <?php foreach ($this->variables as $v) { ?> <h3 id="<?php echo fHTML::encode($v->getName()); ?> "><?php echo fHTML::prepare($v->getName()); ?> </h3> <a href="#variables">[list]</a> <?php if (User::can('set-variable')) { ?> <a href="?edit=<?php echo fHTML::encode($v->getName()); ?> #set_variable">[edit]</a> <a href="?remove=<?php echo fHTML::encode($v->getName()); ?> #set_variable">[remove]</a> <?php
/** * Prepares a number column by calling fNumber::format() * * @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 string The formatted link */ public static function prepareNumberColumn($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); $schema = fORMSchema::retrieve($class); $column_info = $schema->getColumnInfo($table, $column); $value = $values[$column]; if ($value instanceof fNumber) { if ($column_info['type'] == 'float') { $decimal_places = isset($parameters[0]) ? (int) $parameters[0] : $column_info['decimal_places']; if ($decimal_places !== NULL) { $value = $value->trunc($decimal_places)->format(); } else { $value = $value->format(); } } else { $value = $value->format(); } } return fHTML::prepare($value); }
<i class="icon-ok"></i> <?php } ?> </td> <?php } ?> <td><a href="<?php echo SITE_BASE; ?> /contest/<?php echo $r->getId(); ?> "><?php echo fHTML::prepare($r->getTitle()); ?> </a></td> <td><?php echo count($r->getProblems()); ?> </td> <td><?php echo $r->getStartDatetime(); ?> </td> <td><?php echo $r->getEndDatetime(); ?> </td> <td><?php
/** * @dataProvider prepareProvider */ public function testPrepare($input, $output) { $this->assertEquals($output, fHTML::prepare($input)); }
echo $question->getId(); ?> /reply" method="POST"> <textarea name="reply"></textarea> <button type="submit" class="btn btn-mini">回复</button> </form> </td> <?php } else { ?> <td class="timestamp"><?php echo $question->getAnswerTime(); ?> </td> <td><?php echo fHTML::prepare($question->getAnswer()); ?> </td> <?php } ?> <?php if ($this->report->allowAnswer()) { ?> <td> <form class="form-inline" action="<?php echo SITE_BASE; ?> /question/<?php echo $question->getId(); ?>