Source for file grid.php

Documentation is available at grid.php

  1. <?php
  2. /**
  3.  * @package     Joomla.Platform
  4.  * @subpackage  HTML
  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.  * Utility class for creating HTML Grids
  14.  *
  15.  * @package     Joomla.Platform
  16.  * @subpackage  HTML
  17.  * @since       1.5
  18.  */
  19. abstract class JHtmlGrid
  20. {
  21.     /**
  22.      * Display a boolean setting widget.
  23.      *
  24.      * @param   integer  $i        The row index.
  25.      * @param   integer  $value    The value of the boolean field.
  26.      * @param   string   $taskOn   Task to turn the boolean setting on.
  27.      * @param   string   $taskOff  Task to turn the boolean setting off.
  28.      *
  29.      * @return  string   The boolean setting widget.
  30.      *
  31.      * @since   1.6
  32.      */
  33.     public static function boolean($i$value$taskOn null$taskOff null)
  34.     {
  35.         // Load the behavior.
  36.         static::behavior();
  37.         JHtml::_('bootstrap.tooltip');
  38.  
  39.         // Build the title.
  40.         $title ($valueJText::_('JYES'JText::_('JNO');
  41.         $title JHtml::tooltipText($titleJText::_('JGLOBAL_CLICK_TO_TOGGLE_STATE')0);
  42.  
  43.         // Build the <a> tag.
  44.         $bool ($value'true' 'false';
  45.         $task ($value$taskOff $taskOn;
  46.         $toggle (!$taskfalse true;
  47.  
  48.         if ($toggle)
  49.         {
  50.             return '<a class="grid_' $bool ' hasToolip" title="' $title '" rel="{id:\'cb' $i '\', task:\'' $task
  51.                 . '\'}" href="#toggle"></a>';
  52.         }
  53.         else
  54.         {
  55.             return '<a class="grid_' $bool '"></a>';
  56.         }
  57.     }
  58.  
  59.     /**
  60.      * Method to sort a column in a grid
  61.      *
  62.      * @param   string  $title          The link title
  63.      * @param   string  $order          The order field for the column
  64.      * @param   string  $direction      The current direction
  65.      * @param   string  $selected       The selected ordering
  66.      * @param   string  $task           An optional task override
  67.      * @param   string  $new_direction  An optional direction for the new column
  68.      * @param   string  $tip            An optional text shown as tooltip title instead of $title
  69.      *
  70.      * @return  string 
  71.      *
  72.      * @since   1.5
  73.      */
  74.     public static function sort($title$order$direction 'asc'$selected 0$task null$new_direction 'asc'$tip '')
  75.     {
  76.         JHtml::_('bootstrap.tooltip');
  77.  
  78.         $direction strtolower($direction);
  79.         $icon array('arrow-up-3''arrow-down-3');
  80.         $index = (int) ($direction == 'desc');
  81.  
  82.         if ($order != $selected)
  83.         {
  84.             $direction $new_direction;
  85.         }
  86.         else
  87.         {
  88.             $direction ($direction == 'desc''asc' 'desc';
  89.         }
  90.  
  91.         $html '<a href="#" onclick="Joomla.tableOrdering(\'' $order '\',\'' $direction '\',\'' $task '\');return false;"'
  92.             . ' class="hasTooltip" title="' JHtml::tooltipText(($tip $tip $title)'JGLOBAL_CLICK_TO_SORT_THIS_COLUMN''">';
  93.  
  94.         if (isset($title['0']&& $title['0'== '<')
  95.         {
  96.             $html .= $title;
  97.         }
  98.         else
  99.         {
  100.             $html .= JText::_($title);
  101.         }
  102.  
  103.         if ($order == $selected)
  104.         {
  105.             $html .= ' <i class="icon-' $icon[$index'"></i>';
  106.         }
  107.  
  108.         $html .= '</a>';
  109.  
  110.         return $html;
  111.     }
  112.  
  113.     /**
  114.      * Method to check all checkboxes in a grid
  115.      *
  116.      * @param   string  $name    The name of the form element
  117.      * @param   string  $tip     The text shown as tooltip title instead of $tip
  118.      * @param   string  $action  The action to perform on clicking the checkbox
  119.      *
  120.      * @return  string 
  121.      *
  122.      * @since   3.1.2
  123.      */
  124.     public static function checkall($name 'checkall-toggle'$tip 'JGLOBAL_CHECK_ALL'$action 'Joomla.checkAll(this)')
  125.     {
  126.         JHtml::_('bootstrap.tooltip');
  127.  
  128.         return '<input type="checkbox" name="' $name '" value="" class="hasTooltip" title="' JHtml::tooltipText($tip'" onclick="' $action '" />';
  129.     }
  130.  
  131.     /**
  132.      * Method to create a checkbox for a grid row.
  133.      *
  134.      * @param   integer  $rowNum      The row index
  135.      * @param   integer  $recId       The record id
  136.      * @param   boolean  $checkedOut  True if item is checke out
  137.      * @param   string   $name        The name of the form element
  138.      *
  139.      * @return  mixed    String of html with a checkbox if item is not checked out, null if checked out.
  140.      *
  141.      * @since   1.5
  142.      */
  143.     public static function id($rowNum$recId$checkedOut false$name 'cid')
  144.     {
  145.         return $checkedOut '' '<input type="checkbox" id="cb' $rowNum '" name="' $name '[]" value="' $recId
  146.             . '" onclick="Joomla.isChecked(this.checked);" />';
  147.     }
  148.  
  149.     /**
  150.      * Displays a checked out icon.
  151.      *
  152.      * @param   object   &$row        A data object (must contain checkedout as a property).
  153.      * @param   integer  $i           The index of the row.
  154.      * @param   string   $identifier  The property name of the primary key or index of the row.
  155.      *
  156.      * @return  string 
  157.      *
  158.      * @since   1.5
  159.      */
  160.     public static function checkedOut(&$row$i$identifier 'id')
  161.     {
  162.         $user JFactory::getUser();
  163.         $userid $user->get('id');
  164.  
  165.         if ($row instanceof JTable)
  166.         {
  167.             $result $row->isCheckedOut($userid);
  168.         }
  169.         else
  170.         {
  171.             $result false;
  172.         }
  173.  
  174.         if ($result)
  175.         {
  176.             return static::_checkedOut($row);
  177.         }
  178.         else
  179.         {
  180.             if ($identifier == 'id')
  181.             {
  182.                 return JHtml::_('grid.id'$i$row->$identifier);
  183.             }
  184.             else
  185.             {
  186.                 return JHtml::_('grid.id'$i$row->$identifier$result$identifier);
  187.             }
  188.         }
  189.     }
  190.  
  191.     /**
  192.      * Method to create a clickable icon to change the state of an item
  193.      *
  194.      * @param   mixed    $value   Either the scalar value or an object (for backward compatibility, deprecated)
  195.      * @param   integer  $i       The index
  196.      * @param   string   $img1    Image for a positive or on value
  197.      * @param   string   $img0    Image for the empty or off value
  198.      * @param   string   $prefix  An optional prefix for the task
  199.      *
  200.      * @return  string 
  201.      *
  202.      * @since   1.5
  203.      */
  204.     public static function published($value$i$img1 'tick.png'$img0 'publish_x.png'$prefix '')
  205.     {
  206.         if (is_object($value))
  207.         {
  208.             $value $value->published;
  209.         }
  210.  
  211.         $img $value $img1 $img0;
  212.         $task $value 'unpublish' 'publish';
  213.         $alt $value JText::_('JPUBLISHED'JText::_('JUNPUBLISHED');
  214.         $action $value JText::_('JLIB_HTML_UNPUBLISH_ITEM'JText::_('JLIB_HTML_PUBLISH_ITEM');
  215.  
  216.         return '<a href="#" onclick="return listItemTask(\'cb' $i '\',\'' $prefix $task '\')" title="' $action '">'
  217.             . JHtml::_('image''admin/' $img$altnulltrue'</a>';
  218.     }
  219.  
  220.     /**
  221.      * Method to create a select list of states for filtering
  222.      * By default the filter shows only published and unpublished items
  223.      *
  224.      * @param   string  $filter_state  The initial filter state
  225.      * @param   string  $published     The JText string for published
  226.      * @param   string  $unpublished   The JText string for Unpublished
  227.      * @param   string  $archived      The JText string for Archived
  228.      * @param   string  $trashed       The JText string for Trashed
  229.      *
  230.      * @return  string 
  231.      *
  232.      * @since   1.5
  233.      */
  234.     public static function state($filter_state '*'$published 'Published'$unpublished 'Unpublished'$archived null$trashed null)
  235.     {
  236.         $state array('' => '- ' JText::_('JLIB_HTML_SELECT_STATE'' -''P' => JText::_($published)'U' => JText::_($unpublished));
  237.  
  238.         if ($archived)
  239.         {
  240.             $state['A'JText::_($archived);
  241.         }
  242.  
  243.         if ($trashed)
  244.         {
  245.             $state['T'JText::_($trashed);
  246.         }
  247.  
  248.         return JHtml::_(
  249.             'select.genericlist',
  250.             $state,
  251.             'filter_state',
  252.             array(
  253.                 'list.attr' => 'class="inputbox" size="1" onchange="Joomla.submitform();"',
  254.                 'list.select' => $filter_state,
  255.                 'option.key' => null
  256.             )
  257.         );
  258.     }
  259.  
  260.     /**
  261.      * Method to create an icon for saving a new ordering in a grid
  262.      *
  263.      * @param   array   $rows   The array of rows of rows
  264.      * @param   string  $image  The image [UNUSED]
  265.      * @param   string  $task   The task to use, defaults to save order
  266.      *
  267.      * @return  string 
  268.      *
  269.      * @since   1.5
  270.      */
  271.     public static function order($rows$image 'filesave.png'$task 'saveorder')
  272.     {
  273.         return '<a href="javascript:saveorder(' (count($rows1', \'' $task '\')" rel="tooltip" class="saveorder btn btn-micro pull-right" title="'
  274.             . JText::_('JLIB_HTML_SAVE_ORDER''"><i class="icon-menu-2"></i></a>';
  275.     }
  276.  
  277.     /**
  278.      * Method to create a checked out icon with optional overlib in a grid.
  279.      *
  280.      * @param   object   &$row     The row object
  281.      * @param   boolean  $overlib  True if an overlib with checkout information should be created.
  282.      *
  283.      * @return  string   HTMl for the icon and overlib
  284.      *
  285.      * @since   1.5
  286.      */
  287.     protected static function _checkedOut(&$row$overlib true)
  288.     {
  289.         $hover '';
  290.  
  291.         if ($overlib)
  292.         {
  293.             JHtml::_('bootstrap.tooltip');
  294.  
  295.             $date JHtml::_('date'$row->checked_out_timeJText::_('DATE_FORMAT_LC1'));
  296.             $time JHtml::_('date'$row->checked_out_time'H:i');
  297.  
  298.             $hover '<span class="editlinktip hasTooltip" title="' JHtml::tooltipText('JLIB_HTML_CHECKED_OUT'$row->editor'<br />' $date '<br />'
  299.                 . $time '">';
  300.         }
  301.  
  302.         return $hover JHtml::_('image''admin/checked_out.png'nullnulltrue'</span>';
  303.     }
  304.  
  305.     /**
  306.      * Method to build the behavior script and add it to the document head.
  307.      *
  308.      * @return  void 
  309.      *
  310.      * @since   1.6
  311.      */
  312.     public static function behavior()
  313.     {
  314.         static $loaded;
  315.  
  316.         if (!$loaded)
  317.         {
  318.             // Build the behavior script.
  319.             $js '
  320.         window.addEvent(\'domready\', function(){
  321.             actions = $$(\'a.move_up\');
  322.             actions.combine($$(\'a.move_down\'));
  323.             actions.combine($$(\'a.grid_true\'));
  324.             actions.combine($$(\'a.grid_false\'));
  325.             actions.combine($$(\'a.grid_trash\'));
  326.             actions.each(function(a){
  327.                 a.addEvent(\'click\', function(){
  328.                     args = JSON.decode(this.rel);
  329.                     listItemTask(args.id, args.task);
  330.                 });
  331.             });
  332.             $$(\'input.check-all-toggle\').each(function(el){
  333.                 el.addEvent(\'click\', function(){
  334.                     if (el.checked) {
  335.                         document.id(this.form).getElements(\'input[type=checkbox]\').each(function(i){
  336.                             i.checked = true;
  337.                         })
  338.                     }
  339.                     else {
  340.                         document.id(this.form).getElements(\'input[type=checkbox]\').each(function(i){
  341.                             i.checked = false;
  342.                         })
  343.                     }
  344.                 });
  345.             });
  346.         });';
  347.  
  348.             // Add the behavior to the document head.
  349.             $document JFactory::getDocument();
  350.             $document->addScriptDeclaration($js);
  351.  
  352.             $loaded true;
  353.         }
  354.     }
  355. }

Documentation generated on Tue, 19 Nov 2013 15:04:03 +0100 by phpDocumentor 1.4.3