/** * * Usage: * 1. Petro::render_attr_table(<model>); * 2. Petro::render_attr_table(<model>, array(<col1> [ => Closure], <col2> [ => Closure], ..)); * ------------------------- * 1.1 render_attr_table(array) * this will use array key => value to render * 1.2 render_attr_table($data_from_model) * this will use $data->key => $data->value to render * if $data is from Orm\Model, we can use Model::properties() * 2.1 render_attr_table(array1, $disp_columns) * only array key contained in $disp_columns will be render * 2.2 render_attr_table($data_from_model, $disp_columns) * only column key contained in $disp_columns will be render * 3 if $disp_columns contain Closure as its value, Closure will be called and * the return value from Closure will be used */ public static function render_attr_table($data, $columns = null, $col_def = array()) { if (!isset($data)) { return "No data to display."; } if (!isset($columns)) { $columns = $data->properties(); } $out = static::render_attr_table_open($data); foreach ($columns as $col => $prop) { // if the $col is integer value, the actual column name is in its assoc.value $name = is_int($col) ? $columns[$col] : $col; // try to determine the label from its name first, unless the user override it later $label = \Lang::get($name) ?: \Inflector::humanize($name); if (!$data instanceof \Orm\Model) { $value = $data->{$name}; } elseif ($prop instanceof \Closure) { $value = $prop($data); } else { // $old_prop = $prop; $col_name = isset($col_def[$name]) ? $col_def[$name] : array(); $prop = array_merge($data->property($name, array()), (array) $prop, $col_name); $form = isset($prop['form']) ? $prop['form'] : array(); $grid = isset($prop['grid']) ? $prop['grid'] : array(); isset($prop['label']) and $label = $prop['label']; if (isset($form['type']) and $form['type'] == 'select') { if (isset($form['lookup']) and !isset($form['options'])) { if (!is_array($form['lookup'])) { $form['options'] = Petro_Lookup::get($form['lookup']); } } if (isset($form['options'])) { foreach ($form['options'] as $key => $val) { $form['options'][$key] = \Lang::get($val) ?: $val; } } } if (!empty($form['options'])) { $value = $form['options'][$data->{$name}]; } else { $value = isset($data->{$name}) ? $data->{$name} : ''; } if (isset($grid['process']) and !empty($grid['process'])) { if ($grid['process'] instanceof \Closure) { $value = $grid['process']($data, $value); } elseif (is_string($grid['process'])) { $model = get_class($data); $value = call_user_func(array($model, $grid['process']), $data, $value); } } if (isset($grid['format'])) { $value = Petro_Grid::format($grid['format'], $value); } } $out .= static::render_attr_table_row($label, $value); } $out .= static::render_attr_table_close(); return $out; }
public function add_column($name, $label = null, $data_type = null, $validation = array(), $form = array(), $grid = array()) { isset($label) or $label = \Lang::get($name) ?: \Inflector::humanize($name); //localize select options, if available if (isset($form['type']) and \Str::lower($form['type']) == 'select') { if (isset($form['lookup'])) { if (!is_array($form['lookup'])) { $form['options'] = Petro_Lookup::get($form['lookup']); } } if (isset($form['options'])) { foreach ($form['options'] as $key => $value) { $form['options'][$key] = \Lang::get($value) ?: $value; } } } // grid defaults $visible = isset($grid['visible']) ? $grid['visible'] : true; $sortable = isset($grid['sortable']) ? $grid['sortable'] : false; $align = isset($grid['align']) ? $grid['align'] : 'left'; $process = isset($grid['process']) ? $grid['process'] : null; $format = isset($grid['format']) ? $grid['format'] : null; $col = array('name' => $name, 'label' => $label, 'data_type' => $data_type, 'validation' => $validation, 'form' => $form, 'grid' => $grid); if (count($this->columns) === 0) { // if so, use it as the default order_by $this->order_by = array('col' => $name, 'dir' => 'asc'); } $this->columns[$name] = $col; }