Model settings

To set individual settings for model it's 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 of model.

Editable and not editable fields

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

"not_editable_fields" - not editable model fields, can't be edited in admin panel
by default: an empty array

Visible and hidden fields

"visible_fields" - visible fields of model, shown in admin panel
by default: "*" (all fields)

"hidden_fields" - hidden fields of model, not shown in admin panel
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 four parameters are "true" by default.

Grouping 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 model records list

"default_table_columns" - an array of fields which are shown in the general table before a user applies own fields 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 "Pages" model when the "News" item placed in the list of model records, but actual news are located in News model. For user's convenience it is possible to set an option to redirect 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).

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

Link to foreign keys models

If this option is on, the foreign keys values in records table will get a special link. This link will refer to appropriate model and run filter to show only records related to selected foreign key value.

"foreign_keys_admin_links" => true

Kept versions limit

When we update record in admin panel, the old version of all fields is saved in order to make rollback in the future. Limit of kept versions of each record for all models is set in "ModelVersionsLimit" option in "config/settings.php" file. By default this limit is 25 versions for any model. For each model this value can be changed or it's possible to completely switch off the versions 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 applied (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, and 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 history log page. Parameter of "$name_field" specifies which field in the model should be the main name field . It is also possible to set the additional parameter "$name_field_extra" to attach a value of one more field to the name of record in admin panel interface.

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 we create, edit or delete record in admin panel and appropriate operations are occurred, some special methods can be called to make additional operations with record's data. In all methods $id - is a key of the current record, $fields - an associative array of record fields as "array('name' => 'John', '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 a record (old and new fields values)
  • afterUpdate($id, $new_fields) - after editing a record (new field values)
  • beforeDelete($id, $fields) - before deleting into recycle bin
  • afterDelete($id, $fields) - after deleting into recycle bin (already it's no record in the table of model)
  • beforeRestore($id, $fields) - before restoring a record from a recycle bin
  • afterRestore($id, $fields) - after restoring a record from recycle bin
  • beforeFinalDelete($id, $fields) - before final deleting from recycle bin
  • afterFinalDelete($id, $fields) - after final deleting from recycle bin (already it's no record 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 method (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 fragments in admin panel if needed. For this purpose in the "customs/models/" folder we create files which will be automatically included in predesignated 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

Both creation and editing

  • model-action-top.php - before the form
  • model-action-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 redirect to the 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 "customs/models/products-index-bottom.php".

<?
//If a user has update 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>