is_a() static public méthode

This method emulates PHP 5.3.9: is_a with allow_string == true
static public is_a ( $object, $class_name ) : true
$object can be an object or a string contining the class name
$class_name the name of the class to compare
Résultat true if $object is an instance of $class_name
 /**
  * Check if given object can have InfoCom
  *
  * @since version 0.85
  *
  * @param $item  an object or a string
  *
  * @return true if $object is an object that can have InfoCom
  *
  **/
 static function canApplyOn($item)
 {
     global $CFG_GLPI;
     // All devices are subjects to infocom !
     if (Toolbox::is_a($item, 'Item_Devices')) {
         return true;
     }
     // We also allow direct items to check
     if ($item instanceof CommonGLPI) {
         $item = $item->getType();
     }
     if (in_array($item, $CFG_GLPI['infocom_types'])) {
         return true;
     }
     return false;
 }
Exemple #2
0
 /**
  * @since version 0.85
  *
  * @param $itemtype
  **/
 static function getMetaReferenceItemtype($itemtype)
 {
     $types = array('Computer', 'Problem', 'Ticket', 'Printer', 'Monitor', 'Peripheral', 'Software', 'Phone');
     foreach ($types as $type) {
         if (Toolbox::is_a($itemtype, $type)) {
             return $type;
         }
     }
     return false;
 }
 /**
  * @since version 0.85
  *
  * @see CommonDBTM::getMassiveActionsForItemtype()
  **/
 static function getMassiveActionsForItemtype(array &$actions, $itemtype, $is_deleted = 0, CommonDBTM $checkitem = NULL)
 {
     global $CFG_GLPI;
     $action_prefix = 'Document_Item' . MassiveAction::CLASS_ACTION_SEPARATOR;
     if (self::canApplyOn($itemtype)) {
         if (Document::canView()) {
             $actions[$action_prefix . 'add'] = _x('button', 'Add a document');
             $actions[$action_prefix . 'remove'] = _x('button', 'Remove a document');
         }
     }
     if (Toolbox::is_a($itemtype, __CLASS__) && static::canUpdate()) {
         $actions[$action_prefix . 'add_item'] = _x('button', 'Add an item');
         $actions[$action_prefix . 'remove_item'] = _x('button', 'Remove an item');
     }
 }
