function table_for(moojon_model_collection $models, $column_names = array(), $attributes = array(), $no_records_message = null, $count = null) { $no_records_message = $no_records_message ? $no_records_message : moojon_config::get('no_records_message'); if ($models->count) { $attributes = try_set_attribute($attributes, 'cellpadding', '0'); $attributes = try_set_attribute($attributes, 'cellspacing', '0'); $model = $models->first; $model_class = get_class($model); $ths = array(th_tag(title_text($model->get_to_string_column()))); $column_names = $column_names ? $column_names : $model->get_ui_column_names(); foreach ($column_names as $column_name) { if ($model->to_string_column != $column_name) { $ths[] = th_tag(title_text($column_name)); } } $ths[] = th_tag('Edit'); $ths[] = th_tag('Delete'); $trs = array(); $primary_key = moojon_primary_key::NAME; $counter = 0; foreach ($models as $model) { $counter++; $tds = array(td_tag(member_tag($model))); foreach ($column_names as $column_name) { if ($model->to_string_column != $column_name) { if (method_exists($model, "get_{$column_name}") || method_exists($model, $column_name)) { $content = $model->{$column_name}; if (is_object($content) && is_subclass_of($content, 'moojon_base_column')) { $column = $content; $content = $column->get_value(); } else { $column = new moojon_string_column($column_name); $column->set_value($content); } } else { $column = $model->get_column($column_name); if ($relationship = find_has_one_relationship($model, $column_name)) { $name = $relationship->get_name(); $content = member_tag($model->{$name}); } else { if ($relationship = find_belongs_to_relationship($model, $column_name)) { $name = $relationship->get_name(); $content = member_tag($model->{$name}); } else { $content = $model->{$column_name}; } } } $tds[] = td_tag(format_content($model, $column, $content)); } } $tds[] = td_tag(edit_member_tag($model)); $tds[] = td_tag(delete_member_tag($model)); $trs[] = tr_tag($tds, array('class' => 'row' . $counter % 2)); } $children = array(thead_tag(tr_tag($ths)), tbody_tag($trs)); if ($count) { $ul = paginator_ul($count); $children[] = tfoot_tag(tr_tag(td_tag($ul, array('colspan' => count($column_names) + 2)))); } $child = table_tag($children, $attributes); } else { $child = p_tag($no_records_message); } return div_tag($child); }
function rest_actions(moojon_base_model $model, moojon_rest_route $route = null, $action = null, $attributes = array()) { $action = $action ? $action : strtolower(ACTION); $route = $route ? $route : moojon_routes::get_rest_route($model->get_table()); $actions = $route->get_actions(); $lis = array(); switch ($action) { case 'index': if (in_array('_new', $actions)) { $lis[] = li_tag(new_member_tag($model)); } break; case '_new': if (in_array('index', $actions)) { $lis[] = li_tag(collection_tag($model)); } break; case 'show': if (in_array('edit', $actions)) { $lis[] = li_tag(edit_member_tag($model)); } if (in_array('delete', $actions)) { $lis[] = li_tag(delete_member_tag($model)); } break; case 'edit': if (in_array('show', $actions)) { $lis[] = li_tag(member_tag($model)); } if (in_array('delete', $actions)) { $lis[] = li_tag(delete_member_tag($model)); } break; case 'delete': if (in_array('show', $actions)) { $lis[] = li_tag(member_tag($model)); } if (in_array('edit', $actions)) { $lis[] = li_tag(edit_member_tag($model)); } break; default: $lis = array(); break; } return div_tag(ul_tag($lis), $attributes); }