Source for file media.php

Documentation is available at media.php

  1. <?php
  2. /**
  3.  * @package     Joomla.Libraries
  4.  * @subpackage  Form
  5.  *
  6.  * @copyright   Copyright (C) 2005 - 2013 Open Source Matters, Inc. All rights reserved.
  7.  * @license     GNU General Public License version 2 or later; see LICENSE
  8.  */
  9.  
  10. defined('JPATH_PLATFORM') or die;
  11.  
  12. /**
  13.  * Form Field class for the Joomla CMS.
  14.  * Provides a modal media selector including upload mechanism
  15.  *
  16.  * @package     Joomla.Libraries
  17.  * @subpackage  Form
  18.  * @since       1.6
  19.  */
  20. class JFormFieldMedia extends JFormField
  21. {
  22.     /**
  23.      * The form field type.
  24.      *
  25.      * @var    string 
  26.      * @since  1.6
  27.      */
  28.     protected $type = 'Media';
  29.  
  30.     /**
  31.      * The initialised state of the document object.
  32.      *
  33.      * @var    boolean 
  34.      * @since  1.6
  35.      */
  36.     protected static $initialised = false;
  37.  
  38.     /**
  39.      * The authorField.
  40.      *
  41.      * @var    string 
  42.      * @since  3.2
  43.      */
  44.     protected $authorField;
  45.  
  46.     /**
  47.      * The asset.
  48.      *
  49.      * @var    string 
  50.      * @since  3.2
  51.      */
  52.     protected $asset;
  53.  
  54.     /**
  55.      * The link.
  56.      *
  57.      * @var    string 
  58.      * @since  3.2
  59.      */
  60.     protected $link;
  61.  
  62.     /**
  63.      * The authorField.
  64.      *
  65.      * @var    string 
  66.      * @since  3.2
  67.      */
  68.     protected $preview;
  69.  
  70.     /**
  71.      * The preview.
  72.      *
  73.      * @var    string 
  74.      * @since  3.2
  75.      */
  76.     protected $directory;
  77.  
  78.     /**
  79.      * The previewWidth.
  80.      *
  81.      * @var    int 
  82.      * @since  3.2
  83.      */
  84.     protected $previewWidth;
  85.  
  86.     /**
  87.      * The previewHeight.
  88.      *
  89.      * @var    int 
  90.      * @since  3.2
  91.      */
  92.     protected $previewHeight;
  93.  
  94.     /**
  95.      * Method to get certain otherwise inaccessible properties from the form field object.
  96.      *
  97.      * @param   string  $name  The property name for which to the the value.
  98.      *
  99.      * @return  mixed  The property value or null.
  100.      *
  101.      * @since   3.2
  102.      */
  103.     public function __get($name)
  104.     {
  105.         switch ($name)
  106.         {
  107.             case 'authorField':
  108.             case 'asset':
  109.             case 'link':
  110.             case 'preview':
  111.             case 'directory':
  112.             case 'previewWidth':
  113.             case 'previewHeight':
  114.                 return $this->$name;
  115.         }
  116.  
  117.         return parent::__get($name);
  118.     }
  119.  
  120.     /**
  121.      * Method to set certain otherwise inaccessible properties of the form field object.
  122.      *
  123.      * @param   string  $name   The property name for which to the the value.
  124.      * @param   mixed   $value  The value of the property.
  125.      *
  126.      * @return  void 
  127.      *
  128.      * @since   3.2
  129.      */
  130.     public function __set($name, $value)
  131.     {
  132.         switch ($name)
  133.         {
  134.             case 'authorField':
  135.             case 'asset':
  136.             case 'link':
  137.             case 'preview':
  138.             case 'directory':
  139.                 $this->$name = (string) $value;
  140.                 break;
  141.  
  142.             case 'previewWidth':
  143.             case 'previewHeight':
  144.                 $this->$name = (int) $value;
  145.                 break;
  146.  
  147.             default:
  148.                 parent::__set($name, $value);
  149.         }
  150.     }
  151.  
  152.     /**
  153.      * Method to attach a JForm object to the field.
  154.      *
  155.      * @param   SimpleXMLElement  $element  The SimpleXMLElement object representing the <field /> tag for the form field object.
  156.      * @param   mixed             $value    The form field value to validate.
  157.      * @param   string            $group    The field name group control value. This acts as as an array container for the field.
  158.      *                                       For example if the field has name="foo" and the group value is set to "bar" then the
  159.      *                                       full field name would end up being "bar[foo]".
  160.      *
  161.      * @return  boolean  True on success.
  162.      *
  163.      * @see     JFormField::setup()
  164.      * @since   3.2
  165.      */
  166.     public function setup(SimpleXMLElement $element, $value, $group = null)
  167.     {
  168.         $result = parent::setup($element, $value, $group);
  169.  
  170.         if ($result == true)
  171.         {
  172.             $assetField = $this->element['asset_field'] ? (string) $this->element['asset_field'] : 'asset_id';
  173.  
  174.             $this->authorField   = $this->element['created_by_field'] ? (string) $this->element['created_by_field'] : 'created_by';
  175.             $this->asset         = $this->form->getValue($assetField) ? $this->form->getValue($assetField) : (string) $this->element['asset_id'];
  176.             $this->link          = (string) $this->element['link'];
  177.             $this->preview       = (string) $this->element['preview'];
  178.             $this->directory     = (string) $this->element['directory'];
  179.             $this->previewWidth  = isset($this->element['preview_width']) ? (int) $this->element['preview_width'] : 300;
  180.             $this->previewHeight = isset($this->element['preview_height']) ? (int) $this->element['preview_height'] : 200;
  181.         }
  182.  
  183.         return $result;
  184.     }
  185.  
  186.     /**
  187.      * Method to get the field input markup for a media selector.
  188.      * Use attributes to identify specific created_by and asset_id fields
  189.      *
  190.      * @return  string  The field input markup.
  191.      *
  192.      * @since   1.6
  193.      */
  194.     protected function getInput()
  195.     {
  196.         $asset = $this->asset;
  197.  
  198.         if ($asset == '')
  199.         {
  200.             $asset = JFactory::getApplication()->input->get('option');
  201.         }
  202.  
  203.         if (!self::$initialised)
  204.         {
  205.             // Load the modal behavior script.
  206.             JHtml::_('behavior.modal');
  207.  
  208.             // Build the script.
  209.             $script = array();
  210.             $script[] = '    function jInsertFieldValue(value, id) {';
  211.             $script[] = '        var old_value = document.id(id).value;';
  212.             $script[] = '        if (old_value != value) {';
  213.             $script[] = '            var elem = document.id(id);';
  214.             $script[] = '            elem.value = value;';
  215.             $script[] = '            elem.fireEvent("change");';
  216.             $script[] = '            if (typeof(elem.onchange) === "function") {';
  217.             $script[] = '                elem.onchange();';
  218.             $script[] = '            }';
  219.             $script[] = '            jMediaRefreshPreview(id);';
  220.             $script[] = '        }';
  221.             $script[] = '    }';
  222.  
  223.             $script[] = '    function jMediaRefreshPreview(id) {';
  224.             $script[] = '        var value = document.id(id).value;';
  225.             $script[] = '        var img = document.id(id + "_preview");';
  226.             $script[] = '        if (img) {';
  227.             $script[] = '            if (value) {';
  228.             $script[] = '                img.src = "' . JUri::root() . '" + value;';
  229.             $script[] = '                document.id(id + "_preview_empty").setStyle("display", "none");';
  230.             $script[] = '                document.id(id + "_preview_img").setStyle("display", "");';
  231.             $script[] = '            } else { ';
  232.             $script[] = '                img.src = ""';
  233.             $script[] = '                document.id(id + "_preview_empty").setStyle("display", "");';
  234.             $script[] = '                document.id(id + "_preview_img").setStyle("display", "none");';
  235.             $script[] = '            } ';
  236.             $script[] = '        } ';
  237.             $script[] = '    }';
  238.  
  239.             $script[] = '    function jMediaRefreshPreviewTip(tip)';
  240.             $script[] = '    {';
  241.             $script[] = '        var img = tip.getElement("img.media-preview");';
  242.             $script[] = '        tip.getElement("div.tip").setStyle("max-width", "none");';
  243.             $script[] = '        var id = img.getProperty("id");';
  244.             $script[] = '        id = id.substring(0, id.length - "_preview".length);';
  245.             $script[] = '        jMediaRefreshPreview(id);';
  246.             $script[] = '        tip.setStyle("display", "block");';
  247.             $script[] = '    }';
  248.  
  249.             // Add the script to the document head.
  250.             JFactory::getDocument()->addScriptDeclaration(implode("\n", $script));
  251.  
  252.             self::$initialised = true;
  253.         }
  254.  
  255.         $html = array();
  256.         $attr = '';
  257.  
  258.         // Initialize some field attributes.
  259.         $attr .= !empty($this->class) ? ' class="input-small ' . $this->class . '"' : 'class="input-small"';
  260.         $attr .= !empty($this->size) ? ' size="' . $this->size . '"' : '';
  261.  
  262.         // Initialize JavaScript field attributes.
  263.         $attr .= !empty($this->onchange) ? ' onchange="' . $this->onchange . '"' : '';
  264.  
  265.         // The text field.
  266.         $html[] = '<div class="input-prepend input-append">';
  267.  
  268.         // The Preview.
  269.         $showPreview = true;
  270.         $showAsTooltip = false;
  271.  
  272.         switch ($this->preview)
  273.         {
  274.             case 'no': // Deprecated parameter value
  275.             case 'false':
  276.             case 'none':
  277.                 $showPreview = false;
  278.                 break;
  279.  
  280.             case 'yes': // Deprecated parameter value
  281.             case 'true':
  282.             case 'show':
  283.                 break;
  284.  
  285.             case 'tooltip':
  286.             default:
  287.                 $showAsTooltip = true;
  288.                 $options = array(
  289.                     'onShow' => 'jMediaRefreshPreviewTip',
  290.                 );
  291.                 JHtml::_('behavior.tooltip', '.hasTipPreview', $options);
  292.                 break;
  293.         }
  294.  
  295.         if ($showPreview)
  296.         {
  297.             if ($this->value && file_exists(JPATH_ROOT . '/' . $this->value))
  298.             {
  299.                 $src = JUri::root() . $this->value;
  300.             }
  301.             else
  302.             {
  303.                 $src = '';
  304.             }
  305.  
  306.             $width = $this->previewWidth;
  307.             $height = $this->previewHeight;
  308.             $style = '';
  309.             $style .= ($width > 0) ? 'max-width:' . $width . 'px;' : '';
  310.             $style .= ($height > 0) ? 'max-height:' . $height . 'px;' : '';
  311.  
  312.             $imgattr = array(
  313.                 'id' => $this->id . '_preview',
  314.                 'class' => 'media-preview',
  315.                 'style' => $style,
  316.             );
  317.  
  318.             $img = JHtml::image($src, JText::_('JLIB_FORM_MEDIA_PREVIEW_ALT'), $imgattr);
  319.             $previewImg = '<div id="' . $this->id . '_preview_img"' . ($src ? '' : ' style="display:none"') . '>' . $img . '</div>';
  320.             $previewImgEmpty = '<div id="' . $this->id . '_preview_empty"' . ($src ? ' style="display:none"' : '') . '>'
  321.                 . JText::_('JLIB_FORM_MEDIA_PREVIEW_EMPTY') . '</div>';
  322.  
  323.             if ($showAsTooltip)
  324.             {
  325.                 $html[] = '<div class="media-preview add-on">';
  326.                 $tooltip = $previewImgEmpty . $previewImg;
  327.                 $options = array(
  328.                     'title' => JText::_('JLIB_FORM_MEDIA_PREVIEW_SELECTED_IMAGE'),
  329.                     'text' => '<i class="icon-eye"></i>',
  330.                     'class' => 'hasTipPreview'
  331.                 );
  332.  
  333.                 $html[] = JHtml::tooltip($tooltip, $options);
  334.                 $html[] = '</div>';
  335.             }
  336.             else
  337.             {
  338.                 $html[] = '<div class="media-preview add-on" style="height:auto">';
  339.                 $html[] = ' ' . $previewImgEmpty;
  340.                 $html[] = ' ' . $previewImg;
  341.                 $html[] = '</div>';
  342.             }
  343.         }
  344.  
  345.         $html[] = '    <input type="text" name="' . $this->name . '" id="' . $this->id . '" value="'
  346.             . htmlspecialchars($this->value, ENT_COMPAT, 'UTF-8') . '" readonly="readonly"' . $attr . ' />';
  347.  
  348.         if ($this->value && file_exists(JPATH_ROOT . '/' . $this->value))
  349.         {
  350.             $folder = explode('/', $this->value);
  351.             $folder = array_diff_assoc($folder, explode('/', JComponentHelper::getParams('com_media')->get('image_path', 'images')));
  352.             array_pop($folder);
  353.             $folder = implode('/', $folder);
  354.         }
  355.         elseif (file_exists(JPATH_ROOT . '/' . JComponentHelper::getParams('com_media')->get('image_path', 'images') . '/' . $this->directory))
  356.         {
  357.             $folder = $this->directory;
  358.         }
  359.         else
  360.         {
  361.             $folder = '';
  362.         }
  363.  
  364.         // The button.
  365.         if ($this->disabled != true)
  366.         {
  367.             JHtml::_('bootstrap.tooltip');
  368.  
  369.             $html[] = '<a class="modal btn" title="' . JText::_('JLIB_FORM_BUTTON_SELECT') . '" href="'
  370.                 . ($this->readonly ? ''
  371.                 : ($this->link ? $this->link
  372.                     : 'index.php?option=com_media&amp;view=images&amp;tmpl=component&amp;asset=' . $asset . '&amp;author='
  373.                     . $this->form->getValue($this->authorField)) . '&amp;fieldid=' . $this->id . '&amp;folder=' . $folder) . '"'
  374.                 . ' rel="{handler: \'iframe\', size: {x: 800, y: 500}}">';
  375.             $html[] = JText::_('JLIB_FORM_BUTTON_SELECT') . '</a><a class="btn hasTooltip" title="' . JText::_('JLIB_FORM_BUTTON_CLEAR') . '" href="#" onclick="';
  376.             $html[] = 'jInsertFieldValue(\'\', \'' . $this->id . '\');';
  377.             $html[] = 'return false;';
  378.             $html[] = '">';
  379.             $html[] = '<i class="icon-remove"></i></a>';
  380.         }
  381.  
  382.         $html[] = '</div>';
  383.  
  384.         return implode("\n", $html);
  385.     }
  386. }

Documentation generated on Tue, 19 Nov 2013 15:07:41 +0100 by phpDocumentor 1.4.3