Exemple #4
0
 /**
  * Show the availability of a user
  *
  * @since version 0.83
  *
  * @param $params   array of params
  *    must contain :
  *          - begin: begin date to check (default '')
  *          - end: end date to check (default '')
  *          - itemtype : User or Object type (Ticket...)
  *          - foreign key field of the itemtype to define which item to used
  *    optional :
  *          - limitto : limit display to a specific user
  *
  * @return Nothing (display function)
  **/
 static function checkAvailability($params = array())
 {
     global $CFG_GLPI, $DB;
     if (!isset($params['itemtype'])) {
         return false;
     }
     if (!($item = getItemForItemtype($params['itemtype']))) {
         return false;
     }
     if (!isset($params[$item->getForeignKeyField()]) || !$item->getFromDB($params[$item->getForeignKeyField()])) {
         return false;
     }
     // No limit by default
     if (!isset($params['limitto'])) {
         $params['limitto'] = 0;
     }
     if (isset($params['begin']) && !empty($params['begin'])) {
         $begin = $params['begin'];
     } else {
         $begin = date("Y-m-d");
     }
     if (isset($params['end']) && !empty($params['end'])) {
         $end = $params['end'];
     } else {
         $end = date("Y-m-d");
     }
     if ($end < $begin) {
         $end = $begin;
     }
     $realbegin = $begin . " " . $CFG_GLPI["planning_begin"];
     $realend = $end . " " . $CFG_GLPI["planning_end"];
     $users = array();
     switch ($item->getType()) {
         case 'User':
             $users[$item->getID()] = $item->getName();
             break;
         default:
             if (Toolbox::is_a($item, 'CommonITILObject')) {
                 foreach ($item->getUsers(CommonITILActor::ASSIGN) as $data) {
                     $users[$data['users_id']] = getUserName($data['users_id']);
                 }
                 foreach ($item->getGroups(CommonITILActor::ASSIGN) as $data) {
                     foreach (Group_User::getGroupUsers($data['groups_id']) as $data2) {
                         $users[$data2['id']] = formatUserName($data2["id"], $data2["name"], $data2["realname"], $data2["firstname"]);
                     }
                 }
             }
             break;
     }
     asort($users);
     // Use get method to check availability
     echo "<div class='center'><form method='GET' name='form' action='planning.php'>\n";
     echo "<table class='tab_cadre_fixe'>";
     $colspan = 5;
     if (count($users) > 1) {
         $colspan++;
     }
     echo "<tr class='tab_bg_1'><th colspan='{$colspan}'>" . __('Availability') . "</th>";
     echo "</tr>";
     echo "<tr class='tab_bg_1'>";
     echo "<td>" . __('Start') . "</td>\n";
     echo "<td>";
     Html::showDateField("begin", array('value' => $begin, 'maybeempty' => false));
     echo "</td>\n";
     echo "<td>" . __('End') . "</td>\n";
     echo "<td>";
     Html::showDateField("end", array('value' => $end, 'maybeempty' => false));
     echo "</td>\n";
     if (count($users) > 1) {
         echo "<td width='40%'>";
         $data = array(0 => __('All'));
         $data += $users;
         Dropdown::showFromArray('limitto', $data, array('width' => '100%', 'value' => $params['limitto']));
         echo "</td>";
     }
     echo "<td class='center'>";
     echo "<input type='hidden' name='" . $item->getForeignKeyField() . "' value=\"" . $item->getID() . "\">";
     echo "<input type='hidden' name='itemtype' value=\"" . $item->getType() . "\">";
     echo "<input type='submit' class='submit' name='checkavailability' value=\"" . _sx('button', 'Search') . "\">";
     echo "</td>\n";
     echo "</tr>";
     echo "</table>";
     Html::closeForm();
     echo "</div>\n";
     if ($params['limitto'] > 0 && isset($users[$params['limitto']])) {
         $displayuser[$params['limitto']] = $users[$params['limitto']];
     } else {
         $displayuser = $users;
     }
     if (count($displayuser)) {
         foreach ($displayuser as $who => $whoname) {
             $params = array('who' => $who, 'who_group' => 0, 'begin' => $realbegin, 'end' => $realend);
             $interv = array();
             foreach ($CFG_GLPI['planning_types'] as $itemtype) {
                 $interv = array_merge($interv, $itemtype::populatePlanning($params));
             }
             // Print Headers
             echo "<br><div class='center'><table class='tab_cadre_fixe'>";
             $colnumber = 1;
             $plan_begin = explode(":", $CFG_GLPI["planning_begin"]);
             $plan_end = explode(":", $CFG_GLPI["planning_end"]);
             $begin_hour = intval($plan_begin[0]);
             $end_hour = intval($plan_end[0]);
             if ($plan_end[1] != 0) {
                 $end_hour++;
             }
             $colsize = floor((100 - 15) / ($end_hour - $begin_hour));
             $timeheader = '';
             for ($i = $begin_hour; $i < $end_hour; $i++) {
                 $from = ($i < 10 ? '0' : '') . $i;
                 $timeheader .= "<th width='{$colsize}%' colspan='4'>" . $from . ":00</th>";
                 $colnumber += 4;
             }
             // Print Headers
             echo "<tr class='tab_bg_1'><th colspan='{$colnumber}'>";
             echo $whoname;
             echo "</th></tr>";
             echo "<tr class='tab_bg_1'><th width='15%'>&nbsp;</th>";
             echo $timeheader;
             echo "</tr>";
             $day_begin = strtotime($realbegin);
             $day_end = strtotime($realend);
             for ($time = $day_begin; $time < $day_end; $time += DAY_TIMESTAMP) {
                 $current_day = date('Y-m-d', $time);
                 echo "<tr><th>" . Html::convDate($current_day) . "</th>";
                 $begin_quarter = $begin_hour * 4;
                 $end_quarter = $end_hour * 4;
                 for ($i = $begin_quarter; $i < $end_quarter; $i++) {
                     $begin_time = date("Y-m-d H:i:s", strtotime($current_day) + $i * HOUR_TIMESTAMP / 4);
                     $end_time = date("Y-m-d H:i:s", strtotime($current_day) + ($i + 1) * HOUR_TIMESTAMP / 4);
                     // Init activity interval
                     $begin_act = $end_time;
                     $end_act = $begin_time;
                     reset($interv);
                     while ($data = current($interv)) {
                         if ($data["begin"] >= $begin_time && $data["end"] <= $end_time) {
                             // In
                             if ($begin_act > $data["begin"]) {
                                 $begin_act = $data["begin"];
                             }
                             if ($end_act < $data["end"]) {
                                 $end_act = $data["end"];
                             }
                             unset($interv[key($interv)]);
                         } else {
                             if ($data["begin"] < $begin_time && $data["end"] > $end_time) {
                                 // Through
                                 $begin_act = $begin_time;
                                 $end_act = $end_time;
                                 next($interv);
                             } else {
                                 if ($data["begin"] >= $begin_time && $data["begin"] < $end_time) {
                                     // Begin
                                     if ($begin_act > $data["begin"]) {
                                         $begin_act = $data["begin"];
                                     }
                                     $end_act = $end_time;
                                     next($interv);
                                 } else {
                                     if ($data["end"] > $begin_time && $data["end"] <= $end_time) {
                                         //End
                                         $begin_act = $begin_time;
                                         if ($end_act < $data["end"]) {
                                             $end_act = $data["end"];
                                         }
                                         unset($interv[key($interv)]);
                                     } else {
                                         // Defautl case
                                         next($interv);
                                     }
                                 }
                             }
                         }
                     }
                     if ($begin_act < $end_act) {
                         if ($begin_act <= $begin_time && $end_act >= $end_time) {
                             // Activity in quarter
                             echo "<td class='notavailable'>&nbsp;</td>";
                         } else {
                             // Not all the quarter
                             if ($begin_act <= $begin_time) {
                                 echo "<td class='partialavailableend'>&nbsp;</td>";
                             } else {
                                 echo "<td class='partialavailablebegin'>&nbsp;</td>";
                             }
                         }
                     } else {
                         // No activity
                         echo "<td class='available'>&nbsp;</td>";
                     }
                 }
                 echo "</tr>";
             }
             echo "<tr class='tab_bg_1'><td colspan='{$colnumber}'>&nbsp;</td></tr>";
             echo "</table></div>";
         }
     }
     echo "<div><table class='tab_cadre'>";
     echo "<tr class='tab_bg_1'>";
     echo "<th>" . __('Caption') . "</th>";
     echo "<td class='available' colspan=8>" . __('Available') . "</td>";
     echo "<td class='notavailable' colspan=8>" . __('Unavailable') . "</td>";
     echo "</tr>";
     echo "</table></div>";
 }
 /**
  * Print the item costs
  *
  * @param $item                  CommonITILObject object or Project
  * @param $withtemplate boolean  Template or basic item (default '')
  *
  * @return total cost
  **/
 static function showForObject($item, $withtemplate = '')
 {
     global $DB, $CFG_GLPI;
     $forproject = false;
     if (Toolbox::is_a($item, 'Project')) {
         $forproject = true;
     }
     $ID = $item->fields['id'];
     if (!$item->getFromDB($ID) || !$item->canViewItem() || !static::canView()) {
         return false;
     }
     $canedit = false;
     if (!$forproject) {
         $canedit = $item->canUpdateItem();
     }
     echo "<div class='center'>";
     $condition = "= '{$ID}'";
     if ($forproject) {
         $condition = " IN ('" . implode("','", ProjectTask::getAllTicketsForProject($ID)) . "')";
     }
     $query = "SELECT *\n                FROM `" . static::getTable() . "`\n                WHERE `" . static::$items_id . "` {$condition}\n                ORDER BY `begin_date`";
     $rand = mt_rand();
     if ($canedit) {
         echo "<div id='viewcost" . $ID . "_{$rand}'></div>\n";
         echo "<script type='text/javascript' >\n";
         echo "function viewAddCost" . $ID . "_{$rand}() {\n";
         $params = array('type' => static::getType(), 'parenttype' => static::$itemtype, static::$items_id => $ID, 'id' => -1);
         Ajax::updateItemJsCode("viewcost" . $ID . "_{$rand}", $CFG_GLPI["root_doc"] . "/ajax/viewsubitem.php", $params);
         echo "};";
         echo "</script>\n";
         if (static::canCreate()) {
             echo "<div class='center firstbloc'>" . "<a class='vsubmit' href='javascript:viewAddCost" . $ID . "_{$rand}();'>";
             echo __('Add a new cost') . "</a></div>\n";
         }
     }
     $total = 0;
     $total_time = 0;
     $total_costtime = 0;
     $total_fixed = 0;
     $total_material = 0;
     if ($result = $DB->query($query)) {
         echo "<table class='tab_cadre_fixehov'>";
         echo "<tr class='noHover'>";
         if ($forproject) {
             echo "<th colspan='10'>" . _n('Ticket cost', 'Ticket costs', $DB->numrows($result)) . "</th>";
         } else {
             echo "<th colspan='7'>" . self::getTypeName($DB->numrows($result)) . "</th>";
             echo "<th>" . __('Item duration') . "</th>";
             echo "<th>" . CommonITILObject::getActionTime($item->fields['actiontime']) . "</th>";
         }
         echo "</tr>";
         if ($DB->numrows($result)) {
             echo "<tr>";
             if ($forproject) {
                 echo "<th>" . __('Ticket') . "</th>";
                 $ticket = new Ticket();
             }
             echo "<th>" . __('Name') . "</th>";
             echo "<th>" . __('Begin date') . "</th>";
             echo "<th>" . __('End date') . "</th>";
             echo "<th>" . __('Budget') . "</th>";
             echo "<th>" . __('Duration') . "</th>";
             echo "<th>" . __('Time cost') . "</th>";
             echo "<th>" . __('Fixed cost') . "</th>";
             echo "<th>" . __('Material cost') . "</th>";
             echo "<th>" . __('Total cost') . "</th>";
             echo "</tr>";
             Session::initNavigateListItems(static::getType(), sprintf(__('%1$s = %2$s'), $item->getTypeName(1), $item->getName()));
             while ($data = $DB->fetch_assoc($result)) {
                 echo "<tr class='tab_bg_2' " . ($canedit ? "style='cursor:pointer' onClick=\"viewEditCost" . $data[static::$items_id] . "_" . $data['id'] . "_{$rand}();\"" : '') . ">";
                 $name = empty($data['name']) ? sprintf(__('%1$s (%2$s)'), $data['name'], $data['id']) : $data['name'];
                 if ($forproject) {
                     $ticket->getFromDB($data['tickets_id']);
                     echo "<td>" . $ticket->getLink() . "</td>";
                 }
                 echo "<td>";
                 printf(__('%1$s %2$s'), $name, Html::showToolTip($data['comment'], array('display' => false)));
                 if ($canedit) {
                     echo "\n<script type='text/javascript' >\n";
                     echo "function viewEditCost" . $data[static::$items_id] . "_" . $data["id"] . "_{$rand}() {\n";
                     $params = array('type' => static::getType(), 'parenttype' => static::$itemtype, static::$items_id => $data[static::$items_id], 'id' => $data["id"]);
                     Ajax::updateItemJsCode("viewcost" . $ID . "_{$rand}", $CFG_GLPI["root_doc"] . "/ajax/viewsubitem.php", $params);
                     echo "};";
                     echo "</script>\n";
                 }
                 echo "</td>";
                 echo "<td>" . Html::convDate($data['begin_date']) . "</td>";
                 echo "<td>" . Html::convDate($data['end_date']) . "</td>";
                 echo "<td>" . Dropdown::getDropdownName('glpi_budgets', $data['budgets_id']) . "</td>";
                 echo "<td>" . CommonITILObject::getActionTime($data['actiontime']) . "</td>";
                 $total_time += $data['actiontime'];
                 echo "<td class='numeric'>" . Html::formatNumber($data['cost_time']) . "</td>";
                 $total_costtime += $data['actiontime'] * $data['cost_time'] / HOUR_TIMESTAMP;
                 echo "<td class='numeric'>" . Html::formatNumber($data['cost_fixed']) . "</td>";
                 $total_fixed += $data['cost_fixed'];
                 echo "<td class='numeric'>" . Html::formatNumber($data['cost_material']) . "</td>";
                 $total_material += $data['cost_material'];
                 $cost = self::computeTotalCost($data['actiontime'], $data['cost_time'], $data['cost_fixed'], $data['cost_material']);
                 echo "<td class='numeric'>" . Html::formatNumber($cost) . "</td>";
                 $total += $cost;
                 echo "</tr>";
                 Session::addToNavigateListItems(static::getType(), $data['id']);
             }
             $colspan = 4;
             if ($forproject) {
                 $colspan++;
             }
             echo "<tr class='b noHover'><td colspan='{$colspan}' class='right'>" . __('Total') . '</td>';
             echo "<td>" . CommonITILObject::getActionTime($total_time) . "</td>";
             echo "<td class='numeric'>" . Html::formatNumber($total_costtime) . "</td>";
             echo "<td class='numeric'>" . Html::formatNumber($total_fixed) . '</td>';
             echo "<td class='numeric'>" . Html::formatNumber($total_material) . '</td>';
             echo "<td class='numeric'>" . Html::formatNumber($total) . '</td></tr>';
         } else {
             echo "<tr><th colspan='9'>" . __('No item found') . "</th></tr>";
         }
         echo "</table>";
     }
     echo "</div><br>";
     return $total;
 }
 /**
  * @since version 0.85
  *
  * @see CommonDBTM::showMassiveActionsSubForm()
  **/
 static function showMassiveActionsSubForm(MassiveAction $ma)
 {
     $action = $ma->getAction();
     $items = $ma->getItems();
     $itemtypes_affect = array();
     $itemtypes_unaffect = array();
     foreach (array_keys($items) as $itemtype) {
         if (!Toolbox::is_a($itemtype, __CLASS__)) {
             continue;
         }
         $specificities = $itemtype::getConnexityMassiveActionsSpecificities();
         if (in_array($action, $specificities['normalized']['affect'])) {
             $itemtypes_affect[$itemtype] = $specificities;
             continue;
         }
         if (in_array($action, $specificities['normalized']['unaffect'])) {
             $itemtypes_unaffect[$itemtype] = $specificities;
             continue;
         }
     }
     if (count($itemtypes_affect) > count($itemtypes_unaffect)) {
         $normalized_action = 'affect';
         $itemtypes = $itemtypes_affect;
     } else {
         if (count($itemtypes_affect) < count($itemtypes_unaffect)) {
             $normalized_action = 'unaffect';
             $itemtypes = $itemtypes_unaffect;
         } else {
             return parent::showMassiveActionsSubForm($ma);
         }
     }
     switch ($normalized_action) {
         case 'unaffect':
             foreach ($itemtypes as $itemtype => $specificities) {
                 if (Toolbox::is_a($itemtype, 'CommonDBRelation')) {
                     $peer_field = "peer[{$itemtype}]";
                     if (!$itemtype::$mustBeAttached_1 && !$itemtype::$mustBeAttached_2) {
                         // Should never occur ... But we must care !
                         $values = array();
                         if (empty($itemtype::$itemtype_1) || preg_match('/^itemtype/', $itemtype::$itemtype_1)) {
                             $values[0] = __('First Item');
                         } else {
                             $itemtype_1 = $itemtype::$itemtype_1;
                             $values[0] = $itemtype_1::getTypeName(Session::getPluralNumber());
                         }
                         if (empty($itemtype::$itemtype_2) || preg_match('/^itemtype/', $itemtype::$itemtype_2)) {
                             $values[1] = __('Second Item');
                         } else {
                             $itemtype_2 = $itemtype::$itemtype_2;
                             $values[1] = $itemtype_2::getTypeName(Session::getPluralNumber());
                         }
                         echo sprintf(__('Select a peer for %s:'), $itemtype::getTypeName());
                         Dropdown::showFromArray($peer_field, $values);
                         echo "<br>\n";
                     } else {
                         if (!$itemtype::$mustBeAttached_1) {
                             echo "<input type='hidden' name='{$peer_field}' value='0'>";
                         } else {
                             if (!$itemtype::$mustBeAttached_2) {
                                 echo "<input type='hidden' name='{$peer_field}' value='1'>";
                             }
                         }
                     }
                 }
             }
             echo "<br><br>" . Html::submit(_x('button', 'Dissociate'), array('name' => 'massiveaction'));
             return true;
         case 'affect':
             $peertypes = array();
             foreach ($itemtypes as $itemtype => $specificities) {
                 if (!$specificities['reaffect']) {
                     continue;
                 }
                 if (Toolbox::is_a($itemtype, 'CommonDBRelation')) {
                     if ($specificities['reaffect'] == 1) {
                         $peertype = $itemtype::$itemtype_1;
                     } else {
                         $peertype = $itemtype::$itemtype_2;
                     }
                 } else {
                     $peertype = $itemtype::$itemtype;
                 }
                 if (preg_match('/^itemtype/', $peertype)) {
                     $peertypes = array_merge($peertypes, $specificities['itemtypes']);
                 } else {
                     $peertypes[] = $peertype;
                 }
             }
             $peertypes = array_unique($peertypes);
             if (count($peertypes) == 0) {
                 echo __('Unable to reaffect given elements !');
                 exit;
             }
             $options = array();
             if (count($peertypes) == 1) {
                 $options['name'] = 'peers_id';
                 $type_for_dropdown = $peertypes[0];
                 if (preg_match('/^itemtype/', $peertype)) {
                     echo Html::hidden('peertype', array('value' => $type_for_dropdown));
                 }
                 $type_for_dropdown::dropdown($options);
             } else {
                 $options['itemtype_name'] = 'peertype';
                 $options['items_id_name'] = 'peers_id';
                 $options['itemtypes'] = $peertypes;
                 Dropdown::showSelectItemFromItemtypes($options);
             }
             echo "<br><br>" . Html::submit(_x('button', 'Associate'), array('name' => 'massiveaction'));
             return true;
     }
     return parent::showMassiveActionsSubForm($ma);
 }