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

Documentation is available at TMultiFile.php

  1. <?php
  2. namespace Adianti\Widget\Form;
  3.  
  4. use Adianti\Core\AdiantiApplicationConfig;
  5. use Adianti\Widget\Form\AdiantiWidgetInterface;
  6. use Adianti\Widget\Base\TElement;
  7. use Adianti\Widget\Base\TScript;
  8. use Adianti\Widget\Form\TField;
  9. use Adianti\Widget\Form\THidden;
  10.  
  11. use Adianti\Control\TAction;
  12. use Adianti\Core\AdiantiCoreApplication;
  13. use Adianti\Core\AdiantiCoreTranslator;
  14. use Exception;
  15.  
  16. /**
  17.  * FileChooser widget
  18.  *
  19.  * @version    7.4
  20.  * @package    widget
  21.  * @subpackage form
  22.  * @author     Nataniel Rabaioli
  23.  * @author     Pablo Dall'Oglio
  24.  * @copyright  Copyright (c) 2006 Adianti Solutions Ltd. (http://www.adianti.com.br)
  25.  * @license    http://www.adianti.com.br/framework-license
  26.  */
  27. class TMultiFile extends TField implements AdiantiWidgetInterface
  28. {
  29.     protected $id;
  30.     protected $height;
  31.     protected $completeAction;
  32.     protected $uploaderClass;
  33.     protected $extensions;
  34.     protected $seed;
  35.     protected $fileHandling;
  36.     protected $imageGallery;
  37.     protected $galleryWidth;
  38.     protected $galleryHeight;
  39.     protected $popover;
  40.     protected $poptitle;
  41.     protected $popcontent;
  42.     
  43.     /**
  44.      * Constructor method
  45.      * @param $name input name
  46.      */
  47.     public function __construct($name)
  48.     {
  49.         parent::__construct($name);
  50.         $this->id = $this->name . '_' mt_rand(10000000001999999999);
  51.         // $this->height = 25;
  52.         $this->uploaderClass = 'AdiantiUploaderService';
  53.         $this->fileHandling = FALSE;
  54.         
  55.         $ini AdiantiApplicationConfig::get();
  56.         $this->seed = APPLICATION_NAME !empty($ini['general']['seed']$ini['general']['seed''s8dkld83kf73kf094' );
  57.         $this->imageGallery = false;
  58.         $this->popover = false;
  59.     }
  60.     
  61.     /**
  62.      * Enable image gallery view
  63.      */
  64.     public function enableImageGallery($width null$height 100)
  65.     {
  66.         $this->imageGallery  = true;
  67.         $this->galleryWidth  = is_null($width'unset' $width;
  68.         $this->galleryHeight = is_null($height'unset' $height;
  69.     }
  70.     
  71.     /**
  72.      * Enable popover
  73.      * @param $title Title
  74.      * @param $content Content
  75.      */
  76.     public function enablePopover($title null$content '')
  77.     {
  78.         $this->popover    = TRUE;
  79.         $this->poptitle   = $title;
  80.         $this->popcontent = $content;
  81.     }
  82.     
  83.     /**
  84.      * Define the service class for response
  85.      */
  86.     public function setService($service)
  87.     {
  88.         $this->uploaderClass = $service;
  89.     }
  90.     
  91.     /**
  92.      * Define the allowed extensions
  93.      */
  94.     public function setAllowedExtensions($extensions)
  95.     {
  96.         $this->extensions = $extensions;
  97.         $this->tag->{'accept''.' implode(',.'$extensions);
  98.     }
  99.     
  100.     /**
  101.      * Define to file handling
  102.      */
  103.     public function enableFileHandling()
  104.     {
  105.         $this->fileHandling TRUE;
  106.     }
  107.     
  108.     /**
  109.      * Set field size
  110.      */
  111.     public function setSize($width$height NULL)
  112.     {
  113.         $this->size   $width;
  114.     }
  115.     
  116.     /**
  117.      * Set field height
  118.      */
  119.     public function setHeight($height)
  120.     {
  121.         $this->height $height;
  122.     }
  123.     
  124.     /**
  125.      * Return the post data
  126.      */
  127.     public function getPostData()
  128.     {
  129.         $name str_replace(['[',']']['','']$this->name);
  130.         
  131.         if (isset($_POST[$name]))
  132.         {
  133.             return $_POST[$name];
  134.         }
  135.     }
  136.     
  137.     /**
  138.      * Set field value
  139.      */
  140.     public function setValue($value)
  141.     {
  142.         if ($this->fileHandling)
  143.         {
  144.             if (is_array($value))
  145.             {
  146.                 $new_value [];
  147.                 
  148.                 foreach ($value as $key => $item)
  149.                 {
  150.                     if (is_array($item))
  151.                     {
  152.                         $new_value[urlencode(json_encode($item));
  153.                     }
  154.                     else if (is_scalar($itemand (strpos($item'%7B'=== false))
  155.                     {
  156.                         if (!empty($item))
  157.                         {
  158.                             $new_value[urlencode(json_encode(['idFile'=>$key,'fileName'=>$item]));
  159.                         }
  160.                     }
  161.                     else
  162.                     {
  163.                         $value_object json_decode(urldecode($item));
  164.                         
  165.                         if (!empty($value_object->{'delFile'}AND $value_object->{'delFile'== $value_object->{'fileName'})
  166.                         {
  167.                             $value '';
  168.                         }
  169.                         else
  170.                         {
  171.                             $new_value[$item;
  172.                         }
  173.                     }
  174.                 }
  175.                 $value $new_value;
  176.             }
  177.             
  178.             parent::setValue($value);
  179.         }
  180.         else
  181.         {            
  182.             parent::setValue($value);
  183.         }
  184.     }
  185.     
  186.     /**
  187.      * Show the widget at the screen
  188.      */
  189.     public function show()
  190.     {
  191.         // define the tag properties
  192.         $this->tag->{'id'}        $this->id;
  193.         $this->tag->{'name'}      'file_' $this->name.'[]';  // tag name
  194.         $this->tag->{'receiver'}  $this->name;  // tag name
  195.         $this->tag->{'value'}     $this->value// tag value
  196.         $this->tag->{'type'}      'file';       // input type
  197.         $this->tag->{'multiple'}  '1';
  198.         
  199.         if ($this->size)
  200.         {
  201.             $size (strstr((string) $this->size'%'!== FALSE$this->size "{$this->size}px";
  202.             $this->setProperty('style'"width:{$size};"FALSE)//aggregate style info
  203.         }
  204.         
  205.         if ($this->height)
  206.         {
  207.             $height (strstr($this->height'%'!== FALSE$this->height "{$this->height}px";
  208.             $this->setProperty('style'"height:{$height}"FALSE)//aggregate style info
  209.         }
  210.         
  211.         $complete_action "'undefined'";
  212.         
  213.         // verify if the widget is editable
  214.         if (parent::getEditable())
  215.         {
  216.             if (isset($this->completeAction))
  217.             {
  218.                 if (!TForm::getFormByName($this->formNameinstanceof TForm)
  219.                 {
  220.                     throw new Exception(AdiantiCoreTranslator::translate('You must pass the ^1 (^2) as a parameter to ^3'__CLASS__$this->name'TForm::setFields()') );
  221.                 }
  222.                 $string_action $this->completeAction->serialize(FALSE);
  223.                 
  224.                 $complete_action "function() { __adianti_post_lookup('{$this->formName}', '{$string_action}', '{$this->tag-> id}', 'callback'); }";
  225.             }
  226.         }
  227.         else
  228.         {
  229.             // make the field read-only
  230.             $this->tag->{'readonly'"1";
  231.             $this->tag->{'type'}     'text';
  232.             $this->tag->{'class'}    'tfield_disabled'// CSS
  233.         }
  234.         
  235.         $id_div mt_rand(10000000001999999999);
  236.         
  237.         $div new TElement('div');
  238.         $div->{'id'}    'div_file_'.$id_div;
  239.         
  240.         foreach(array)$this->value as $val )
  241.         {
  242.             $hdFileName new THidden($this->name.'[]');
  243.             $hdFileName->setValue$val );
  244.             
  245.             $div->add$hdFileName );
  246.         }
  247.                 
  248.         $div->add$this->tag );
  249.         $div->show();
  250.         
  251.         if (empty($this->extensions))
  252.         {
  253.             $action "engine.php?class={$this->uploaderClass}";
  254.         }
  255.         else
  256.         {
  257.             $hash md5("{$this->seed}{$this->name}".base64_encode((string) serialize($this->extensions)));
  258.             $action "engine.php?class={$this->uploaderClass}&name={$this->name}&hash={$hash}&extensions=".base64_encode((string) serialize($this->extensions));
  259.         }
  260.         
  261.         if ($router AdiantiCoreApplication::getRouter())
  262.         {
  263.             $action $router($actionfalse);
  264.         }
  265.  
  266.         $fileHandling $this->fileHandling '1' '0';
  267.         $imageGallery json_encode(['enabled'=> $this->imageGallery '1' '0''width' => $this->galleryWidth'height' => $this->galleryHeight]);
  268.         $popover json_encode(['enabled' => $this->popover '1' '0''title' => $this->poptitle'content' => base64_encode((string) $this->popcontent)]);
  269.         
  270.         TScript::create(" tmultifile_start( '{$this->tag-> id}', '{$div-> id}', '{$action}', {$complete_action}$fileHandling, '$imageGallery', '$popover');");
  271.     }
  272.     
  273.     /**
  274.      * Define the action to be executed when the user leaves the form field
  275.      * @param $action TAction object
  276.      */
  277.     function setCompleteAction(TAction $action)
  278.     {
  279.         if ($action->isStatic())
  280.         {
  281.             $this->completeAction $action;
  282.         }
  283.         else
  284.         {
  285.             $string_action $action->toString();
  286.             throw new Exception(AdiantiCoreTranslator::translate('Action (^1) must be static to be used in ^2'$string_action__METHOD__));
  287.         }
  288.     }
  289.     
  290.     /**
  291.      * Enable the field
  292.      * @param $form_name Form name
  293.      * @param $field Field name
  294.      */
  295.     public static function enableField($form_name$field)
  296.     {
  297.         TScript::create" tmultifile_enable_field('{$form_name}', '{$field}'); );
  298.     }
  299.     
  300.     /**
  301.      * Disable the field
  302.      * @param $form_name Form name
  303.      * @param $field Field name
  304.      */
  305.     public static function disableField($form_name$field)
  306.     {
  307.         TScript::create" tmultifile_disable_field('{$form_name}', '{$field}'); );
  308.     }
  309.     
  310.     /**
  311.      * Clear the field
  312.      * @param $form_name Form name
  313.      * @param $field Field name
  314.      */
  315.     public static function clearField($form_name$field)
  316.     {
  317.         TScript::create" tmultifile_clear_field('{$form_name}', '{$field}'); );
  318.     }
  319. }