system/modules/pct_customelements/PCT/CustomElements/Attributes/Files/Files.php line 47

Open in your IDE?
  1. <?php
  2. /**
  3.  * Contao Open Source CMS
  4.  * 
  5.  * Copyright (C) 2005-2013 Leo Feyer
  6.  * 
  7.  * @copyright    Tim Gatzky 2013, Premium Contao Webworks, Premium Contao Themes
  8.  * @author        Tim Gatzky <info@tim-gatzky.de>
  9.  * @package        pct_customelements
  10.  * @attribute    AttributeFiles
  11.  * @link        http://contao.org
  12.  */
  13. /**
  14.  * Namespace
  15.  */
  16. namespace PCT\CustomElements\Attributes;
  17. /**
  18.  * Imports
  19.  */
  20. use Contao\FilesModel;
  21. use Contao\StringUtil;
  22. use PCT\CustomElements\Core\Attribute as Attribute;
  23. use PCT\CustomElements\Helper\ControllerHelper as ControllerHelper;
  24. /**
  25.  * Class file
  26.  * Files
  27.  */
  28. class Files extends Attribute
  29. {
  30.     /**
  31.      * Tell the vault to store how to save the data (binary,blob)
  32.      * Leave empty to varchar
  33.      * @var boolean
  34.      */
  35.     protected $saveDataAs '';
  36.         
  37.     /**
  38.      * Create new instance
  39.      * @param array
  40.      */ 
  41.     public function __construct($arrData=array())
  42.     {
  43.         if(count($arrData) > 0)
  44.         {
  45.             $this->setData($arrData);
  46.         }
  47.         
  48.         if($this->get('eval_multiple'))
  49.         {
  50.             $this->saveDataAs 'blob';
  51.         }
  52.     }    
  53.     /**
  54.      * Return the field definition
  55.      * @return array
  56.      */
  57.     public function getFieldDefinition()
  58.     {
  59.         $arrEval $this->getEval();
  60.         
  61.         $arrEval['fieldType'] ='radio';
  62.         if( isset($arrEval['multiple']) && $arrEval['multiple'])
  63.         {
  64.             $arrEval['fieldType'] ='checkbox';
  65.         }
  66.         
  67.         // toggle show files only or folders only
  68.         $arrEval['files'] = $this->get('eval_files') ? 1;
  69.         
  70.         if($this->get('isDownload'))
  71.         {
  72.             $arrEval['isDownloads'] = true;
  73.         
  74.             // restricted file types
  75.             if(!$this->get('eval_extensions'))
  76.             {
  77.                 $arrEval['extensions'] = $GLOBALS['TL_CONFIG']['allowedDownload'];
  78.             }
  79.         }
  80.         
  81.         $arrReturn = array
  82.         (
  83.             'label'            => array( $this->get('title'),$this->get('description') ),
  84.             'exclude'        => true,
  85.             'inputType'        => 'fileTree',
  86.             'eval'            => $arrEval,
  87.             'sql'            => "binary(16) NULL",
  88.         );
  89.         
  90.         if($this->get('eval_multiple'))
  91.         {
  92.             $arrReturn['sql'] = "blob NULL";
  93.         }
  94.         
  95.         // make attribute sortable
  96.         if($this->get('sortBy') == 'custom' && isset($arrEval['multiple']) && $arrEval['multiple'])
  97.         {
  98.             $arrReturn['sortable'] = true;
  99.         }
  100.         
  101.         return $arrReturn;
  102.     }
  103.     /**
  104.      * Parse widget callback
  105.      * Generate the widgets and add them to the child list
  106.      * Optional, return the whole compiled widget as html string
  107.      * @param object    Widget
  108.      * @param string    Name of the field
  109.      * @param array        Field definition
  110.      * @param object    DataContainer
  111.      */
  112.     public function parseWidgetCallback($objWidget,$strField,$arrFieldDef,$objDC)
  113.     {
  114.         // validate the input
  115.         $objWidget->validate();
  116.         if($objWidget->hasErrors())
  117.         {
  118.             $objWidget->class 'error';
  119.         }
  120.         
  121.         $strBuffer $objWidget->parse();
  122.         
  123.         // load data container and language file
  124.         ControllerHelper::callstatic('loadDataContainer',array('tl_content'));
  125.         ControllerHelper::callstatic('loadLanguageFile',array('tl_content'));
  126.         
  127.         $options \Contao\StringUtil::deserialize($this->get('options'));
  128.         if(empty($options) || !is_array($options) || $objWidget->multiple)
  129.         {
  130.             return $strBuffer;
  131.         }
  132.         
  133.         // generate a checkbox field for the 'new window' option 
  134.         if(in_array('linkTitle'$options))
  135.         {
  136.             $strName $strField.'_linkTitle';
  137.             $arrFieldDef $GLOBALS['TL_DCA']['tl_content']['fields']['linkTitle'];
  138.             $arrFieldDef['eval']['tl_class'] = 'w50';
  139.             $this->prepareChildAttribute($arrFieldDef,$strName);
  140.         }
  141.         
  142.         // generate a text field for the link text option 
  143.         if(in_array('titleText'$options))
  144.         {
  145.             $strName $strField.'_titleText';
  146.             $arrFieldDef $GLOBALS['TL_DCA']['tl_content']['fields']['titleText'];
  147.             $arrFieldDef['eval']['tl_class'] = 'w50';
  148.             $this->prepareChildAttribute($arrFieldDef,$strName);
  149.         }
  150.         
  151.         return $strBuffer;
  152.     }
  153.     
  154.     
  155.     /**
  156.      * Render the attribute and return html
  157.      * @return array
  158.      */
  159.     public function renderCallback($strField,$varValue,$objTemplate,$objAttribute)
  160.     {
  161.         // convert to array
  162.         $varValue StringUtil::deserialize($varValue);
  163.         if($this->get('eval_multiple') && !is_array($varValue))
  164.         {
  165.             $varValue array_filter(explode(','$varValue));
  166.         }
  167.         # issue: #342
  168.         if(is_array($varValue))
  169.         {
  170.             $varValue array_filter($varValue);
  171.         }
  172.         
  173.         $objOrig $this->getActiveRecord();
  174.         $objActiveRecord = new \Contao\ContentModel();
  175.         $objActiveRecord->mergeRow$objOrig->row() );
  176.         $objActiveRecord->__set('strPk',$objActiveRecord->id);
  177.         $objActiveRecord->customTpl '';
  178.         $objActiveRecord->start '';
  179.         $objActiveRecord->stop '';
  180.         $objActiveRecord->invisible '';
  181.         $objActiveRecord->autogrid 0;
  182.         $objActiveRecord->cssID $objAttribute->get('cssID');
  183.         $objActiveRecord->isCustomElement true;
  184.         $objActiveRecord->headline '';
  185.         $objActiveRecord->inline $objAttribute->get('inline');
  186.         // a non ce attribute template is coming in
  187.         if( \property_exists($this,'isCustomTemplate') && $this->isCustomTemplate)
  188.         {
  189.             $objActiveRecord->customTpl $objAttribute->get('template');
  190.         }
  191.         
  192.         $objMyAttribute null;
  193.         if($this->get('isDownload') && $this->get('eval_multiple'))
  194.         {
  195.             $objMyAttribute = new \Contao\ContentDownloads($objActiveRecord);
  196.             $objMyAttribute->multiSRC $varValue;
  197.             $objMyAttribute->sortBy $objAttribute->get('sortBy');
  198.             $objMyAttribute->orderSRC $varValue;
  199.             $objMyAttribute->inline $objAttribute->get('inline');
  200.             // respect individual ordering via record
  201.             if(isset($objActiveRecord->{'orderSRC_'.$strField}))
  202.             {
  203.                 $objMyAttribute->orderSRC $objActiveRecord->{'orderSRC_'.$strField};
  204.             }
  205.         }
  206.         else if($this->get('isDownload') && !$this->get('eval_multiple'))
  207.         {
  208.             $arrOptionValues $objAttribute->loadOptionValues($strField);
  209.         
  210.             $objMyAttribute = new \Contao\ContentDownload($objActiveRecord);
  211.             $objMyAttribute->singleSRC $varValue;
  212.             $objMyAttribute->linkTitle $arrOptionValues['linkTitle'] ?? '';
  213.             $objMyAttribute->linkText $arrOptionValues['linkText'] ?? '';
  214.             $objMyAttribute->inline $objAttribute->get('inline');
  215.             // handle single folders
  216.             $path FilesModel::findByPk($varValue)->path;
  217.             if(is_dir($path))
  218.             {
  219.                 $this->set('eval_multiple',true);
  220.                 return $this->renderCallback($strField,$varValue,$objTemplate,$objAttribute);
  221.             }
  222.         }
  223.         else if(!$this->get('isDownload'))
  224.         {
  225.             $objMyAttribute = new \Contao\ContentText($objActiveRecord);
  226.             
  227.             // respect individual ordering via record
  228.             if(isset($objActiveRecord->{'orderSRC_'.$strField}))
  229.             {
  230.                 $varValue $objActiveRecord->{'orderSRC_'.$strField};
  231.             }
  232.             
  233.             // make sure it is an array
  234.             if(!is_array($varValue))
  235.             {
  236.                 $varValue explode(','$varValue);
  237.             }
  238.             
  239.             $paths = array();
  240.             if(count($varValue) > 0)
  241.             {
  242.                 foreach($varValue as $v)
  243.                 {
  244.                     $file FilesModel::findByPk($v);
  245.                     if( $file !== null )
  246.                     {
  247.                         $paths[] = FilesModel::findByPk($v)->path;
  248.                     }
  249.                 }
  250.             }
  251.             $objMyAttribute->text implode(','$paths);
  252.         }
  253.         // render raw
  254.         else {return '';}
  255.         
  256.         $arrCssID \Contao\StringUtil::deserialize($this->get('cssID'));
  257.         $arrClass = array('attribute',$this->get('type'));
  258.         if(strlen($arrCssID[1]) > 0)
  259.         {
  260.             $arrClass array_unique(array_merge($arrClass,explode(' ',$arrCssID[1])));
  261.         }
  262.         $arrCssID[1] = implode(' ',$arrClass);
  263.         $objMyAttribute->cssID $arrCssID;
  264.         $objMyAttribute->space = array();
  265.         
  266.         // merge with object vars
  267.         foreach($objActiveRecord->row() as $k => $v)
  268.         {
  269.             if( isset($objMyAttribute->{$k}) )
  270.             {
  271.                 $objActiveRecord->{$k} = $objMyAttribute->{$k};
  272.             }
  273.         }
  274.                 
  275.         // pass to template
  276.         $objTemplate->activeRecord = clone($objActiveRecord);
  277.         $objTemplate->element = clone($objMyAttribute);
  278.         // download: remove cid parameter
  279.         if( \Contao\Input::get('file') != '' && empty($_GET['file']) === false && isset($_GET['cid']) )
  280.         {
  281.             \Contao\Input::setGet('cid',null);
  282.         }
  283.         // generate the attribute and place html in attribute template
  284.         $objTemplate->value $objMyAttribute->generate();
  285.         
  286.         // bypass the CE attribute template when a Contao template is in use
  287.         if( \property_exists($objAttribute,'isCustomTemplate') && $objAttribute->isCustomTemplate)
  288.         {
  289.             return $objTemplate->value;
  290.         }
  291.         
  292.         return $objTemplate->parse();
  293.     }
  294.     
  295.     
  296.     /**
  297.      * Generate wildcard value
  298.      * @param mixed
  299.      * @param object    DatabaseResult
  300.      * @return string
  301.      */
  302.     public function processWildcardValue($varValue,$objAttribute)
  303.     {
  304.         if($objAttribute->get('type') != 'files' || empty($varValue))
  305.         {
  306.             return $varValue;
  307.         }
  308.         
  309.         $varValue \Contao\StringUtil::deserialize($varValue);
  310.         
  311.         if(!is_array($varValue))
  312.         {
  313.             $varValue array_filterexplode(',',$varValue) );
  314.         }
  315.         
  316.         $objFiles \Contao\FilesModel::findMultipleByUuids($varValue);
  317.         
  318.         $strReturn '';
  319.         if($objAttribute->get('isDownload'))
  320.         {
  321.             $tmp = array();
  322.             foreach($objFiles as $file)
  323.             {
  324.                 $tmp[] = '<a href="'.$file->path.'" title="'.$file->name.'" download>'.$file->path.'</a>';
  325.             }
  326.             $strReturn implode(', '$tmp);
  327.             unset($tmp);
  328.         }
  329.         else
  330.         {
  331.             $strReturn implode(', '$objFiles->fetchEach('path'));
  332.         }
  333.                 
  334.         return $strReturn;
  335.         
  336.     }
  337. }