Model Settings

For more detailed control of model behavior it is possible to pass additional parameters with "$model_display_params" property in a model class. All options are valid for Admin Panel , and also for "create ()", "update ()" and "delete()" methods.

Editable and not editable fields

"editable_fileds" - the editable model fields
by default: "*" (all fields)

"not_editable_fields" - not editable model fields, not to be edited in Admin Interface
by default: an empty "array()"

Visible and hidden fields

"visible_fields" - visible fields of model
by default: "*" (all fields)

"hidden_fields" - hidden fields of model, not shown in Admin Interface
by default: an empty "array()"

Operations of creating, editing and deleting

"create_actions" - an option to create records of model in Admin Panel
"update_actions" - an option to edit records of model in Admin Panel
"delete_actions" - an option to delete records of model in Admin Panel
"mass_actions" - an option of mass editing / deleting records of model in Admin Panel

All fourparameters are "true" by default.

Group fields on tabs

If there are many fields in a model, it is possible to group them on tabs (when creating and editing records in Admin Panel).

"fields_groups" => array("Group name 1 (tabs)" => array(array of field names),
                         "Group name 2 (tabs)" => array(array of field names));

Default fields in the General Table of Model Records

"default_table_columns" - an array of fields which are shown in the general table before a user enters his own field settings

"default_table_columns" => array("name", "date", "color", "options")

Transfer to other models

For some records of model in Admin Panel it is possible to set an option of transfer to other models. Such a need may happen, for example, in the Menu Pages model when the News item is in the list of model records, but actual news are located in News Model. For users convenience it is possible to set an option to transfer not to the next level of a tree, but into News model directly when clicking on News link.

"redirects_to_models" - an array of parameters for transfer to other models by registered attributes (as "id"="32", "url"="news", "name" = "Gallery"). This option is active only in tree-like models (with a "parent" field inside).

"redirects_to_models" => array("url" => array("news" => "News"),
                               "name" => array("Catalog" => "Catalogs"),
                               "id" => array("8" => "Faq"))

Link to foreign keys

If this option is on, so for the foreign keys values in the main table of model in Admin Panel, the link to foreign keys pages will be added.

"foreign_keys_admin_links" => true

Number of version restrictions

When update record in Admin Panel old version of all fields are saved in order to make rollback in the future. Limit of keeped versions of each record for all models is set in "ModelVersionsLimit" option in "config/settings.php" file. By default this limit is 25. For each model this value can be changed or completely switched off the version saving. When the number of versions for record exeeds the limit oldest versions are removed.

"versions_limit" => 30
"versions_limit" => 0

Model Filters settings

"default_filters" - an array of filters that are shown by default before any filter runs (if the option is not set, then the first 7 filters of the model will be displayed)

"show_empty_default_filters" - if the option is turned on, filters will be shown in the sidebar by default even if they are empty, an the others are running

"default_filters" => array("price", "articul"), 
"show_empty_default_filters" => true

"$name_field" and "$name_field_extra" parameters

It is used when there is no "name" field in the model . This is a record name field which is displayed in statistics, for example on the main page. Parameter of "$name_field" specifies which field in the model to be nominal. It is also possible to set the additional parameter "$name_field_extra" is to attach a value of one more field to the name of record.

Example, model of clients for registration on the website

class Clients extends Model
{
    protected $name = "Clients";

    protected $name_field = "first_name";

    protected $name_field_extra = "last_name";

    protected $model_elements = array(
        array("Activation", "bool", "active"),
        array("Registration confirmation", "bool", "confirm"),
        array("Name", "char", "first_name", array("required" => true)),
        array("Last name", "char", "last_name", array("required" => true)),
        ...
    );

    protected $model_display_params = array(
        "editable_fields" => array("active", "first_name", "last_name"),
        "hidden_fields" => array("confirm"),
        "create_actions" => false,
        "fields_groups" => array("Main parameters" => array("first_name","last_name"), 
                                 "Registration activation" => array("active","confirm"),
                                 ... 
                                 ),
        "default_table_columns" => array("first_name", "last_name", "active"), 
        "default_filters" => array("first_name", "last_name") 
    );
}

