Group::getDataItems PHP 메소드

getDataItems() 공개 메소드

get list of Computers in a group
public getDataItems ( array $types, $field, $tree, $user, $start, array &$res ) : integer
$types array Array of types
$field String field name
$tree Boolean include child groups
$user Boolean include members (users)
$start Integer (first row to retrieve)
$res array Array result filled on ouput
리턴 integer total of items
    function getDataItems(array $types, $field, $tree, $user, $start, array &$res)
    {
        global $DB;
        // include item of child groups ?
        if ($tree) {
            $grprestrict = "IN (" . implode(',', getSonsOf('glpi_groups', $this->getID())) . ")";
        } else {
            $grprestrict = "='" . $this->getID() . "'";
        }
        // include items of members
        if ($user) {
            $ufield = str_replace('groups', 'users', $field);
            $grprestrict = "(`{$field}` {$grprestrict}\n                          OR (`{$field}`=0\n                              AND `{$ufield}` IN (SELECT `users_id`\n                                                FROM `glpi_groups_users`\n                                                WHERE `groups_id` {$grprestrict})))";
        } else {
            $grprestrict = "`{$field}` {$grprestrict}";
        }
        // Count the total of item
        $nb = array();
        $tot = 0;
        foreach ($types as $itemtype) {
            $nb[$itemtype] = 0;
            if (!($item = getItemForItemtype($itemtype))) {
                continue;
            }
            if (!$item->canView()) {
                continue;
            }
            if (!$item->isField($field)) {
                continue;
            }
            $restrict[$itemtype] = $grprestrict;
            if ($item->isEntityAssign()) {
                $restrict[$itemtype] .= getEntitiesRestrictRequest(" AND ", $item->getTable(), '', '', $item->maybeRecursive());
            }
            if ($item->maybeTemplate()) {
                $restrict[$itemtype] .= " AND NOT `is_template`";
            }
            if ($item->maybeDeleted()) {
                $restrict[$itemtype] .= " AND NOT `is_deleted`";
            }
            $tot += $nb[$itemtype] = countElementsInTable($item->getTable(), $restrict[$itemtype]);
        }
        $max = $_SESSION['glpilist_limit'];
        if ($start >= $tot) {
            $start = 0;
        }
        $res = array();
        foreach ($types as $itemtype) {
            if (!($item = getItemForItemtype($itemtype))) {
                continue;
            }
            if ($start >= $nb[$itemtype]) {
                // No need to read
                $start -= $nb[$itemtype];
            } else {
                $query = "SELECT `id`\n                      FROM `" . $item->getTable() . "`\n                      WHERE " . $restrict[$itemtype] . "\n                      ORDER BY `name`\n                      LIMIT {$start},{$max}";
                foreach ($DB->request($query) as $data) {
                    $res[] = array('itemtype' => $itemtype, 'items_id' => $data['id']);
                    $max--;
                }
                // For next type
                $start = 0;
            }
            if (!$max) {
                break;
            }
        }
        return $tot;
    }

Usage Example

예제 #1
0
 static function pdfItems(PluginPdfSimplePDF $pdf, Group $group, $tech, $tree, $user)
 {
     global $CFG_GLPI;
     if ($tech) {
         $types = $CFG_GLPI['linkgroup_tech_types'];
         $field = 'groups_id_tech';
         $title = __('Managed items');
     } else {
         $types = $CFG_GLPI['linkgroup_types'];
         $field = 'groups_id';
         $title = __('Used items');
     }
     $datas = array();
     $max = $group->getDataItems($types, $field, $tree, $user, 0, $datas);
     $nb = count($datas);
     if ($nb < $max) {
         $title = sprintf(__('%1$s (%2$s)'), $title, $nb / $max);
     } else {
         $title = sprintf(__('%1$s (%2$s)'), $title, $nb);
     }
     $pdf->setColumnsSize(100);
     $pdf->displayTitle($title);
     if ($nb) {
         if ($tree || $user) {
             $pdf->setColumnsSize(16, 20, 34, 30);
             $pdf->displayTitle(__('Type'), __('Name'), __('Entity'), Group::getTypeName(1) . " / " . User::getTypeName(1));
         } else {
             $pdf->setColumnsSize(20, 25, 55);
             $pdf->displayTitle(__('Type'), __('Name'), __('Entity'));
         }
     } else {
         $pdf->displayLine(__('No item found'));
     }
     $tmpgrp = new Group();
     $tmpusr = new User();
     foreach ($datas as $data) {
         if (!($item = getItemForItemtype($data['itemtype']))) {
             continue;
         }
         $item->getFromDB($data['items_id']);
         $col4 = '';
         if ($tree || $user) {
             if ($grp = $item->getField($field)) {
                 if ($tmpgrp->getFromDB($grp)) {
                     $col4 = $tmpgrp->getNameID();
                 }
             } else {
                 if ($usr = $item->getField(str_replace('groups', 'users', $field))) {
                     $col4 = Html::clean(getUserName($usr));
                 }
             }
         }
         $pdf->displayLine($item->getTypeName(1), $item->getName(), Html::clean(Dropdown::getDropdownName("glpi_entities", $item->getEntityID())), $col4);
     }
     $pdf->displaySpace();
 }