Lançado Adianti Framework 7.6!
Clique aqui para saber mais
menu

Adianti Solutions

API

Adianti, Framework, PHP, MVC, Active record, Front controller, IDE, RAD, Web, multiplataforma, geração de código, desenvolvimento rápido, relatórios, formulários, listagens, datagrids, gráficos, banco de dados, padrões de projeto, design patterns API do Adianti Framework.
API Docs
code
Selecione a classe

Source for file TKanban.php

Documentation is available at TKanban.php

  1. <?php
  2. namespace Adianti\Widget\Util;
  3.  
  4. use Adianti\Database\TTransaction;
  5. use Adianti\Widget\Base\TScript;
  6. use Adianti\Widget\Base\TElement;
  7. use Adianti\Control\TAction;
  8. use Adianti\Util\AdiantiTemplateHandler;
  9. use Adianti\Widget\Template\THtmlRenderer;
  10.  
  11. use stdClass;
  12. use ApplicationTranslator;
  13.  
  14. /**
  15.  * Kanban
  16.  *
  17.  * @version    7.4
  18.  * @package    widget
  19.  * @subpackage util
  20.  * @author     Artur Comunello
  21.  * @author     Pablo Dall'Oglio
  22.  * @copyright  Copyright (c) 2006 Adianti Solutions Ltd. (http://www.adianti.com.br)
  23.  * @license    http://www.adianti.com.br/framework-license
  24.  */
  25. class TKanban extends TElement
  26. {
  27.     protected $kanban;
  28.     protected $items;
  29.     protected $stages;
  30.     protected $itemActions;
  31.     protected $stageActions;
  32.     protected $stageShortcuts;
  33.     protected $itemDropAction;
  34.     protected $stageDropAction;
  35.     protected $templatePath;
  36.     protected $itemTemplate;
  37.     protected $itemDatabase;
  38.     
  39.     /**
  40.      * Class Constructor
  41.      */
  42.     public function __construct()
  43.     {
  44.         parent::__construct('div');
  45.         $this->items          = [];
  46.         $this->stages         = [];
  47.         $this->itemActions    = [];
  48.         $this->stageActions   = [];
  49.         $this->stageShortcuts = [];
  50.         
  51.         $this->kanban                 = new TElement('div');
  52.         $this->kanban->{'id'}         'tkanban_' mt_rand(10000000001999999999);
  53.         $this->kanban->{'item_class''kanban-item-wrapper';
  54.         $this->kanban->{'class'}      'kanban-board';
  55.     }
  56.     
  57.     /**
  58.      * Add stage to kanban board
  59.      * @param  $id     Stage id
  60.      * @param  $title  Stage title
  61.      * @param  $color  Stage color
  62.      * @param  $object Stage data object
  63.      */
  64.     public function addStage($id$title$object null$color null)
  65.     {
  66.         if (is_null($object))
  67.         {
  68.             $object new stdClass;
  69.         }
  70.         
  71.         $stage             new stdClass;
  72.         $stage->{'id'}     $id;
  73.         $stage->{'title'}  $title;
  74.         $stage->{'object'$object;
  75.         $stage->{'color'}  $color;
  76.         
  77.         $this->stages[$stage;
  78.     }
  79.     
  80.     /**
  81.      * Add item to stage
  82.      * @param  $id       Item id
  83.      * @param  $stage_id Stage id
  84.      * @param  $title    Item title
  85.      * @param  $content  Item content
  86.      * @param  $color    Item color
  87.      * @param  $object   Item data object
  88.      */
  89.     public function addItem($id$stage_id$title$content$color null$object null)
  90.     {
  91.         if (is_null($object))
  92.         {
  93.             $object new stdClass;
  94.             $object->{'title'$title;
  95.             $object->{'content'$content;
  96.             $object->{'color'$color;
  97.         }
  98.  
  99.         if (empty($object->{'id'}))
  100.         {
  101.             $object->{'id'$id;
  102.         }
  103.  
  104.         $item              new stdClass;
  105.         $item->{'id'}      $id;
  106.         $item->{'title'}   $title;
  107.         $item->{'color'}   $color;
  108.         $item->{'content'$content;
  109.         $item->{'object'}  $object;
  110.         
  111.         $this->items[$stage_id][$item;
  112.     }
  113.     
  114.     /**
  115.      * Set kanban item template for rendering
  116.      * @param  $path   Template path
  117.      */
  118.     public function setTemplatePath($path)
  119.     {
  120.         $this->templatePath $path;
  121.     }
  122.     
  123.     /**
  124.      * Set card item template for rendering
  125.      * @param  $template   Template content
  126.      */
  127.     public function setItemTemplate($template)
  128.     {
  129.         $this->itemTemplate $template;
  130.     }
  131.     
  132.     /**
  133.      * Set item min database
  134.      * @param $database min database
  135.      */
  136.     public function setItemDatabase($database)
  137.     {
  138.         $this->itemDatabase $database;
  139.     }
  140.     
  141.     /**
  142.      * Set item drop action
  143.      * @param  $action  TAction object
  144.      */
  145.     public function setItemDropAction(TAction $action)
  146.     {
  147.         if ($action->isStatic())
  148.         {
  149.             $this->itemDropAction $action;
  150.         }
  151.         else
  152.         {
  153.             $string_action $action->toString();
  154.             throw new Exception(AdiantiCoreTranslator::translate('Action (^1) must be static to be used in ^2'$string_action__METHOD__));
  155.         }
  156.     }
  157.     
  158.     /**
  159.      * Set stage drop action
  160.      * @param  $action  TAction object
  161.      */
  162.     public function setStageDropAction(TAction $action)
  163.     {
  164.         if ($action->isStatic())
  165.         {
  166.             $this->stageDropAction $action;
  167.         }
  168.         else
  169.         {
  170.             $string_action $action->toString();
  171.             throw new Exception(AdiantiCoreTranslator::translate('Action (^1) must be static to be used in ^2'$string_action__METHOD__));
  172.         }
  173.     }
  174.     
  175.     /**
  176.      * Add item action
  177.      * @param  $label             Action label
  178.      * @param  $action            Action callback (TAction)
  179.      * @param  $icon              Action icon
  180.      * @param  $display_condition Display condition
  181.      */
  182.     public function addItemAction($labelTAction $action$icon NULL$display_condition NULL)
  183.     {
  184.         $itemAction            new stdClass;
  185.         $itemAction->label     $label;
  186.         $itemAction->action    $action;
  187.         $itemAction->icon      $icon;
  188.         $itemAction->condition $display_condition;
  189.         
  190.         $this->itemActions[]   $itemAction;
  191.     }
  192.     
  193.     /**
  194.      * Add stage action
  195.      * @param  $label             Action label
  196.      * @param  $action            Action callback (TAction)
  197.      * @param  $icon              Action icon
  198.      */
  199.     public function addStageAction($labelTAction $action$icon NULL$display_condition NULL)
  200.     {
  201.         $stageAction            new stdClass;
  202.         $stageAction->label     $label;
  203.         $stageAction->action    $action;
  204.         $stageAction->icon      $icon;
  205.         $stageAction->condition $display_condition;
  206.         
  207.         $this->stageActions[$stageAction;
  208.     }
  209.     
  210.     /**
  211.      * Add stage shortcut
  212.      * @param  $label             Action label
  213.      * @param  $action            Action callback (TAction)
  214.      * @param  $icon              Action icon
  215.      */
  216.     public function addStageShortcut($labelTAction $action$icon NULL)
  217.     {
  218.         $stageAction          new stdClass;
  219.         $stageAction->label   $label;
  220.         $stageAction->action  $action;
  221.         $stageAction->icon    $icon;
  222.         
  223.         $this->stageShortcuts[$stageAction;
  224.     }
  225.     
  226.     /**
  227.      * Render stage items
  228.      */
  229.     private function renderStageItems($stage)
  230.     {
  231.         $itemSortable               new TElement('div');
  232.         $itemSortable->{'class'}    'kanban-item-sortable ' $this->kanban->item_class;
  233.         $itemSortable->{'stage_id'$stage->{'stage_id'};
  234.         
  235.         if (!empty($this->itemDatabase))
  236.         {
  237.             TTransaction::open($this->itemDatabase);
  238.         }
  239.         
  240.         if (!empty($this->items[$stage->{'stage_id'}]))
  241.         {
  242.             foreach ($this->items[$stage->{'stage_id'}as $key => $item)
  243.             {
  244.                 $itemSortable->add(self::renderItem($item));
  245.             }
  246.         }
  247.         
  248.         if (!empty($this->itemDatabase))
  249.         {
  250.             TTransaction::close();
  251.         }
  252.         
  253.         $stage->add($itemSortable);
  254.     }
  255.     
  256.     /**
  257.      * Render item
  258.      */
  259.     private function renderItem($item)
  260.     {
  261.         if (!empty($this->templatePath))
  262.         {
  263.             $html new THtmlRenderer($this->templatePath);
  264.             $html->enableSection('main');
  265.             $html->enableTranslation();
  266.             $html AdiantiTemplateHandler::replace($html->getContents()$item->{'object'});
  267.             return $html;
  268.         }
  269.         
  270.         $item_wrapper              new TElement('div');
  271.         $item_wrapper->{'item_id'$item->id;
  272.         $item_wrapper->{'class'}   'kanban-item';
  273.         
  274.         if (!empty($item->color))
  275.         {
  276.             $item_wrapper->{'style'}   'border-top: 3px solid '.$item->color;
  277.         }
  278.         
  279.         $item_title new TElement('div');
  280.         $item_title->{'class''kanban-item-title';
  281.         $item_title->add(AdiantiTemplateHandler::replace($item->{'title'}$item->{'object'}));
  282.         
  283.         $item_content new TElement('div');
  284.         $item_content->{'class''kanban-item-content';
  285.         $item_content->add(AdiantiTemplateHandler::replace($item->{'content'}$item->{'object'}));
  286.         
  287.         if (!empty($this->itemTemplate))
  288.         {
  289.             $item_content new TElement('div');
  290.             $item_content->{'class''kanban-item-content';
  291.             $item_template ApplicationTranslator::translateTemplate($this->itemTemplate);
  292.             $item_template AdiantiTemplateHandler::replace($item_template$item);
  293.             $item_content->add($item_template);
  294.         }
  295.         
  296.         $item_wrapper->add($item_title);
  297.         $item_wrapper->add($item_content);
  298.  
  299.         if (!empty($this->itemActions))
  300.         {
  301.             $item_wrapper->add($this->renderItemActions($item->id$item->object));
  302.         }
  303.  
  304.         return $item_wrapper;
  305.     }
  306.     
  307.     /**
  308.      * Render stages
  309.      */
  310.     private function renderStages()
  311.     {
  312.         foreach ($this->stages as $key => $stage)
  313.         {
  314.             $title            new TElement('span');
  315.             $title->{'class''kanban-title';
  316.             $title->add(AdiantiTemplateHandler::replace($stage->{'title'}$stage->{'object'}));
  317.  
  318.             $stageDiv                new TElement('div');
  319.             $stageDiv->{'stage_id'}  $stage->{'id'};
  320.             $stageDiv->{'class'}     'kanban-stage';
  321.             if (!empty($stage->{'color'}))
  322.             {
  323.                 $stageDiv->{'style''background:'.$stage->{'color'};
  324.             }
  325.             $stageDiv->add($title);
  326.  
  327.             if (!empty($this->stageActions))
  328.             {
  329.                 $title $stageDiv->children[0];
  330.                 $title->add($this->renderStageActions$stage->{'id'}$stage ));
  331.             }
  332.             
  333.             $this->renderStageItems($stageDiv);
  334.             $this->kanban->add($stageDiv);
  335.             $stageDiv->add($this->renderStageShortcuts$stage ));
  336.         }
  337.     }
  338.     
  339.     /**
  340.      * Render item actions
  341.      */
  342.     private function renderItemActions($itemId$object NULL)
  343.     {
  344.         $div            new TElement('div');
  345.         $div->{'class''kanban-item-actions';
  346.         
  347.         foreach ($this->itemActions as $key => $actionTemplate)
  348.         {
  349.             $itemAction $actionTemplate->action->prepare($object);
  350.             
  351.             if (empty($actionTemplate->conditionOR call_user_func($actionTemplate->condition$object))
  352.             {
  353.                 $itemAction->setParameter('id'$itemId);
  354.                 $itemAction->setParameter('key'$itemId);
  355.                 $url $itemAction->serialize();
  356.                 
  357.                 $icon                new TImage($actionTemplate->icon);
  358.                 $icon->{'style'}    .= ';cursor:pointer;margin-right:4px;';
  359.                 $icon->{'title'}     $actionTemplate->label;
  360.                 $icon->{'generator''adianti';
  361.                 $icon->{'href'}      $url;
  362.                 
  363.                 $div->add($icon);
  364.             }
  365.         }
  366.         
  367.         return $div;
  368.     }
  369.     
  370.     /**
  371.      * Render stage actions
  372.      */
  373.     private function renderStageActions($stage_id$stage)
  374.     {
  375.         $icon                  new TImage('mi:more_vert');
  376.         $icon->{'data-toggle''dropdown';
  377.  
  378.         $ul new TElement('ul');
  379.         $ul->{'class''dropdown-menu pull-right';
  380.         
  381.         foreach ($this->stageActions as $key => $stageActionTemplate)
  382.         {
  383.             $stageAction $stageActionTemplate->action->prepare($stage);
  384.             
  385.             if (empty($stageActionTemplate->conditionOR call_user_func($stageActionTemplate->condition$stage))
  386.             {
  387.                 $stageAction->setParameter('id',  $stage_id);
  388.                 $stageAction->setParameter('key'$stage_id);
  389.                 $url $stageAction->serialize();
  390.                 
  391.                 $action                new TElement('a');
  392.                 $action->{'generator''adianti';
  393.                 $action->{'href'}      $url;
  394.                 if (!empty($stageActionTemplate->icon))
  395.                 {
  396.                     $action->add(new TImage($stageActionTemplate->icon));
  397.                 }
  398.                 $action->add($stageActionTemplate->label);
  399.                 
  400.                 $li new TElement('li');
  401.                 $li->add($action);
  402.                 $ul->add($li);
  403.             }
  404.         }
  405.         
  406.         $dropWrapper new TElement('div');
  407.         $dropWrapper->{'style''cursor:pointer;';
  408.         $dropWrapper->{'class''btn-group user-helper-dropdown';
  409.         $dropWrapper->add($icon);
  410.         $dropWrapper->add($ul);
  411.  
  412.         $stageActions new TElement('span');
  413.         $stageActions->{'style''float: right;';
  414.         $stageActions->{'class''kanban-stage-actions';
  415.         $stageActions->add($dropWrapper);
  416.         
  417.         return $stageActions;
  418.     }
  419.     
  420.     /**
  421.      * Render stage shortcuts
  422.      */
  423.     private function renderStageShortcuts($stage)
  424.     {
  425.         $actions_wrapper new TElement('div');
  426.         $actions_wrapper->{'class''kanban-shortcuts';
  427.         
  428.         foreach ($this->stageShortcuts as $key => $stageActionTemplate)
  429.         {
  430.             $stageAction $stageActionTemplate->action->prepare($stage);
  431.             
  432.             $stageAction->setParameter('id',  $stage->{'id'});
  433.             $stageAction->setParameter('key'$stage->{'id'});
  434.             $url $stageAction->serialize();
  435.             
  436.             $action                new TElement('a');
  437.             $action->{'generator''adianti';
  438.             $action->{'href'}      $url;
  439.             if (!empty($stageAction->icon))
  440.             {
  441.                 $action->add(new TImage($stageAction->icon));
  442.             }
  443.             $action->add($stageActionTemplate->label);
  444.             
  445.             $actions_wrapper->add($action);
  446.         }
  447.         
  448.         return $actions_wrapper;
  449.     }
  450.     
  451.     /**
  452.      * Show kanban
  453.      */
  454.     public function show()
  455.     {
  456.         $this->renderStages();
  457.         $this->add($this->kanban);
  458.         $this->{'style'.= ';overflow-x:auto';
  459.         
  460.         if (!empty($this->stageDropAction))
  461.         {
  462.             $stage_drop_action_string $this->stageDropAction->serialize();
  463.             TScript::create("kanban_start_board('{$this->kanban->id}', '{$stage_drop_action_string}');");
  464.         }
  465.  
  466.         if (!empty($this->itemDropAction))
  467.         {
  468.             $item_drop_action_string $this->itemDropAction->serialize();
  469.             TScript::create("kanban_start_item('{$this->kanban->item_class}', '{$item_drop_action_string}');");
  470.         }
  471.  
  472.         parent::show();
  473.     }
  474. }