PHP Booosta 3 Tutorial

PHP Booosta 3 Tutorial

Creating the files for users

In a previous step we created PHP files and templates for admin users. Now we want to do this for the normal users. We do not do this for every database table. Users should not be able to manipulate lecturers or courses. They just should deal with course registrations. And of course only with their own course registrations! So we create the file ENABLE_MKFILES in the webroot. If you have shell access you can do that with touch else you upload an empty file with that name with FTP. Then reload the admin page and click on "Create app files":
booosta13.png
Now we set the target to user. This has the effect, that all created files have a user_ prefix and slightly different contents. As in the admin section we also have to manipulate the PHP file. Here we have even more changes to do. The user should not be able to set the grade of his registration - it should always be "not attended" and of course he should not be able to select the user that is registered. That user should always be himself. Here are our modifications:
<?php
include_once 'lib/framework.incl.php';

class App extends booosta\usersystem\Webappuser
{
  protected $tpl_default = 'tpl/user_registration_default.tpl';
  protected $tpl_new = 'tpl/user_registration_new.tpl';
  protected $tpl_edit = 'tpl/user_registration_edit.tpl';

  #protected $fields = 'name,edit,delete';
  #protected $header = 'Name,Edit,Delete';
  #protected $sub_fields = 'name,edit,delete';
  #protected $use_datatable = true;

  protected $foreign_keys = [['course' => ['table' => 'course']],['user' => ['table' => 'user']],];
  #protected $checkbox_fields = '';
  #protected $auth_actions = true;

  protected function init()
  {
    parent::init();
    $this->default_clause = "user='$this->user_id'";
  }

  protected function before_add_($data, $obj)
  {
    $obj->set('regdate', date('Y-m-d'));
    $obj->set('user', $this->user_id);
    $obj->set('grade', 'not attended');
  }
}

$app = new App('registration');
$app->auth_user();
$app();
?>
Let's analyze that file:
  • The created class is derived from Webappuser, not Webappadmin
  • There are 3 variables $tpl_* that define the templates to use. This is necessary, because the default template for $tpl_new would be tpl/registration_new.tpl and this file is already used by the admin section.
  • There is a new (commented out) variable named $use_datatable. If it is set to true, the list of existing records is displayed with a javascript enabled table that allows paging, sorting and searching. It is advisable to use this option when displaying very long lists.
  • Another commented out variable is $auth_actions. We do not need it here. You need that if you have different users with different privileges and the right to execute a particular method should depend on having a special privilege.
  • We defined a new method: init(). This method is always called after the constructor of the object ran. First we call the constructor of the superclass (which is Webappuser). It is always a good idea to call the superclass' function if you override a function and you do not want to explicitly prevent the code of this method in the superclass from running. Then we set the object variable $default_clause to the SQL where clause that should be used to filter the correct records from the database. This particular clause makes sure, that only records that have the user_id of the current logged in user in the field "user" are displayed. All other records are ignored.
  • We already know the hook function before_add_(). Here we do some additional settings. We set the user to the current logged in user and the grade to "not attended".
Now we also have to change the templates.
tpl/user_registration_new.tpl:

{DATEINIT}
{BBOXCENTER}
{BPANEL|paneltitle::New Registration}

{BFORMSTART|user_registration.php|onSubmit::return checkForm();}
{HIDDEN action newdo}

{BFORMGRP Course size::4}{%list_course}{/BFORMGRP}

{BFORMSUBMIT class::center-block}
{BFORMEND}

{/BPANEL}
{/BBOXCENTER}

You see that we delete every form element except the list of courses. Edit the template tpl/user_registration_edit.tpl in a similar way.
Now log in as user and register with a course. Watch the registration in the list and also in the admin section.

Previous - Next