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

Documentation is available at TFile.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 TFile extends TField implements AdiantiWidgetInterface
  28. {
  29.     protected $id;
  30.     protected $height;
  31.     protected $completeAction;
  32.     protected $errorAction;
  33.     protected $uploaderClass;
  34.     protected $placeHolder;
  35.     protected $extensions;
  36.     protected $displayMode;
  37.     protected $seed;
  38.     protected $fileHandling;
  39.     protected $imageGallery;
  40.     protected $galleryWidth;
  41.     protected $galleryHeight;
  42.     protected $popover;
  43.     protected $poptitle;
  44.     protected $popcontent;
  45.     
  46.     /**
  47.      * Constructor method
  48.      * @param $name input name
  49.      */
  50.     public function __construct($name)
  51.     {
  52.         parent::__construct($name);
  53.         $this->id = $this->name . '_' mt_rand(10000000001999999999);
  54.         $this->uploaderClass = 'AdiantiUploaderService';
  55.         $this->fileHandling = FALSE;
  56.         
  57.         $ini AdiantiApplicationConfig::get();
  58.         $this->seed = APPLICATION_NAME !empty($ini['general']['seed']$ini['general']['seed''s8dkld83kf73kf094' );
  59.         $this->imageGallery = false;
  60.         $this->popover = false;
  61.         $this->popcontent = '';
  62.         $this->tag->{'widget''tfile';
  63.     }
  64.     
  65.     /**
  66.      * Enable image gallery view
  67.      */
  68.     public function enableImageGallery($width null$height 100)
  69.     {
  70.         $this->imageGallery  true;
  71.         $this->galleryWidth  is_null($width'unset' $width;
  72.         $this->galleryHeight is_null($height'unset' $height;
  73.     }
  74.     
  75.     /**
  76.      * Enable popover
  77.      * @param $title Title
  78.      * @param $content Content
  79.      */
  80.     public function enablePopover($title null$content '')
  81.     {
  82.         $this->popover    TRUE;
  83.         $this->poptitle   $title;
  84.         $this->popcontent $content;
  85.     }
  86.     
  87.     /**
  88.      * Define the display mode {file}
  89.      */
  90.     public function setDisplayMode($mode)
  91.     {
  92.         $this->displayMode $mode;
  93.     }
  94.     
  95.     /**
  96.      * Define the service class for response
  97.      */
  98.     public function setService($service)
  99.     {
  100.         $this->uploaderClass $service;
  101.     }
  102.     
  103.     /**
  104.      * Define the allowed extensions
  105.      */
  106.     public function setAllowedExtensions($extensions)
  107.     {
  108.         $this->extensions $extensions;
  109.         $this->tag->{'accept''.' implode(',.'$extensions);
  110.     }
  111.     
  112.     /**
  113.      * Define to file handling
  114.      */
  115.     public function enableFileHandling()
  116.     {
  117.         $this->fileHandling TRUE;
  118.     }
  119.     
  120.     /**
  121.      * Set place holder
  122.      */
  123.     public function setPlaceHolder(TElement $widget)
  124.     {
  125.         $this->placeHolder $widget;
  126.     }
  127.     
  128.     /**
  129.      * Set field size
  130.      */
  131.     public function setSize($width$height NULL)
  132.     {
  133.         $this->size   $width;
  134.     }
  135.     
  136.     /**
  137.      * Set field height
  138.      */
  139.     public function setHeight($height)
  140.     {
  141.         $this->height $height;
  142.     }
  143.     
  144.     /**
  145.      * Return the post data
  146.      */
  147.     public function getPostData()
  148.     {
  149.         $name str_replace(['[',']']['','']$this->name);
  150.         
  151.         if (isset($_POST[$name]))
  152.         {
  153.             return $_POST[$name];
  154.         }
  155.     }
  156.     
  157.     /**
  158.      * Set field value
  159.      */
  160.     public function setValue($value)
  161.     {
  162.         if ($this->fileHandling)
  163.         {
  164.             if (strpos($value'%7B'=== false)
  165.             {
  166.                 if (!empty($value))
  167.                 {
  168.                     $this->value urlencode(json_encode(['fileName'=>$value]));
  169.                 }
  170.                 else
  171.                 {
  172.                     $this->value $value;
  173.                 }
  174.             }
  175.             else
  176.             {
  177.                 $value_object json_decode(urldecode($value));
  178.                 
  179.                 if (!empty($value_object->{'delFile'}AND $value_object->{'delFile'== $value_object->{'fileName'})
  180.                 {
  181.                     $value '';
  182.                 }
  183.                 
  184.                 parent::setValue($value);
  185.             }
  186.         }
  187.         else
  188.         {
  189.             parent::setValue($value);
  190.         }
  191.     }
  192.     
  193.     /**
  194.      * Show the widget at the screen
  195.      */
  196.     public function show()
  197.     {
  198.         // define the tag properties
  199.         $this->tag->{'id'}       $this->id;
  200.         $this->tag->{'name'}     'file_' $this->name;  // tag name
  201.         $this->tag->{'receiver'$this->name;  // tag name
  202.         $this->tag->{'value'}    $this->value// tag value
  203.         $this->tag->{'type'}     'file';       // input type
  204.         
  205.         if (!empty($this->size))
  206.         {
  207.             if (strstr((string) $this->size'%'!== FALSE)
  208.             {
  209.                 $this->setProperty('style'"width:{$this->size};"false)//aggregate style info
  210.             }
  211.             else
  212.             {
  213.                 $this->setProperty('style'"width:{$this->size}px;"false)//aggregate style info
  214.             }
  215.         }
  216.         
  217.         if (!empty($this->height))
  218.         {
  219.             $this->setProperty('style'"height:{$this->height}px;"false)//aggregate style info
  220.         }
  221.         
  222.         $hdFileName new THidden($this->name);
  223.         $hdFileName->setValue$this->value );
  224.         
  225.         $complete_action "'undefined'";
  226.         $error_action "'undefined'";
  227.         
  228.         // verify if the widget is editable
  229.         if (parent::getEditable())
  230.         {
  231.             if (isset($this->completeAction|| isset($this->errorAction))
  232.             {
  233.                 if (!TForm::getFormByName($this->formNameinstanceof TForm)
  234.                 {
  235.                     throw new Exception(AdiantiCoreTranslator::translate('You must pass the ^1 (^2) as a parameter to ^3'__CLASS__$this->name'TForm::setFields()') );
  236.                 }
  237.             }
  238.             
  239.             if (isset($this->completeAction))
  240.             {
  241.                 $string_action $this->completeAction->serialize(FALSE);
  242.                 $complete_action "function() { __adianti_post_lookup('{$this->formName}', '{$string_action}', '{$this->id}', 'callback'); tfile_update_download_link('{$this->name}') }";
  243.             }
  244.             
  245.             if (isset($this->errorAction))
  246.             {
  247.                 $string_action $this->errorAction->serialize(FALSE);
  248.                 $error_action "function() { __adianti_post_lookup('{$this->formName}', '{$string_action}', '{$this->id}', 'callback'); }";
  249.             }
  250.         }
  251.         else
  252.         {
  253.             // make the field read-only
  254.             $this->tag->{'readonly'"1";
  255.             $this->tag->{'type''text';
  256.             $this->tag->{'class''tfield_disabled'// CSS
  257.         }
  258.         
  259.         $div new TElement('div');
  260.         $div->{'style'"display:inline;width:100%;";
  261.         $div->{'id''div_file_'.mt_rand(10000000001999999999);
  262.         $div->{'class''div_file';
  263.         
  264.         $div->add$hdFileName );
  265.         if ($this->placeHolder)
  266.         {
  267.             $div->add$this->tag );
  268.             $div->add$this->placeHolder );
  269.             $this->tag->{'style''display:none';
  270.         }
  271.         else
  272.         {
  273.             $div->add$this->tag );
  274.         }
  275.         
  276.         if ($this->displayMode == 'file' AND file_exists($this->value))
  277.         {
  278.             $icon TElement::tag('i'null['class' => 'fa fa-download']);
  279.             $link new TElement('a');
  280.             $link->{'id'}     'view_'.$this->name;
  281.             $link->{'href'}   'download.php?file='.$this->value;
  282.             $link->{'target''download';
  283.             $link->{'style'}  'padding: 4px; display: block';
  284.             $link->add($icon);
  285.             $link->add($this->value);
  286.             $div->add$link );
  287.         }
  288.         
  289.         $div->show();
  290.         
  291.         if (empty($this->extensions))
  292.         {
  293.             $action "engine.php?class={$this->uploaderClass}";
  294.         }
  295.         else
  296.         {
  297.             $hash md5("{$this->seed}{$this->name}".base64_encode(serialize($this->extensions)));
  298.             $action "engine.php?class={$this->uploaderClass}&name={$this->name}&hash={$hash}&extensions=".base64_encode(serialize($this->extensions));
  299.         }
  300.         
  301.         if ($router AdiantiCoreApplication::getRouter())
  302.         {
  303.             $action $router($actionfalse);
  304.         }
  305.  
  306.         $fileHandling $this->fileHandling '1' '0';
  307.         $imageGallery json_encode(['enabled'=> $this->imageGallery '1' '0''width' => $this->galleryWidth'height' => $this->galleryHeight]);
  308.         $popover json_encode(['enabled' => $this->popover '1' '0''title' => $this->poptitle'content' => base64_encode($this->popcontent)]);
  309.         
  310.         TScript::create(" tfile_start( '{$this->tag-> id}', '{$div-> id}', '{$action}', {$complete_action}, {$error_action}$fileHandling, '$imageGallery', '$popover');");
  311.     }
  312.     
  313.     /**
  314.      * Define the action to be executed when upload is finished
  315.      * @param $action TAction object
  316.      */
  317.     function setCompleteAction(TAction $action)
  318.     {
  319.         if ($action->isStatic())
  320.         {
  321.             $this->completeAction $action;
  322.         }
  323.         else
  324.         {
  325.             $string_action $action->toString();
  326.             throw new Exception(AdiantiCoreTranslator::translate('Action (^1) must be static to be used in ^2'$string_action__METHOD__));
  327.         }
  328.     }
  329.     
  330.     /**
  331.      * Define the action to be executed when some error occurs
  332.      * @param $action TAction object
  333.      */
  334.     function setErrorAction(TAction $action)
  335.     {
  336.         if ($action->isStatic())
  337.         {
  338.             $this->errorAction $action;
  339.         }
  340.         else
  341.         {
  342.             $string_action $action->toString();
  343.             throw new Exception(AdiantiCoreTranslator::translate('Action (^1) must be static to be used in ^2'$string_action__METHOD__));
  344.         }
  345.     }
  346.     
  347.     /**
  348.      * Enable the field
  349.      * @param $form_name Form name
  350.      * @param $field Field name
  351.      */
  352.     public static function enableField($form_name$field)
  353.     {
  354.         TScript::create" tfile_enable_field('{$form_name}', '{$field}'); );
  355.     }
  356.     
  357.     /**
  358.      * Disable the field
  359.      * @param $form_name Form name
  360.      * @param $field Field name
  361.      */
  362.     public static function disableField($form_name$field)
  363.     {
  364.         TScript::create" tfile_disable_field('{$form_name}', '{$field}'); );
  365.     }
  366.     
  367.     /**
  368.      * Clear the field
  369.      * @param $form_name Form name
  370.      * @param $field Field name
  371.      */
  372.     public static function clearField($form_name$field)
  373.     {
  374.         TScript::create" tfile_clear_field('{$form_name}', '{$field}'); );
  375.     }
  376. }