Html::showMassiveActions PHP Method

showMassiveActions() static public method

Display massive actions
Since: 0.84 (before Search::displayMassiveActions)
static public showMassiveActions ( $options = [] ) : nothing
$options array of parameters must contains : - container : DOM ID of the container of the item checkboxes (since version 0.85) may contains : - num_displayed : integer number of displayed items. Permit to check suhosin limit. (default -1 not to check) - ontop : boolean true if displayed on top (default true) - fixed : boolean true if used with fixed table display (default true) - forcecreate : boolean force creation of modal window (default = false). Modal is automatically created when displayed the ontop item. If only a bottom one is displayed use it - check_itemtype : string alternate itemtype to check right if different from main itemtype (default empty) - check_items_id : integer ID of the alternate item used to check right / optional (default empty) - is_deleted : boolean is massive actions for deleted items ? - extraparams : string extra URL parameters to pass to massive actions (default empty) if ([extraparams]['hidden'] is set : add hidden fields to post) - specific_actions : array of specific actions (do not use standard one) - add_actions : array of actions to add (do not use standard one) - confirm : string of confirm message before massive action - item : CommonDBTM object that has to be passed to the actions - tag_to_send : the tag of the elements to send to the ajax window (default: common)
return nothing
    static function showMassiveActions($options = array())
    {
        global $CFG_GLPI;
        /// TODO : permit to pass several itemtypes to show possible actions of all types : need to clean visibility management after
        $p['ontop'] = true;
        $p['num_displayed'] = -1;
        $p['fixed'] = true;
        $p['forcecreate'] = false;
        $p['check_itemtype'] = '';
        $p['check_items_id'] = '';
        $p['is_deleted'] = false;
        $p['extraparams'] = array();
        $p['width'] = 800;
        $p['height'] = 400;
        $p['specific_actions'] = array();
        $p['add_actions'] = array();
        $p['confirm'] = '';
        $p['rand'] = '';
        $p['container'] = '';
        $p['display_arrow'] = true;
        $p['title'] = _n('Action', 'Actions', Session::getPluralNumber());
        $p['item'] = false;
        $p['tag_to_send'] = 'common';
        foreach ($options as $key => $val) {
            if (isset($p[$key])) {
                $p[$key] = $val;
            }
        }
        $url = $CFG_GLPI['root_doc'] . "/ajax/massiveaction.php";
        if ($p['container']) {
            $p['extraparams']['container'] = $p['container'];
        }
        if ($p['is_deleted']) {
            $p['extraparams']['is_deleted'] = 1;
        }
        if (!empty($p['check_itemtype'])) {
            $p['extraparams']['check_itemtype'] = $p['check_itemtype'];
        }
        if (!empty($p['check_items_id'])) {
            $p['extraparams']['check_items_id'] = $p['check_items_id'];
        }
        if (is_array($p['specific_actions']) && count($p['specific_actions'])) {
            $p['extraparams']['specific_actions'] = $p['specific_actions'];
        }
        if (is_array($p['add_actions']) && count($p['add_actions'])) {
            $p['extraparams']['add_actions'] = $p['add_actions'];
        }
        if ($p['item'] instanceof CommonDBTM) {
            $p['extraparams']['item_itemtype'] = $p['item']->getType();
            $p['extraparams']['item_items_id'] = $p['item']->getID();
        }
        // Manage modal window
        if (isset($_REQUEST['_is_modal']) && $_REQUEST['_is_modal']) {
            $p['extraparams']['hidden']['_is_modal'] = 1;
        }
        if ($p['fixed']) {
            $width = '950px';
        } else {
            $width = '95%';
        }
        $identifier = md5($url . serialize($p['extraparams']) . $p['rand']);
        $max = Toolbox::get_max_input_vars();
        if ($p['num_displayed'] >= 0 && $max > 0 && $max < $p['num_displayed'] + 10) {
            if (!$p['ontop'] || isset($p['forcecreate']) && $p['forcecreate']) {
                echo "<table class='tab_cadre' width='{$width}'><tr class='tab_bg_1'>" . "<td><span class='b'>";
                echo __('Selection too large, massive action disabled.') . "</span>";
                if ($_SESSION['glpi_use_mode'] == Session::DEBUG_MODE) {
                    echo "<br>" . __('To increase the limit: change max_input_vars or suhosin.post.max_vars in php configuration.');
                }
                echo "</td></tr></table>";
            }
        } else {
            // Create Modal window on top
            if ($p['ontop'] || isset($p['forcecreate']) && $p['forcecreate']) {
                echo "<div id='massiveactioncontent{$identifier}'></div>";
                if (!empty($p['tag_to_send'])) {
                    $js_modal_fields = "            var items = \$('";
                    if (!empty($p['container'])) {
                        $js_modal_fields .= '[id=' . $p['container'] . '] ';
                    }
                    $js_modal_fields .= "[data-glpicore-ma-tags~=" . $p['tag_to_send'] . "]')";
                    $js_modal_fields .= ".each(function( index ) {\n";
                    $js_modal_fields .= "              fields[\$(this).attr('name')] = \$(this).attr('value');\n";
                    $js_modal_fields .= "              if ((\$(this).attr('type') == 'checkbox') && (!\$(this).is(':checked'))) {\n";
                    $js_modal_fields .= "                 fields[\$(this).attr('name')] = 0;\n";
                    $js_modal_fields .= "              }\n";
                    $js_modal_fields .= "            });";
                } else {
                    $js_modal_fields = "";
                }
                Ajax::createModalWindow('massiveaction_window' . $identifier, $url, array('title' => $p['title'], 'container' => 'massiveactioncontent' . $identifier, 'extraparams' => $p['extraparams'], 'width' => $p['width'], 'height' => $p['height'], 'js_modal_fields' => $js_modal_fields));
            }
            echo "<table class='tab_glpi' width='{$width}'><tr>";
            if ($p['display_arrow']) {
                echo "<td width='30px'><img src='" . $CFG_GLPI["root_doc"] . "/pics/arrow-left" . ($p['ontop'] ? '-top' : '') . ".png' alt=''></td>";
            }
            echo "<td width='100%' class='left'>";
            echo "<a class='vsubmit' ";
            if (is_array($p['confirm'] || strlen($p['confirm']))) {
                echo self::addConfirmationOnAction($p['confirm'], "massiveaction_window{$identifier}.dialog(\"open\");");
            } else {
                echo "onclick='massiveaction_window{$identifier}.dialog(\"open\");'";
            }
            echo " href='#modal_massaction_content{$identifier}' title=\"" . htmlentities($p['title'], ENT_QUOTES, 'UTF-8') . "\">";
            echo $p['title'] . "</a>";
            echo "</td>";
            echo "</tr></table>";
            if (!$p['ontop'] || isset($p['forcecreate']) && $p['forcecreate']) {
                // Clean selection
                $_SESSION['glpimassiveactionselected'] = array();
            }
        }
    }