Event Methods

When in Admin Panel record creation, editing or deleting operations are occured, some special methods can be called to make additional operations over data. In all methods $id - is a key of the current record, $fields - an associative array of record fields as "array('name' => 'Maxim', 'age' => 27)".

  • beforeCreate($fields) - a method is called before record creation (the id doesn't exist yet)
  • afterCreate($id, $fields) - a method is called after record creation (the id is already received after INSERT query)
  • beforeUpdate($id, $old_fields, $new_fields) - before editing record (old and new field values)
  • afterUpdate($id, $new_fields) - after editing record (new field values)
  • beforeDelete($id, $fields) - before deleting into recycle bin
  • afterDelete($id, $fields) - after deleting into a recycle bin (no entry in the table of model )
  • beforeRestore($id, $fields) - before restoring from a recycle bin
  • afterRestore($id, $fields) - after restoring from a recycle bin
  • beforeFinalDelete($id, $fields) - before final deleting from a recycle bin
  • afterFinalDelete($id, $fields) - after final deleting from a recycle bin (no entry in the table of a recycle bin)

Attention! "beforeCreate()" and "beforeUpdate()" methods can return an array of the modified fields which will be written in a database and the change history. Thus, the returned array may contain only those fields that have been modified inside this method. In beforeUpdate method both old and new values of record fields are passed to the input (for comparison). See examples below.

Attention! If "beforeDelete()" or "beforeRestore()" return "false" so operation will not be completed.

class Products extends Model 
{
    protected $name = "Products catalog";

    protected $model_elements = array( ... );

    protected function beforeCreate($fields)
    {
        if(!$fields["price"])
            return array("active" => 0);
    }

    protected function beforeUpdate($id, $old_fields, $new_fields)
    {
        if(!$old_fields["active"] && $new_fields["active"]) 
            return array("status" => "new", "order" => 1);
    }

    protected function beforeDelete($id, $fields)
    { 
        if($fields["type"] == "special") 
        { 
            $this -> addError("Can not delete catalog by the reason of..."); 
            return false; 
        }
    }
}

Insert a code into Admin Interface

You can add your own HTML and PHP code snippets in Admin Panel if needed. For this purpose in the "models/includes/" folder we create files which will be automatically included in pre-designated places. Use the name of the model in the file name, instead of the word "model" use a name of model, for example "products-create.top.php".

For standard models

List of model records

  • model-index-top.php - on top of buttons of the upper navigation
  • model-index-bottom.php - after one by one pagination

Creation of new record

  • model-create-top.php - before the form
  • model-create-form.php - in front of buttons before closing of the form
  • model-create-bottom.php - after the form and buttons

Editing record

  • model-update-top.php - before the form
  • model-update-form.php - in front of buttons before closing of the form
  • model-update-bottom.php - after the form and buttons

For simple models

Editing fields of simple model

  • model-index-top.php - before the form
  • model-index-form.php - in front of buttons before closing of the form
  • model-index-bottom.php - after the form

Example of the included file, we will add a to redirect to a special page in Admin Panel that is described in Additional Notes to Admin Panel section. For model of products we will create a file with a button which will be located under the table with model records. The file name for "Products" model is "models/includes/products-index-bottom.php".

<?
//If a user has edit rights, show a link
//if not, show an error message about insufficient rights
if($system -> user -> checkModelRights($system -> model -> getModelClass(), "update"))
    $update_path = "location.href='".$system -> registry -> getSetting("AdminPanelPath")."custom/?view=upload'";
else
    $update_path = "dialogs.showAlertMessage('{no_rights}')";
?>

<div class="extra-admin-buttons">
   <input type="button" class="button-light" value="Update products base" onclick="<? echo $update_path; ?>" />
</div>