📜 ⬆️ ⬇️

When you need something almost ready

image

Good day, habrazhitel!

Just over a year ago I ran into a situation where it was necessary to implement a web application (as usual, in a very short time) with rich functionality:
')
  1. Manage user accounts with different roles
  2. Assigning tasks to users and tracking the future life of these tasks
  3. User planning (per day, month)
  4. Work with the results of the work of employees (recording time and types of work)
  5. Formation of various reports, statistical information cuts and TP

And this is only the tenth part of what had to be done yesterday. Immediately make a reservation: I am not a web developer, so I needed a solution on which to build a web application as quickly as possible without deeply immersed in the world of PHP, JS, Java, Ruby, and so on.

I will deliberately skip the analysis and the choice of means of implementation and go straight to the most interesting: Koala Framework was chosen as the platform - an explosive mixture from Zend Framework and ExtJS. This framework is based on the MVC model (model-view-controller). But his real strength is autogenerated forms and tables ( AutoForm and AutoGrid ).

The meaning of auto-forms and auto-tables is that the developer needs to describe the model (database table) and link the model fields / properties to the table columns and form fields. For example:

Let's say we have a table in MySQL:

CREATE TABLE IF NOT EXISTS `tasks` (     `id` int(10) unsigned NOT NULL AUTO_INCREMENT,     `userId` int NOT NULL,     `title` varchar(300) COLLATE utf8_unicode_ci,     `description` varchar(1000) COLLATE utf8_unicode_ci,     `startDate` date,     `endDate` date,     `status` int NOT NULL DEFAULT '0',     PRIMARY KEY (`id`),     KEY `userId` (`userId`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci; 


The model will be:

 <?php class Tasks extends Kwf_Model_Db {    protected $_table = 'tasks'; } 


To display the task list, you need to create a controller (more precisely, AutoGrid Controller):

 <?php class TasksController extends Kwf_Controller_Action_Auto_Grid {    protected $_modelName = 'Tasks';    protected $_defaultOrder = array('field' => 'id', 'direction' => 'DESC');    protected $_paging = 30;    protected $_buttons = array('add');    public function indexAction()    {        $this->view->ext('Tasks');    }       protected function _initColumns()    {        $this->_columns->add(new Kwf_Grid_Column('title', trl('Title'), 200));        $this->_columns->add(new Kwf_Grid_Column_Date('endDate', trl('End Date'), 100))->setRenderer('checkDate');    }   } 


Here, the class properties describe the behavior: the number of elements per page (yes, paginated output is supported out of the box!), The data model, sorting of the selection (optionally), and possible actions. Of the actions here, only adding tasks is indicated, but deleting, saving and exporting to Excel / PDF is also available.

Now you need to download tasks only for a specific logged in user. For this, there are global variables in the framework. Now we can easily set the conditions for selecting tasks in the controller:

  protected function _getWhere()    {        $users = Kwf_Registry::get('userModel');               $ret = parent::_getWhere();               $ret['status = ?'] = 0;        $ret['userId = ?'] = $users->getAuthedUserId();        return $ret;    } 


Great, now the user sees only his active tasks!

So, it remains to add the creation and modification of tasks. For this we need another controller (AutoForm Controller):

 <?php class TaskController extends Kwf_Controller_Action_Auto_Form {    protected $_modelName = 'Tasks';    protected $_permissions = array('save', 'add');    protected $_buttons = array('save');    protected function _initFields()    {        $this->_form->add(new Kwf_Form_Field_TextField('title', trl('Title')))        ->setWidth(400)        ->setAllowBlank(false);               $this->_form->add(new Kwf_Form_Field_DateField('startDate', trl('Start Date')));        $this->_form->add(new Kwf_Form_Field_DateField('endDate', trl('End Date')));               $this->_form->add(new Kwf_Form_Field_TextArea('description', trl('Description')))        ->setHeight(70)        ->setWidth(400);                       $this->_form->add(new Kwf_Form_Field_Checkbox('status', trl('Done')));    }   } 


The class properties describe the behavior of the form, and the data connections with the fields are specified in the function initFields (). But there is a small problem: the userId field is in no way associated with the current user and is not tied to the form fields. So far ... To free the user from the work of choosing himself as the owner of the task, you need to add a trigger - a function that is triggered during the creation of a new record:

  protected function _beforeInsert(Kwf_Model_Row_Interface $row)    {        $users = Kwf_Registry::get('userModel');               if ($row->startDate == NULL)        {            $row->startDate = new DateTime ();        }        $row->userId = $users->getAuthedUserId();        $row->status = 0;    } 


Other triggers are also available in KWF: to modify and delete data.

Now somehow you need to connect both of these controllers to each other. The code of both controllers generates ExtJS forms. Consequently, we will link them in JS code:

 var Tasks = Ext.extend(Ext.Panel, {       initComponent : function(test)       {       var form = new Kwf.Auto.FormPanel({                                         controllerUrl : '/task',                                         region : 'center'                                         });                    var grid = new Kwf.Auto.GridPanel({                                         controllerUrl : '/tasks',                                         region : 'west',                                         width : 400,                                         resizable : true,                                         split : true,                                         collapsible : true,                                         title : trl('Tasks'),                                         bindings: [{                                                    queryParam: 'id',                                                    item: form                                                    }]                                         });       this.layout = 'border';       this.items = [grid, {                     layout: 'border',                     region: 'center',                     items: [form]                     }];       Tasks.superclass.initComponent.call(this);    } }); 


Anyone who has already worked with ExtJS, this code will probably seem very familiar =)

It remains only to determine the access point (input) to these controllers. To do this, KWF has a separate predefined class Kwf_Acl:

 <?php class Acl extends Kwf_Acl {    public function __construct()    {        parent::__construct();        $this->remove('default_index');        $this->addResource(new Kwf_Acl_Resource_MenuUrl('default_tasks', array('text'=>trl('Tasks'), 'icon'=>'time.png'), '/tasks'));        $this->addResource(new Zend_Acl_Resource('default_task'), 'default_tasks');        $this->addRole(new Zend_Acl_Role('user'));        $this->allow('user', 'default_tasks');        $this->allow('admin', 'default_tasks');        $this->allow('admin', 'default_index');    } } 


That's all! The next TO-DO list is ready! In addition, it is multi-user and, if necessary, tasks such as exporting a list to Excel / PDF, filtering and sorting lists are added a maximum of 5 minutes!



The full application code is available here: github.com/kronik/kwf-app-demo

Let's create more useful and convenient tools! =)

From myself I also want to add that now the web application on KFW has been successfully developed and implemented in a large Russian company. User feedback is extremely positive.

PS1: Trying to figure out and show the technology is already punishable by losing karma? Let's better discuss in the comments :)

PS 2: The team is looking for developers PHP, ExtJS, MySQL, who want to learn more about the Koala Framework and are ready to help (not for free of course) in the development of the national specialized CIS. All questions can be answered in the comments or in a personal.

Source: https://habr.com/ru/post/228901/


All Articles