Usage Example

Example #1
1
 /**
  * Display datas extracted from DB
  *
  * @param $data array of search datas prepared to get datas
  *
  * @return nothing
  **/
 static function displayDatas(array &$data)
 {
     global $CFG_GLPI;
     $item = null;
     if (class_exists($data['itemtype'])) {
         $item = new $data['itemtype']();
     }
     $rand = mt_rand();
     if (!isset($data['data']) || !isset($data['data']['totalcount'])) {
         return false;
     }
     // Contruct Pager parameters
     $globallinkto = Toolbox::append_params(array('criteria' => Toolbox::stripslashes_deep($data['search']['criteria']), 'metacriteria' => Toolbox::stripslashes_deep($data['search']['metacriteria'])), '&amp;');
     $parameters = "sort=" . $data['search']['sort'] . "&amp;order=" . $data['search']['order'] . '&amp;' . $globallinkto;
     if (isset($_GET['_in_modal'])) {
         $parameters .= "&amp;_in_modal=1";
     }
     // Global search header
     if ($data['display_type'] == self::GLOBAL_SEARCH) {
         if ($data['item']) {
             echo "<div class='center'><h2>" . $data['item']->getTypeName();
             // More items
             if ($data['data']['totalcount'] > $data['search']['start'] + self::GLOBAL_DISPLAY_COUNT) {
                 echo " <a href='" . $data['search']['target'] . "?{$parameters}'>" . __('All') . "</a>";
             }
             echo "</h2></div>\n";
         } else {
             return false;
         }
     }
     // If the begin of the view is before the number of items
     if ($data['data']['count'] > 0) {
         // Display pager only for HTML
         if ($data['display_type'] == self::HTML_OUTPUT) {
             // For plugin add new parameter if available
             if ($plug = isPluginItemType($data['itemtype'])) {
                 $function = 'plugin_' . $plug['plugin'] . '_addParamFordynamicReport';
                 if (function_exists($function)) {
                     $out = $function($data['itemtype']);
                     if (is_array($out) && count($out)) {
                         $parameters .= Toolbox::append_params($out, '&amp;');
                     }
                 }
             }
             $search_config_top = "";
             $search_config_bottom = "";
             if (!isset($_GET['_in_modal']) && Session::haveRightsOr('search_config', array(DisplayPreference::PERSONAL, DisplayPreference::GENERAL))) {
                 $search_config_top = $search_config_bottom = "<div class='pager_controls'><img alt=\"" . __s('Select default items to show') . "\" title=\"" . __s('Select default items to show') . "\" src='" . $CFG_GLPI["root_doc"] . "/pics/options_search.png' ";
                 $search_config_top .= " class='pointer' onClick=\"";
                 $search_config_top .= Html::jsGetElementbyID('search_config_top') . ".dialog('open');\">";
                 $search_config_bottom .= " class='pointer' onClick=\"";
                 $search_config_bottom .= Html::jsGetElementbyID('search_config_bottom') . ".dialog('open');\">";
                 $search_config_top .= Ajax::createIframeModalWindow('search_config_top', $CFG_GLPI["root_doc"] . "/front/displaypreference.form.php?itemtype=" . $data['itemtype'], array('title' => __('Select default items to show'), 'reloadonclose' => true, 'display' => false));
                 $search_config_bottom .= Ajax::createIframeModalWindow('search_config_bottom', $CFG_GLPI["root_doc"] . "/front/displaypreference.form.php?itemtype=" . $data['itemtype'], array('title' => __('Select default items to show'), 'reloadonclose' => true, 'display' => false));
             }
             if ($item !== null && $item->maybeDeleted()) {
                 $delete_ctrl = self::isDeletedSwitch($data['search']['is_deleted']);
                 $search_config_top .= $delete_ctrl;
             }
             Html::printPager($data['search']['start'], $data['data']['totalcount'], $data['search']['target'], $parameters, $data['itemtype'], 0, $search_config_top);
             $search_config_top .= "</div>";
             $search_config_bottom .= "</div>";
         }
         // Define begin and end var for loop
         // Search case
         $begin_display = $data['data']['begin'];
         $end_display = $data['data']['end'];
         // Form to massive actions
         $isadmin = $data['item'] && $data['item']->canUpdate();
         if (!$isadmin && InfoCom::canApplyOn($data['itemtype'])) {
             $isadmin = Infocom::canUpdate() || Infocom::canCreate();
         }
         if ($data['itemtype'] != 'AllAssets') {
             $showmassiveactions = count(MassiveAction::getAllMassiveActions($data['item'], $data['search']['is_deleted']));
         } else {
             $showmassiveactions = true;
         }
         $massformid = 'massform' . $data['itemtype'];
         if ($showmassiveactions && $data['display_type'] == self::HTML_OUTPUT) {
             Html::openMassiveActionsForm($massformid);
             $massiveactionparams = $data['search']['massiveactionparams'];
             $massiveactionparams['num_displayed'] = $end_display - $begin_display;
             $massiveactionparams['fixed'] = false;
             $massiveactionparams['is_deleted'] = $data['search']['is_deleted'];
             $massiveactionparams['container'] = $massformid;
             Html::showMassiveActions($massiveactionparams);
         }
         // Compute number of columns to display
         // Add toview elements
         $nbcols = count($data['data']['cols']);
         if ($data['display_type'] == self::HTML_OUTPUT && $showmassiveactions) {
             // HTML display - massive modif
             $nbcols++;
         }
         // Display List Header
         echo self::showHeader($data['display_type'], $end_display - $begin_display + 1, $nbcols);
         // New Line for Header Items Line
         $headers_line = '';
         $headers_line_top = '';
         $headers_line_bottom = '';
         $headers_line_top .= self::showBeginHeader($data['display_type']);
         $headers_line_top .= self::showNewLine($data['display_type']);
         if ($data['display_type'] == self::HTML_OUTPUT) {
             //          $headers_line_bottom .= self::showBeginHeader($data['display_type']);
             $headers_line_bottom .= self::showNewLine($data['display_type']);
         }
         $header_num = 1;
         if ($data['display_type'] == self::HTML_OUTPUT && $showmassiveactions) {
             // HTML display - massive modif
             $headers_line_top .= self::showHeaderItem($data['display_type'], Html::getCheckAllAsCheckbox($massformid), $header_num, "", 0, $data['search']['order']);
             if ($data['display_type'] == self::HTML_OUTPUT) {
                 $headers_line_bottom .= self::showHeaderItem($data['display_type'], Html::getCheckAllAsCheckbox($massformid), $header_num, "", 0, $data['search']['order']);
             }
         }
         // Display column Headers for toview items
         $metanames = array();
         foreach ($data['data']['cols'] as $key => $val) {
             $linkto = '';
             if (!$val['meta'] && (!isset($val['searchopt']['nosort']) || !$val['searchopt']['nosort'])) {
                 $linkto = $data['search']['target'] . (strpos($data['search']['target'], '?') ? '&amp;' : '?') . "itemtype=" . $data['itemtype'] . "&amp;sort=" . $val['id'] . "&amp;order=" . ($data['search']['order'] == "ASC" ? "DESC" : "ASC") . "&amp;start=" . $data['search']['start'] . "&amp;" . $globallinkto;
             }
             $name = $val["name"];
             // prefix by group name (corresponding to optgroup in dropdown) if exists
             if (isset($val['groupname'])) {
                 $name = $val['groupname'] . " - " . $name;
             }
             // Not main itemtype add itemtype to display
             if ($data['itemtype'] != $val['itemtype']) {
                 if (!isset($metanames[$val['itemtype']])) {
                     if ($metaitem = getItemForItemtype($val['itemtype'])) {
                         $metanames[$val['itemtype']] = $metaitem->getTypeName();
                     }
                 }
                 $name = sprintf(__('%1$s - %2$s'), $metanames[$val['itemtype']], $val["name"]);
             }
             $headers_line .= self::showHeaderItem($data['display_type'], $name, $header_num, $linkto, !$val['meta'] && $data['search']['sort'] == $val['id'], $data['search']['order']);
         }
         // Add specific column Header
         if (isset($CFG_GLPI["union_search_type"][$data['itemtype']])) {
             $headers_line .= self::showHeaderItem($data['display_type'], __('Item type'), $header_num);
         }
         // End Line for column headers
         $headers_line .= self::showEndLine($data['display_type']);
         $headers_line_top .= $headers_line;
         if ($data['display_type'] == self::HTML_OUTPUT) {
             $headers_line_bottom .= $headers_line;
         }
         $headers_line_top .= self::showEndHeader($data['display_type']);
         //          $headers_line_bottom .= self::showEndHeader($data['display_type']);
         echo $headers_line_top;
         // Init list of items displayed
         if ($data['display_type'] == self::HTML_OUTPUT) {
             Session::initNavigateListItems($data['itemtype']);
         }
         // Num of the row (1=header_line)
         $row_num = 1;
         $massiveaction_field = 'id';
         if ($data['itemtype'] != 'AllAssets' && isset($CFG_GLPI["union_search_type"][$data['itemtype']])) {
             $massiveaction_field = 'refID';
         }
         $typenames = array();
         // Display Loop
         foreach ($data['data']['rows'] as $rowkey => $row) {
             // Column num
             $item_num = 1;
             $row_num++;
             // New line
             echo self::showNewLine($data['display_type'], $row_num % 2, $data['search']['is_deleted']);
             $current_type = isset($row['TYPE']) ? $row['TYPE'] : $data['itemtype'];
             $massiveaction_type = $current_type;
             if ($data['itemtype'] != 'AllAssets' && isset($CFG_GLPI["union_search_type"][$data['itemtype']])) {
                 $massiveaction_type = $data['itemtype'];
             }
             // Add item in item list
             Session::addToNavigateListItems($current_type, $row["id"]);
             if ($data['display_type'] == self::HTML_OUTPUT && $showmassiveactions) {
                 // HTML display - massive modif
                 $tmpcheck = "";
                 if ($data['itemtype'] == 'Entity' && !in_array($row["id"], $_SESSION["glpiactiveentities"])) {
                     $tmpcheck = "&nbsp;";
                 } else {
                     if ($data['item'] instanceof CommonDBTM && $data['item']->maybeRecursive() && !in_array($row["entities_id"], $_SESSION["glpiactiveentities"])) {
                         $tmpcheck = "&nbsp;";
                     } else {
                         $tmpcheck = Html::getMassiveActionCheckBox($massiveaction_type, $row[$massiveaction_field]);
                     }
                 }
                 echo self::showItem($data['display_type'], $tmpcheck, $item_num, $row_num, "width='10'");
             }
             // Print other toview items
             foreach ($data['data']['cols'] as $colkey => $col) {
                 if (!$col['meta']) {
                     echo self::showItem($data['display_type'], $row[$colkey]['displayname'], $item_num, $row_num, self::displayConfigItem($data['itemtype'], $col['id'], $row, $colkey));
                 } else {
                     // META case
                     echo self::showItem($data['display_type'], $row[$colkey]['displayname'], $item_num, $row_num);
                 }
             }
             if (isset($CFG_GLPI["union_search_type"][$data['itemtype']])) {
                 if (!isset($typenames[$row["TYPE"]])) {
                     if ($itemtmp = getItemForItemtype($row["TYPE"])) {
                         $typenames[$row["TYPE"]] = $itemtmp->getTypeName();
                     }
                 }
                 echo self::showItem($data['display_type'], $typenames[$row["TYPE"]], $item_num, $row_num);
             }
             // End Line
             echo self::showEndLine($data['display_type']);
         }
         // Create title
         $title = '';
         if ($data['display_type'] == self::PDF_OUTPUT_LANDSCAPE || $data['display_type'] == self::PDF_OUTPUT_PORTRAIT) {
             $title = self::computeTitle($data);
         }
         if ($data['display_type'] == self::HTML_OUTPUT) {
             echo $headers_line_bottom;
         }
         // Display footer
         echo self::showFooter($data['display_type'], $title);
         // Delete selected item
         if ($data['display_type'] == self::HTML_OUTPUT) {
             if ($showmassiveactions) {
                 $massiveactionparams['ontop'] = false;
                 Html::showMassiveActions($massiveactionparams);
                 // End form for delete item
                 Html::closeForm();
             } else {
                 echo "<br>";
             }
         }
         if ($data['display_type'] == self::HTML_OUTPUT) {
             // In case of HTML display
             Html::printPager($data['search']['start'], $data['data']['totalcount'], $data['search']['target'], $parameters, '', 0, $search_config_bottom);
         }
     } else {
         if ($item !== null && $item->maybeDeleted()) {
             echo "<div class='center'>" . self::isDeletedSwitch($data['search']['is_deleted']) . "</div><br/>";
         }
         echo self::showError($data['display_type']);
     }
 }
All Usage Examples Of Html::showMassiveActions
Html