function testArrayToHash() { $arr = array('first' => 'one', 'second' => 'two', 'third', 'fourth'); $arr = array_to_hash($arr); $this->assertEquals(array('first' => 'one', 'second' => 'two', 'third' => 'third', 'fourth' => 'fourth'), $arr); }
/** * Convert our filter arrays (source hosts, log levels, and log facilities) into * hashes. This saves us from needing to search through the array to see if a * value exists and is much more performant. **/ if ($_GET['log_facilities']) { $_GET['log_facilities'] = array_to_hash($_GET['log_facilities']); } if ($_GET['log_levels']) { $_GET['log_levels'] = array_to_hash($_GET['log_levels']); } if ($_GET['source_hosts']) { $_GET['source_hosts'] = array_to_hash($_GET['source_hosts']); } /** * This script can be quite costly to run; but we specifically do not want it to timeout * on a system administrator or developer who is searching over a large amount of logs. * Therefore we allow the script to run for 1 second for each 10 seconds of the requested * timeframe, with a minimum of 30 seconds. This could be dangerous, however a conscious * choice has been made to be extravagant with the resources allocated to this script to * make things nicer for the users of it. **/ set_time_limit(max(30, ceil(($end_timestamp - $start_timestamp) / 10))); } } ?> <html>
protected static function getThruFields($thru, $thru_fields) { // setup the select fields for the thru table $thru_fields = array_to_hash($thru_fields); $thru_select_fields = array(); foreach ($thru_fields as $field) { $thru_select_fields["{$thru}.{$field}"] = $field; } return $thru_select_fields; }
/** * Short description for file * * Long description for file (if any)... * * @category Framework * @package Pfw * @author Sean Sitter <*****@*****.**> * @copyright 2010 The Picnic PHP Framework * @license http://www.apache.org/licenses/LICENSE-2.0 Apache License, Version 2.0 * @link http://www.picnicphp.com * @since 0.10 */ function smarty_function_form_field($params, &$smarty) { list($args, $attrs) = $smarty->_filterAttrs($params); $obj_prop = $args['prop']; if ($args['confirmation']) { $obj_prop = $obj_prop . "_confirm"; } if (array_key_exists('value', $args)) { $attrs['value'] = $args['value']; } if (array_key_exists('index', $args)) { $args['for'] = $args['for'] . "[{$args['index']}]"; } $obj_path = $args['for']; if (empty($obj_path)) { error_log("missing required template variable name 'for'"); } // get the value of the property $var = ""; if (false === strpos($obj_path, '[')) { $var = $obj_path; $obj = $smarty->get_template_vars($var); } else { preg_match("/^([\\w]+)\\[?/", $obj_path, $matches); $var = $matches[1]; $obj_path = str_replace($var, 'obj', $obj_path); // not sure if this is needed $obj_path = preg_replace('/\\[(?!\')/', '[\'', $obj_path); $obj_path = preg_replace('/\\](?<!\')/', '\']', $obj_path); $e = "return \${$obj_path};"; $obj = $smarty->get_template_vars($var); $obj = eval($e); } if (empty($obj)) { Pfw_Loader::loadClass('Pfw_Form'); $obj = new Pfw_Form(); } $element = "input"; $content = null; if (!isset($args['type'])) { if (method_exists($obj, 'getSchema')) { $schema = $obj->getSchema(); switch ($schema[$obj_prop]['type']) { case Pfw_Db_Adapter::TYPE_CHARACTER: case Pfw_Db_Adapter::TYPE_INTEGER: case Pfw_Db_Adapter::TYPE_FLOAT: case Pfw_Db_Adapter::TYPE_ENUM: $attrs['type'] = "text"; if (!isset($attrs['size'])) { $attrs['size'] = 3; } break; case Pfw_Db_Adapter::TYPE_TEXT: $attrs['type'] = "textarea"; break; case Pfw_Db_Adapter::TYPE_VARCHAR: $attrs['type'] = "text"; if (!isset($attrs['size'])) { $attrs['size'] = 32; } break; case Pfw_Db_Adapter::TYPE_DATE: break; case Pfw_Db_Adapter::TYPE_TIME: break; } } else { $attrs['type'] = "text"; } } else { $attrs['type'] = $args['type']; } if (!isset($attrs['value']) and isset($obj->{$obj_prop})) { if (is_object($obj)) { $attrs['value'] = $obj->{$obj_prop}; } elseif (is_array($obj) and isset($obj[$obj_prop])) { $attrs['value'] = $obj[$obj_prop]; } } if (!array_key_exists('value', $attrs)) { if (array_key_exists('default_value', $args)) { $attrs['value'] = $args['default_value']; } else { $attrs['value'] = ""; } } if ($attrs['type'] == 'checkbox') { if ($attrs['value']) { $attrs['checked'] = "1"; } $attrs['value'] = 1; } /* elseif (empty($attrs['value'])) { $attrs['value'] = ""; } */ if ('textarea' == $attrs['type']) { $element = "textarea"; $content = $attrs['value']; unset($attrs['type']); unset($attrs['value']); } elseif ('select' == $attrs['type']) { $element = "select"; $content = "\n"; if (empty($params['options'])) { $params['options'] = array(); } $options = array_to_hash($params['options']); foreach ($options as $label => $value) { $label = empty($label) ? $value : $label; $selected = ""; if ($value == $attrs['value']) { $selected = " selected"; } $value = urlencode($value); $content .= "<option value=\"{$value}\"{$selected}>{$label}</option>\n"; } unset($attrs['type']); unset($attrs['value']); } $attrs['name'] = "{$args['for']}[{$obj_prop}]"; $err_list = ""; if (is_object($obj) and method_exists($obj, 'hasErrors') and $obj->hasErrors($args['prop']) and false !== $args['highlight_errors']) { if (isset($attrs['class'])) { $attrs['class'] = "{$attrs['class']} pfw-error"; } else { $attrs['class'] = "pfw-error"; } } $field = $smarty->_generateElement($element, $attrs, $content); return $err_list . $field; }
/** * Short description for file * * Long description for file (if any)... * * @category Framework * @package Pfw * @author Sean Sitter <*****@*****.**> * @copyright 2010 The Picnic PHP Framework * @license http://www.apache.org/licenses/LICENSE-2.0 Apache License, Version 2.0 * @link http://www.picnicphp.com * @since 0.10 */ function smarty_function_formset_field($params, &$smarty) { list($args, $attrs) = $smarty->_filterAttrs($params); $var = $smarty->get_template_vars('_pfw_formset_var'); $index = $smarty->get_template_vars('_pfw_formset_idx'); $obj = $smarty->get_template_vars('_pfw_formset_obj'); if (array_key_exists('value', $args)) { $attrs['value'] = $args['value']; } $obj_prop = $args['prop']; if ($args['confirmation']) { $obj_prop = $obj_prop . "_confirm"; } if (array_key_exists('index', $args)) { $args['for'] = $args['for'] . "[{$index}]"; } if (empty($obj)) { Pfw_Loader::loadClass('Pfw_Form'); $obj = new Pfw_Form(); } $element = "input"; $content = null; if (!isset($args['type'])) { if (method_exists($obj, 'getSchema')) { $schema = $obj->getSchema(); switch ($schema[$obj_prop]['type']) { case Pfw_Db_Adapter::TYPE_CHARACTER: case Pfw_Db_Adapter::TYPE_INTEGER: case Pfw_Db_Adapter::TYPE_FLOAT: case Pfw_Db_Adapter::TYPE_ENUM: $attrs['type'] = "text"; if (!isset($attrs['size'])) { $attrs['size'] = 3; } break; case Pfw_Db_Adapter::TYPE_TEXT: $attrs['type'] = "textarea"; break; case Pfw_Db_Adapter::TYPE_VARCHAR: $attrs['type'] = "text"; if (!isset($attrs['size'])) { $attrs['size'] = 32; } break; case Pfw_Db_Adapter::TYPE_DATE: break; case Pfw_Db_Adapter::TYPE_TIME: break; } } else { $attrs['type'] = "text"; } } else { $attrs['type'] = $args['type']; } if (!isset($attrs['value'])) { if (is_object($obj) and isset($obj->{$obj_prop})) { $attrs['value'] = $obj->{$obj_prop}; } elseif (is_array($obj) and isset($obj[$obj_prop])) { $attrs['value'] = $obj[$obj_prop]; } } if (!array_key_exists('value', $attrs)) { if (array_key_exists('default_value', $args)) { $attrs['value'] = $args['default_value']; } else { $attrs['value'] = ""; } } if ($attrs['type'] == 'checkbox') { if ($attrs['value']) { $attrs['checked'] = "1"; } $attrs['value'] = 1; } if ('textarea' == $attrs['type']) { $element = "textarea"; $content = $attrs['value']; unset($attrs['type']); unset($attrs['value']); } elseif ('select' == $attrs['type']) { $element = "select"; $content = "\n"; if (empty($params['options'])) { $params['options'] = array(); } $options = array_to_hash($params['options']); foreach ($options as $label => $value) { $label = empty($label) ? $value : $label; $selected = ""; if ($value == $attrs['value']) { $selected = " selected"; } $value = urlencode($value); $content .= "<option value=\"{$value}\"{$selected}>{$label}</option>\n"; } unset($attrs['type']); unset($attrs['value']); } $attrs['name'] = "{$var}[{$index}][{$obj_prop}]"; $err_list = ""; if (is_object($obj) and method_exists($obj, 'hasErrors') and $obj->hasErrors($args['prop']) and false !== $args['highlight_errors']) { if (isset($attrs['class'])) { $attrs['class'] = "{$attrs['class']} pfw-error"; } else { $attrs['class'] = "pfw-error"; } } $field = $smarty->_generateElement($element, $attrs, $content); return $err_list . $field; }
/** * If this instance has a value for its primary key property, * sets our instance variables to the values in the db. * * @example Pfw/Model/retrieve.inc * * @param $options * @return unknown_type */ public function retrieve($options = array()) { if (null == $this->getId()) { throw new Pfw_Exception_System("Cannot retrieve '" . $this->getClass() . "' without an id"); } $pk_name = $this->getPrimaryKey(); $qo = $this->Q($this->_getDb())->where(array("this.{$pk_name} = %s", $this->getId()))->first(); if (isset($options['with'])) { if (is_string($options['with'])) { $desc = $this->_getAssociationDescription($options['with']); $js = isset($desc['default_strategy']) ? $desc['default_strategy'] : "Immediate"; $qo->with($options['with'], array('join_strategy' => $js)); } elseif (is_array($options['with'])) { $withs = array_to_hash($options['with']); foreach ($withs as $with => $options) { $desc = $this->_getAssociationDescription($with); if ($with == $options) { $js = isset($desc['default_strategy']) ? $desc['default_strategy'] : "Immediate"; $qo->with($with, array('join_strategy' => $js)); } else { if (isset($options['join_strategy'])) { $js = $options['join_strategy']; } else { $js = isset($desc['default_strategy']) ? $desc['default_strategy'] : "Immediate"; } $qo->with($with, array('join_strategy' => $js)); } } } } $obj = $qo->exec(); if (is_null($obj)) { Pfw_Loader::loadClass('Pfw_Exception_NotFound'); $class = $this->getClass(); throw new Pfw_Exception_NotFound("Could not find '{$class}' with id: " . $this->getId()); } $this->_setProperties((array) $obj, false); $this->_setRetrieved(); unset($obj); }
protected function doJoins() { $withs = array_keys($this->with); if (empty($withs)) { return; } foreach ($withs as $with) { $with_params = $this->description['has'][$with]; // get the model description for the association model $with_model_class = $with_params['class']; Pfw_Loader::loadModel($with_model_class); $model_inst = new $with_model_class(); $with_model_desc = $model_inst->_getDescription(); $this->withDesc[$with] = $with_model_desc; $with_model_fields = array_to_hash(array_keys($with_model_desc['schema'])); // remove anything that should be blocked if (isset($with_model_desc['block_fetch_fields'])) { $block_fetch_fields = array_keys($with_model_desc['block_fetch_fields']); foreach ($with_model_fields as $alias => $field) { if (in_array($field, $block_fetch_fields)) { unset($with_model_fields[$alias]); } } } if (isset($with_model_desc['fetch_fields'])) { $with_model_fields = array_merge($with_model_fields, $with_model_desc['fetch_fields']); } // all of the fields in the association model $with_select_fields = array(); foreach ($with_model_fields as $field => $value) { if (is_a($value, 'Pfw_Db_Expr')) { $value->setAlias($with); } $with_select_fields["{$with}.{$field}"] = $value; } // just an alias $join_extra_cond = ""; if (isset($with_params['conditions'])) { $join_extra_cond .= " AND {$with_params['conditions']}"; } $ct = $with_params['count']; if ($ct == Pfw_Model::ASSOC_BELONGSTO) { $cond = "this.{$with_params['foreign_key']} = {$with}.{$with_params['owner_key']}"; $this->select->joinLeft(array($with => $with_params['table']), array("{$cond}{$join_extra_cond}"), $with_select_fields); } elseif ($ct == Pfw_Model::ASSOC_ONE or $ct == Pfw_Model::ASSOC_MANY) { $my_key = $this->description['has'][$with]['my_key']; if (isset($with_params['thru'])) { $this->hasThru = true; $this->thruDesc[$with_params['thru']] = array('owned_by' => $with, 'class' => $with_params['thru_class']); // setup the select fields for the thru table $thru_fields = array_to_hash($with_params['thru_fields']); $thru_select_fields = array(); foreach ($thru_fields as $field) { $thru_select_fields["{$with_params['thru']}.{$field}"] = $field; } // us->join table $thru_extra_cond = ""; if (isset($with_params['thru_conditions'])) { $thru_extra_cond = "AND {$with_params['conditions']}"; } $cond = "this.{$with_params['my_key']} = {$with_params['thru']}.{$with_params['my_join_key']}"; $this->select->joinLeft(array($with_params['thru'] => $with_params['thru_table']), array("{$cond}{$thru_extra_cond}"), $thru_select_fields); // join table->association $cond = "{$with_params['thru']}.{$with_params['as_join_key']} = {$with}.{$with_params['as_key']}"; $this->select->joinLeft(array($with => $with_params['table']), array("{$cond}{$join_extra_cond}"), $with_select_fields); } else { // us->association $cond = "this.{$with_params['my_key']} = {$with}.{$with_params['foreign_key']}"; $this->select->joinLeft(array($with => $with_params['table']), array("{$cond}{$join_extra_cond}"), $with_select_fields); } } } }
public function getJsLinksHtml() { if (empty($this->js_linkrel_arr)) { return; } global $_PATHS; $js_linkrels = array_unique(array_to_hash($this->js_linkrel_arr)); $rev = $this->get_config_vars('LINKREL_REV'); $links = ""; foreach ($js_linkrels as $alias => $js_linkrel) { $attr = $this->js_attr_arr[$alias]; if (isset($this->ext_js_linkrels[$alias])) { $attr['src'] = $js_linkrel; } else { $attr['src'] = $_PATHS['js'] . $js_linkrel; if (!empty($rev)) { $attr['src'] = str_replace('.js', '', $attr['src']); $attr['src'] = "{$attr['src']}.v{$rev}.js"; } } $links .= $this->_generateJsElement($attr) . "\n"; } return $links; }
protected function _toStringJoins() { $joins_str = ""; foreach ($this->joins as $join_bundle) { $join_type = $join_bundle[0]; $type_str = $this->join_str[$join_type]; $from_part = $join_bundle[1]; $v = array_keys(array_to_hash($from_part)); $table_or_alias = $v[0]; if ($table_or_alias == $from_part[$table_or_alias]) { $table = $table_or_alias; $table_str = "`{$table}`"; } else { $alias = $table_or_alias; $table = $from_part[$table_or_alias]; $table_str = "`{$table}` AS `{$alias}`"; } $joins_str .= "{$type_str} {$table_str}"; if (self::JOIN_CROSS == $join_type) { continue; } $join_conds = $join_bundle[3]; if (!empty($join_conds)) { if (is_array($join_conds)) { $join_conds = $this->adapter->_sprintfEsc($join_conds); } $joins_str .= " ON {$join_conds} "; } } return $joins_str; }