About this example : PartListManager MVC Assembly source Code

Controller

controllers/examples/db/PartListManager.php

<?php
namespace controllers\examples\db;

// Basic Framework classes usage
use framework\Controller;
use framework\Model;
use framework\View;

// Use related applications View and Model
use views\examples\db\PartListManager as PartListManagerView;
use models\examples\db\PartListManager as PartListManagerModel;

// Using some common and shared application controllers
use controllers\examples\cms\NavigationBar;

// Using some framework components
use framework\components\bootstrap\PaginatorBootstrap;
use framework\components\Searcher;
use framework\components\DataRepeater;
use framework\components\bootstrap\SorterBootstrap;

/**
 * Class PartListManager
 * Manages Parts.
 *
 * @package controllers\examples\db
 */
class PartListManager extends Controller
{

    public function __construct(View $view=null, Model $model=null)
    {
        $this->view = empty($view) ? $this->getView() : $view;
        $this->model = empty($model) ? $this->getModel() : $model;
        parent::__construct($this->view,$this->model);
    }

    /**
     * Helper method to associate View
     * @return PartListManagerView
     */
    public function getView()
    {
        $view = new PartListManagerView();
        return $view;
    }

    /**
     * Helper Method to associate Model
     * @return PartListManagerModel
     */
    public function getModel()
    {
        $model = new PartListManagerModel();
        return $model;
    }

    /**
     * @param $parameters
     * @return void
     * @throws \framework\exceptions\NotInitializedViewException
     * @throws \framework\exceptions\VariableNotFoundException
     */
    public function autorun($parameters = null)
    {
        parent::autorun($parameters);

        // Builds child controller
        $navigation = $this->makeNavigation();

        // Build components making care to use the following
        // components creation order:
        // 1st Searcher
        // 2nd Sorters
        // 3rd Pagination
        // 4th Selection
        // (Note: order is similar to SQL query procesor execution order)

        $searcher = $this->makeSearcher();

        $sorterPartCode = $this->view->makeSoterPartCode($this->model);
        $sorterDescription = $this->view->makeSoterDescription($this->model);
        $sorterSource = $this->view->makeSoterSource($this->model);
        $sorterSourceLeadTime = $this->view->makeSoterSourceLeadTime($this->model);
        $sorterPartTypeCode =$this->view->makeSoterPartTypeCode($this->model);
        $soterMeasurementUnitCode = $this->view->makeSoterMeasurementUnitCode($this->model);
        $sorterPartCategoryCode = $this->view->makeSoterPartCategoryCode($this->model);
        $sorterWastage = $this->view->makeSoterWastage($this->model);
        $sorterBomLevels = $this->view->makeSoterBomLevels($this->model);

        $pagination = $this->makePagination();
        $parts = $this->makePartsWithDataRepeater();

        // Binding child controller and components instances to the main View
        $this->bindController($navigation);

        // Binding components instances to the main View (by
        // using the same creation order)
        $this->bindComponent($searcher,false);

        $this->bindComponent($sorterPartCode);
        $this->bindComponent($sorterDescription);
        $this->bindComponent($sorterSource);
        $this->bindComponent($sorterSourceLeadTime);
        $this->bindComponent($sorterPartTypeCode);
        $this->bindComponent($soterMeasurementUnitCode);
        $this->bindComponent($sorterPartCategoryCode);
        $this->bindComponent($sorterWastage);
        $this->bindComponent($sorterBomLevels);

        $this->bindComponent($pagination);

        $this->bindComponent($parts);

        $this->setAsObserver("parts", false, "myCallBack()");

    }

