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 TCardView.php

Documentation is available at TCardView.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\Form\TField;
  10. use Adianti\Widget\Template\THtmlRenderer;
  11. use Adianti\Widget\Form\TButton;
  12.  
  13. use stdClass;
  14. use ApplicationTranslator;
  15.  
  16. /**
  17.  * Card
  18.  *
  19.  * @version    7.4
  20.  * @package    widget
  21.  * @subpackage util
  22.  * @author     Pablo Dall'Oglio
  23.  * @copyright  Copyright (c) 2006 Adianti Solutions Ltd. (http://www.adianti.com.br)
  24.  * @license    http://www.adianti.com.br/framework-license
  25.  */
  26. class TCardView extends TElement
  27. {
  28.     protected $items;
  29.     protected $itemActions;
  30.     protected $templatePath;
  31.     protected $itemTemplate;
  32.     protected $titleTemplate;
  33.     protected $useButton;
  34.     protected $titleField;
  35.     protected $contentField;
  36.     protected $colorField;
  37.     protected $searchAttributes;
  38.     protected $itemHeight;
  39.     protected $contentHeight;
  40.     protected $itemDatabase;
  41.     
  42.     /**
  43.      * Class Constructor
  44.      */
  45.     public function __construct()
  46.     {
  47.         parent::__construct('div');
  48.         $this->items          = [];
  49.         $this->itemActions    = [];
  50.         $this->useButton      = FALSE;
  51.         $this->searchAttributes = [];
  52.         $this->itemHeight     = NULL;
  53.         $this->contentHeight  = NULL;
  54.         $this->{'id'}         'tcard_' mt_rand(10000000001999999999);
  55.         $this->{'class'}      'card-wrapper';
  56.     }
  57.     
  58.     /**
  59.      * Set item min height
  60.      * @param $height min height
  61.      */
  62.     public function setItemHeight($height)
  63.     {
  64.         $this->itemHeight $height;
  65.     }
  66.     
  67.     /**
  68.      * Set item min database
  69.      * @param $database min database
  70.      */
  71.     public function setItemDatabase($database)
  72.     {
  73.         $this->itemDatabase $database;
  74.     }
  75.     
  76.     /**
  77.      * Set content min height
  78.      * @param $height min height
  79.      */
  80.     public function setContentHeight($height)
  81.     {
  82.         $this->contentHeight $height;
  83.     }
  84.     
  85.     /**
  86.      * Set title attribute
  87.      * @param $field attribute name
  88.      */
  89.     public function setTitleAttribute($field)
  90.     {
  91.         $this->titleField $field;
  92.     }
  93.     
  94.     /**
  95.      * Set content attribute
  96.      * @param $field attribute name
  97.      */
  98.     public function setContentAttribute($field)
  99.     {
  100.         $this->contentField $field;
  101.     }
  102.     
  103.     /**
  104.      * Set color attribute
  105.      * @param $field attribute name
  106.      */
  107.     public function setColorAttribute($field)
  108.     {
  109.         $this->colorField $field;
  110.     }
  111.     
  112.     /**
  113.      * Clear items
  114.      */
  115.     public function clear()
  116.     {
  117.         $this->items [];
  118.     }
  119.     
  120.     /**
  121.      * Add item
  122.      * @param  $object Item data object
  123.      */
  124.     public function addItem($object)
  125.     {
  126.         $this->items[$object;
  127.     }
  128.     
  129.     /**
  130.      * Display icons as buttons
  131.      */
  132.     public function setUseButton()
  133.     {
  134.         $this->useButton TRUE;
  135.     }
  136.     
  137.     /**
  138.      * Set card item template for rendering
  139.      * @param  $path   Template path
  140.      */
  141.     public function setTemplatePath($path)
  142.     {
  143.         $this->templatePath $path;
  144.     }
  145.     
  146.     /**
  147.      * Set card item template for rendering
  148.      * @param  $template   Template content
  149.      */
  150.     public function setItemTemplate($template)
  151.     {
  152.         $this->itemTemplate $template;
  153.     }
  154.     
  155.     /**
  156.      * Set card title template for rendering
  157.      * @param  $template   Template content
  158.      */
  159.     public function setTitleTemplate($template)
  160.     {
  161.         $this->titleTemplate $template;
  162.     }
  163.     
  164.     /**
  165.      * Add item action
  166.      * @param  $label             Action label
  167.      * @param  $action            Action callback (TAction)
  168.      * @param  $icon              Action icon
  169.      * @param  $display_condition Display condition
  170.      */
  171.     public function addAction(TAction $action$label$icon NULL$display_condition NULL$title NULL)
  172.     {
  173.         $itemAction            new stdClass;
  174.         $itemAction->label     $label;
  175.         $itemAction->action    $action;
  176.         $itemAction->icon      $icon;
  177.         $itemAction->condition $display_condition;
  178.         $itemAction->title     $title;
  179.         
  180.         $this->itemActions[]   $itemAction;
  181.     }
  182.     
  183.     /**
  184.      * Render item
  185.      */
  186.     private function renderItem($item)
  187.     {
  188.         if (!empty($this->templatePath))
  189.         {
  190.             $html new THtmlRenderer($this->templatePath);
  191.             $html->enableSection('main');
  192.             $html->enableTranslation();
  193.             $html AdiantiTemplateHandler::replace($html->getContents()$item);
  194.             
  195.             return $html;
  196.         }
  197.         
  198.         $titleField   $this->titleField;
  199.         $contentField $this->contentField;
  200.         $colorField   $this->colorField;
  201.         
  202.         $item_wrapper              new TElement('div');
  203.         $item_wrapper->{'class'}   'panel card panel-default card-item';
  204.         
  205.         if ($colorField && $item->$colorField)
  206.         {
  207.             $item_wrapper->{'style'}   'border-top: 3px solid '.$item->$colorField;
  208.         }
  209.         
  210.         if ($titleField)
  211.         {
  212.             $item_title new TElement('div');
  213.             $item_title->{'class''panel-heading card-header card-item-title';
  214.             $titleField (strpos($titleField'{'=== FALSE'{' $titleField '}'$titleField;
  215.             $item_title->add(AdiantiTemplateHandler::replace($titleField$item));
  216.         }
  217.         
  218.         if (!empty($this->titleTemplate))
  219.         {
  220.             $item_title new TElement('div');
  221.             $item_title->{'class''panel-heading card-header card-item-title';
  222.             $item_title->add(AdiantiTemplateHandler::replace($this->titleTemplate$item));
  223.         }
  224.         
  225.         if ($contentField)
  226.         {
  227.             $item_content new TElement('div');
  228.             $item_content->{'class''panel-body card-body card-item-content';
  229.             $contentField (strpos($contentField'{'=== FALSE'{' $contentField '}'$contentField;
  230.             $item_content->add(AdiantiTemplateHandler::replace($contentField$item));
  231.         }
  232.         
  233.         if (!empty($this->itemTemplate))
  234.         {
  235.             $item_content new TElement('div');
  236.             $item_content->{'class''panel-body card-body card-item-content';
  237.             $item_template ApplicationTranslator::translateTemplate($this->itemTemplate);
  238.             $item_template AdiantiTemplateHandler::replace($item_template$item);
  239.             $item_content->add($item_template);
  240.         }
  241.         
  242.         if (!empty($item_title))
  243.         {
  244.             $item_wrapper->add($item_title);
  245.         }
  246.         
  247.         if (!empty($item_content))
  248.         {
  249.             $item_wrapper->add($item_content);
  250.             
  251.             if (!empty($this->contentHeight))
  252.             {
  253.                 $item_content->{'style'}   'min-height:'.$this->contentHeight;
  254.                 
  255.                 if (strstr((string) $this->size'%'!== FALSE)
  256.                 {
  257.                     $item_content->{'style'}   'min-height:'.$this->contentHeight;
  258.                 }
  259.                 else
  260.                 {
  261.                     $item_content->{'style'}   'min-height:'.$this->contentHeight.'px';
  262.                 }
  263.             }
  264.         }
  265.         
  266.         if (!empty($this->itemHeight))
  267.         {
  268.             $item_wrapper->{'style'}   'min-height:'.$this->itemHeight;
  269.             
  270.             if (strstr((string) $this->size'%'!== FALSE)
  271.             {
  272.                 $item_wrapper->{'style'}   'min-height:'.$this->itemHeight;
  273.             }
  274.             else
  275.             {
  276.                 $item_wrapper->{'style'}   'min-height:'.$this->itemHeight.'px';
  277.             }
  278.         }
  279.         
  280.         if (count($this->searchAttributes0)
  281.         {
  282.             $item_wrapper->{'id''row_' mt_rand(10000000001999999999);
  283.             
  284.             foreach ($this->searchAttributes as $search_att)
  285.             {
  286.                 if (isset($item->$search_att))
  287.                 {
  288.                     $row_dom_search_att 'search_' $search_att;
  289.                     $item_wrapper->$row_dom_search_att $item->$search_att;
  290.                 }
  291.             }
  292.         }
  293.         
  294.         if (!empty($this->itemActions))
  295.         {
  296.             $item_wrapper->add($this->renderItemActions($item));
  297.         }
  298.         
  299.         return $item_wrapper;
  300.     }
  301.     
  302.     /**
  303.      * Enable fuse search
  304.      * @param $input Field input for search
  305.      * @param $attribute Attribute name
  306.      */
  307.     public function enableSearch(TField $input$attribute
  308.     {
  309.         $input_id    $input->getId();
  310.         $card_id $this->{'id'};
  311.         $this->searchAttributes[$attribute;
  312.         TScript::create("__adianti_input_fuse_search('#{$input_id}', 'search_{$attribute}', '#{$card_id} .card-item')");
  313.     }
  314.     
  315.     /**
  316.      * Render item actions
  317.      */
  318.     private function renderItemActions($object NULL)
  319.     {
  320.         $div            new TElement('div');
  321.         $div->{'class''panel-footer card-footer card-item-actions';
  322.         
  323.         foreach ($this->itemActions as $key => $action)
  324.         {
  325.             if (empty($action->conditionOR call_user_func($action->condition$object))
  326.             {
  327.                 $item_action clone $action->action;
  328.                 if ($item_action->getFieldParameters())
  329.                 {
  330.                     $key $item_action->getFieldParameters()[0];
  331.                     $item_action->setParameter('key'$object->$key);
  332.                 }
  333.                 
  334.                 $url $item_action->prepare($object)->serialize();
  335.                 
  336.                 if ($this->useButton)
  337.                 {
  338.                     $button new TElement('a');
  339.                     $button->{'class''btn btn-default';
  340.                     $button->{'href'$url;
  341.                     $button->{'generator''adianti';
  342.                     $button->add(new TImage($action->icon));
  343.                     $button->add($action->label)
  344.                     
  345.                     if (!empty($action->title))
  346.                     {
  347.                         $button->{'title'$action->title;
  348.                         $button->{'titside''bottom';
  349.                     }
  350.                     
  351.                     $div->add($button);
  352.                 }
  353.                 else
  354.                 {
  355.                     $icon                new TImage($action->icon);
  356.                     $icon->{'style'}    .= ';cursor:pointer;margin-right:4px;';
  357.                     $icon->{'title'}     $action->label;
  358.                     $icon->{'generator''adianti';
  359.                     $icon->{'href'}      $url;
  360.                     
  361.                     $div->add($icon);
  362.                 }
  363.             }
  364.         }
  365.         
  366.         return $div;
  367.     }
  368.     
  369.     
  370.     /**
  371.      * Show cards
  372.      */
  373.     public function show()
  374.     {
  375.         if ($this->items)
  376.         {
  377.             if (!empty($this->itemDatabase))
  378.             {
  379.                 TTransaction::open($this->itemDatabase);
  380.             }
  381.             
  382.             foreach ($this->items as $item)
  383.             {
  384.                 $this->add($this->renderItem($item));
  385.             }
  386.             
  387.             if (!empty($this->itemDatabase))
  388.             {
  389.                 TTransaction::close();
  390.             }
  391.         }
  392.         
  393.         parent::show();
  394.     }
  395. }