    /**
     * Makes a Searcher Component
     * The componponent uses new View witch uses itself
     * an external template design (part_search_form.html.tpl).
     *
     * @return Searcher We can use it to binding it {Searcher:ricerca}
     *                  placeholder of the main View
     */
    protected function makeSearcher()
    {
        // Creates a searcher by using a new View and an external template
        // for its search form HTML design.
        $view = new View("examples/db/part_search_form");
        $searcher = new Searcher($view, $this->model);

        // Set component name
        $searcher->setName("ricerca");

        // Creates filters:
        // parameters: table field, form input, operators into query, data type
        $searcher->addFilter("part_code","s_part_code","=","string");
        $searcher->addFilter("description","s_description","LIKE","string");
        $searcher->addFilter("source","s_source","=","string");

        // Sets form name (tpl variable)
        $searcher->setFormName("search_form", $searcher->getName());

        // Sets component submit and reset inputs name (tpl variables)
        $searcher->setResetButton("search_reset", "Reset");
        $searcher->setSubmitButton("search_submit","Cerca");

        // Init component ( do the search job for you if :) )
        $searcher->init($this->model,$view);
        return $searcher;
    }


    /**
     * Makes pagination by using PaginatorBootstrap component
     * @return PaginatorBootstrap
     */
    protected function makePagination(){
        $paginator = new PaginatorBootstrap();
        $paginator->setName("Bottom");
        $paginator->resultPerPage = 4;
        $paginator->setModel($this->model);
        $paginator->buildPagination();
        $this->model->sql = $paginator->query;
        return $paginator;
    }

    /**
     * Makes part list by using a DataRepeater
     * @return DataRepeater The DataRepeater to use form binging it
     *                      to Parts Block of the main View
     */
    protected function makePartsWithDataRepeater()
    {
        $parts = new  DataRepeater($this->view,$this->model,"Parts",null);
        return $parts;

    }

    /**
     * Makes the navigation bars by using a shared application controller
     * @return NavigationBar The navigation bar
     */
    protected function makeNavigation(){
        $navigation = new NavigationBar();
        return $navigation;
    }

}

Model

models/examples/db/PartListManager.php

<?php
/**
 * Class PartListManager
 *
 * {ModelResponsability}
 *
 * @package models\examples\db
 * @category Application Model
 * @author  {AuthorName} - {AuthorEmail}
*/
namespace models\examples\db;

use framework\Model;

/**
 * Class PartListManager
 * Part List Manager Model
 *
 * @package models\examples\db
 */
class PartListManager extends Model
{
    public function __construct()
    {
        parent::__construct();
        $this->sql =
<<<SQL
          SELECT  
              part_code, 
              description, 
              source, 
              source_lead_time, 
              measurement_unit_code, 
              part_type_code, 
              part_category_code, 
              wastage, 
              bom_levels 
            FROM 
              part
            WHERE 1 = 1
SQL;
        // Also you can use a statement like this to enveloping sql:
        // a) $this->sql = "SELECT t.* FROM part t";
        //
        // Warning:
        //
        // Do not use "SELECT * FROM part" because frameworks components
        // and SQL itself cannot build SQL subquery that use the same
        // table
        //
        // Use $this->envelopeSql() to enveloping automatically
        // your sql.
        $this->envelopeSql();
        $this->updateResultSet();
    }
}

View

views/examples/db/PartListManager.php

<?php

namespace views\examples\db;

use framework\View;
use framework\components\bootstrap\SorterBootstrap;
use models\examples\db\PartListManager as PartListManagerModel;

/**
 * Class PartListManager
 * Part List Manager View
 *
 * @package views\examples\db
 */
class PartListManager extends View
{
    public function __construct($tplName = null)
    {
        if (empty($tplName))
            $tplName = "/examples/db/part_list_manager";
        parent::__construct($tplName);
    }

    public function setBlockParts(\mysqli_result $resultset){
        $this->openBlock("Parts");
        while ($part = $resultset->fetch_object()) {
            $this->setVar("part_code",$part->part_code);
            $this->setVar("description",$part->description);
            $this->setVar("source",$part->source);
            $this->setVar("source_lead_time",$part->source_lead_time);
            $this->setVar("measurement_unit_code",$part->measurement_unit_code);
            $this->setVar("part_type_code",$part->part_type_code);
            $this->setVar("part_category_code",$part->part_category_code);
            $this->setVar("wastage",$part->wastage);
            $this->setVar("bom_levels",$part->bom_levels);
            $this->parseCurrentBlock();
        }
        $this->setBlock();
    }

    /**
     * Makes sorter for part_code field
     * @return SorterBootstrap
     */
    public function makeSoterPartCode(PartListManagerModel $model)
    {

        $sorterPartCode = new SorterBootstrap();
        $sorterPartCode->setName("part_code");
        $sorterPartCode->field = "part_code";
        $sorterPartCode->caption = "{RES:part_code}";
        $sorterPartCode->init($model);
        return $sorterPartCode;
    }

    /**
     * Make sorter for description field
     * @return SorterBootstrap
     */
    public function makeSoterDescription(PartListManagerModel $model)
    {
        $sorterDescription = new SorterBootstrap();
        $sorterDescription->setName("description");
        $sorterDescription->field = "description";
        $sorterDescription->caption = "{RES:description}";
        $sorterDescription->init($model);
        return $sorterDescription;
    }

    /**
     * Make sorter for source field
     * @return SorterBootstrap
     */
    public function makeSoterSource(PartListManagerModel $model)
    {
        $sorterSource = new SorterBootstrap();
        $sorterSource->setName("source");
        $sorterSource->field = "source";
        $sorterSource->caption = "{RES:source}";
        $sorterSource->init($model);
        return $sorterSource;
    }

    /**
     * Make sorte for source_lead_time field
     * @return SorterBootstrap
     */
    public function makeSoterSourceLeadTime(PartListManagerModel $model)
    {
        $sorterSourceLeadTime = new SorterBootstrap();
        $sorterSourceLeadTime->setName("source_lead_time");
        $sorterSourceLeadTime->field = "source_lead_time";
        $sorterSourceLeadTime->caption = "{RES:source_lead_time}";
        $sorterSourceLeadTime->init($model);
        return $sorterSourceLeadTime;
    }

    /**
     * Make sorte for part_type_code field
     * @return SorterBootstrap
     */
    public function makeSoterPartTypeCode(PartListManagerModel $model)
    {
        $sorterPartTypeCode = new SorterBootstrap();
        $sorterPartTypeCode->setName("part_type_code");
        $sorterPartTypeCode->field = "part_type_code";
        $sorterPartTypeCode->caption = "{RES:part_type_code}";
        $sorterPartTypeCode->init($model);
        return $sorterPartTypeCode;
    }

    /**
     * Make sorte for measurement_unit_code field
     * @return SorterBootstrap
     */
    public function makeSoterMeasurementUnitCode(PartListManagerModel $model)
    {
        $sorterMeasurementUnitCode = new SorterBootstrap();
        $sorterMeasurementUnitCode->setName("measurement_unit_code");
        $sorterMeasurementUnitCode->field = "measurement_unit_code";
        $sorterMeasurementUnitCode->caption = "{RES:measurement_unit_code}";
        $sorterMeasurementUnitCode->init($model);
        return $sorterMeasurementUnitCode;
    }

    /**
     * Make sorte for part_category_code field
     * @return SorterBootstrap
     */
    public function makeSoterPartCategoryCode(PartListManagerModel $model)
    {
        $sorterPartCategoryCode = new SorterBootstrap();
        $sorterPartCategoryCode->setName("part_category_code");
        $sorterPartCategoryCode->field = "part_category_code";
        $sorterPartCategoryCode->caption = "{RES:part_category_code}";
        $sorterPartCategoryCode->init($model);
        return $sorterPartCategoryCode;
    }

    /**
     * Make sorte for wastage field
     * @return SorterBootstrap
     */
    public function makeSoterWastage(PartListManagerModel $model)
    {
        $sorterWastage = new SorterBootstrap();
        $sorterWastage->setName("wastage");
        $sorterWastage->field = "wastage";
        $sorterWastage->caption = "{RES:wastage}";
        $sorterWastage->init($model);
        return $sorterWastage;
    }

    /**
     * Make sorte for bom_levels field
     * @return SorterBootstrap
     */
    public function makeSoterBomLevels(PartListManagerModel $model)
    {
        $sorterBomLevels = new SorterBootstrap();
        $sorterBomLevels->setName("bom_levels");
        $sorterBomLevels->field = "bom_levels";
        $sorterBomLevels->caption = "{RES:bom_levels}";
        $sorterBomLevels->init($model);
        return $sorterBomLevels;
    }

}

HTML Template

templates/examples/db/part_list_manager.html.tpl

<!DOCTYPE html>
<html>
<head>
    <title>Listing, paginating, sorting and searching</title>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <!-- Bootstrap core CSS -->
    <link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.4/css/bootstrap.min.css" rel="stylesheet"
          media="screen">

    <!-- HTML5 shim and Respond.js IE8 support of HTML5 elements and media queries -->
    <!--[if lt IE 9]>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/html5shiv/3.7.2/html5shiv.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/respond.js/1.4.2/respond.js"></script>
    <![endif]-->
</head>
<body>
{Controller:examples\cms\NavigationBar}
<div class="container">
    <h1>{RES:PartsList}</h1>

    {Searcher:ricerca}
    <a href="part_record/add/new" class="btn btn-info"><span
                class="glyphicon glyphicon-plus-sign"></span> {RES:AddNewPart}</a>
    <div class="table table-responsive">
        <table class="table table-bordered" id="parts">
            <thead>
            <tr>
                <th>{SorterBootstrap:part_code}</th>
                <th>{SorterBootstrap:description}</th>
                <th>{SorterBootstrap:source}</th>
                <th>{SorterBootstrap:source_lead_time}</th>
                <th>{SorterBootstrap:measurement_unit_code}</th>
                <th>{SorterBootstrap:part_type_code}</th>
                <th>{SorterBootstrap:part_category_code}</th>
                <th>{SorterBootstrap:wastage}</th>
                <th>{SorterBootstrap:bom_levels}</th>
            </tr>
            </thead>

            <tbody>
            <!-- BEGIN Parts -->
            <tr>
                <td><a href="part_record/open/{part_code}">{part_code}</a></td>
                <td>{description}</td>
                <td>{source}</td>
                <td>{source_lead_time}</td>
                <td>{measurement_unit_code}</td>
                <td>{part_type_code}</td>
                <td>{part_category_code}</td>
                <td>{wastage}</td>
                <td>{bom_levels}</td>
            </tr>
            <!-- END Parts -->
            </tbody>

            <tfoot>
            <tr>
                <td class="text-center" colspan="9">{PaginatorBootstrap:Bottom}</td>
            </tr>
            </tfoot>
        </table>

        <hr>
        <a href="https://www.webmvcframework.com/webmvc/examples/about/example/partListManager" class="btn btn-info">Mostra codice sorgente</a>
        <a href="https://www.webmvcframework.com/webmvc/examples/db/part_list_manager/" class="btn btn-success">Mostra il template</a>
        <a href="https://www.webmvcframework.com/webmvc/examples/db/part_list_manager" class="btn btn-success">Ricarica</a>
        <a href="https://www.webmvcframework.com/webmvc/examples/" class="btn btn-primary">Indice degli esempi</a>

    </div>

</div>

<!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>

<!-- Include all compiled plugins (below), or include individual files as needed -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.4/js/bootstrap.min.js"></script>
<script>
    function myCallBack() {
        console.log("Call back from custom call back");
    }

    function observerCallBack() {
        console.log("Call back from observer");
    }
</script>
</body>
</html> 


Back