/** * Replay collection rules on an existing DB for model dropdowns * * @param $offset offset used to begin (default 0) * @param $maxtime maximum time of process (reload at the end) (default 0) * * @return -1 on completion else current offset **/ function replayRulesOnExistingDBForModel($offset = 0, $maxtime = 0) { global $DB; if (isCommandLine()) { printf(__('Replay rules on existing database started on %s') . "\n", date("r")); } // Model check : need to check using manufacturer extra data if (strpos($this->item_table, 'models') === false) { _e('Error replaying rules'); return false; } $model_table = getPlural(str_replace('models', '', $this->item_table)); $model_field = getForeignKeyFieldForTable($this->item_table); // Need to give manufacturer from item table $Sql = "SELECT DISTINCT `glpi_manufacturers`.`id` AS idmanu,\n `glpi_manufacturers`.`name` AS manufacturer,\n `" . $this->item_table . "`.`id`,\n `" . $this->item_table . "`.`name` AS name,\n `" . $this->item_table . "`.`comment`\n FROM `" . $this->item_table . "`,\n `{$model_table}`\n LEFT JOIN `glpi_manufacturers`\n ON (`{$model_table}`.`manufacturers_id` = `glpi_manufacturers`.`id`)\n WHERE `{$model_table}`.`{$model_field}` = `" . $this->item_table . "`.`id`"; if ($offset) { $Sql .= " LIMIT " . intval($offset) . ",999999999"; } $result = $DB->query($Sql); $nb = $DB->numrows($result) + $offset; $i = $offset; if ($result && $nb > $offset) { // Step to refresh progressbar $step = $nb > 20 ? floor($nb / 20) : 1; $tocheck = array(); while ($data = $DB->fetch_assoc($result)) { if (!($i % $step)) { if (isCommandLine()) { printf(__('Replay rules on existing database: %1$s/%2$s') . "\r", $i, $nb); } else { Html::changeProgressBarPosition($i, $nb, "{$i} / {$nb}"); } } // Model case if (isset($data["manufacturer"])) { $data["manufacturer"] = Manufacturer::processName(addslashes($data["manufacturer"])); } //Replay Type dictionnary $ID = Dropdown::importExternal(getItemTypeForTable($this->item_table), addslashes($data["name"]), -1, $data, addslashes($data["comment"])); if ($data['id'] != $ID) { $tocheck[$data["id"]][] = $ID; $sql = "UPDATE `{$model_table}`\n SET `{$model_field}` = '{$ID}'\n WHERE `{$model_field}` = '" . $data['id'] . "'"; if (empty($data['idmanu'])) { $sql .= " AND (`manufacturers_id` IS NULL\n OR `manufacturers_id` = '0')"; } else { $sql .= " AND `manufacturers_id` = '" . $data['idmanu'] . "'"; } $DB->query($sql); } $i++; if ($maxtime) { $crt = explode(" ", microtime()); if ($crt[0] + $crt[1] > $maxtime) { break; } } } foreach ($tocheck as $ID => $tab) { $sql = "SELECT COUNT(*)\n FROM `{$model_table}`\n WHERE `{$model_field}` = '{$ID}'"; $result = $DB->query($sql); $deletecartmodel = false; // No item left : delete old item if ($result && $DB->result($result, 0, 0) == 0) { $Sql = "DELETE\n FROM `" . $this->item_table . "`\n WHERE `id` = '{$ID}'"; $resdel = $DB->query($Sql); $deletecartmodel = true; } // Manage cartridge assoc Update items if ($this->getRuleClassName() == 'RuleDictionnaryPrinterModel') { $sql = "SELECT *\n FROM `glpi_cartridgeitems_printermodels`\n WHERE `printermodels_id` = '{$ID}'"; if ($result = $DB->query($sql)) { if ($DB->numrows($result)) { // Get compatible cartridge type $carttype = array(); while ($data = $DB->fetch_assoc($result)) { $carttype[] = $data['cartridgeitems_id']; } // Delete cartrodges_assoc if ($deletecartmodel) { $sql = "DELETE\n FROM `glpi_cartridgeitems_printermodels`\n WHERE `printermodels_id` = 'id'"; $DB->query($sql); } // Add new assoc $ct = new CartridgeItem(); foreach ($carttype as $cartID) { foreach ($tab as $model) { $ct->addCompatibleType($cartID, $model); } } } } } } // each tocheck } if (isCommandLine()) { printf(__('Replay rules on existing database ended on %s') . "\n", date("r")); } else { Html::changeProgressBarPosition($i, $nb, "{$i} / {$nb}"); } return $i == $nb ? -1 : $i; }
/** * @covers ::getPlural * @dataProvider dataPlural **/ public function testGetPlural($singular, $plural) { $this->assertEquals($plural, getPlural($singular)); $this->assertEquals($plural, getPlural(getPlural($singular))); }
/** * Return table name for a specific itemtype * * @param $itemtype Type of item * @return string Name of table */ function plugin_customfields_table($itemtype) { return 'glpi_plugin_customfields_' . strtolower(getPlural($itemtype)); }
$expires = ""; if ($result["deleteafter"] > 0) { $postdate = strtotime($result["time"]); $expiredate = $postdate + $result["deleteafter"] * 3600; $seconds_left = $expiredate - date("U"); if ($seconds_left <= 0) { $expires = 'This paste has expired and is pending deletion.'; } else { $expires = 'This paste expires in '; if ($seconds_left >= 86400) { $days_left = floor($seconds_left / 86400); $expires .= $days_left . getPlural($days_left, ' day', ' days'); } else { $hours_left = floor($seconds_left / 3600); $hours_secleft = floor($seconds_left / 60) % 60; $expires .= ($hours_left > 0 ? $hours_left . getPlural($hours_left, ' hour', ' hours') : '') . ($hours_secleft > 0 ? ' and ' . $hours_secleft . getPlural($hours_secleft, ' minute', ' minutes') : ''); } $expires .= ' from now.'; } } include "includes/page/header.php"; ?> <?php if (!empty($expires)) { ?> <div id="expire" class="alert alert-info"> <?php echo $expires; ?> </div>
/** * Link inventory object to another one * * @param $params the input parameters * @param $protocol the commonication protocol used * **/ static function methodLinkObjects($params, $protocol) { global $CFG_GLPI; if (isset($params['help'])) { return array('fields' => 'array, mandatory', 'help' => 'bool, optional'); } if (!Session::getLoginUserID()) { return self::Error($protocol, WEBSERVICES_ERROR_NOTAUTHENTICATED); } //Must be superadmin to use this method if (!Session::haveRight('config', UPDATE)) { return self::Error($protocol, WEBSERVICES_ERROR_NOTALLOWED); } if (!isset($params['fields']) || empty($params['fields']) || !is_array($params['fields'])) { return self::Error($protocol, WEBSERVICES_ERROR_MISSINGPARAMETER, '', 'fields'); } if (!isset($_SESSION["glpi_currenttime"])) { $_SESSION["glpi_currenttime"] = date("Y-m-d H:i:s"); } $resp = array(); $errors = array(); foreach ($params['fields'] as $links) { if (!in_array($links['from_item']['itemtype'], array('Computer')) && !preg_match("/Device/", $links['from_item']['itemtype'])) { $errors[] = self::Error($protocol, WEBSERVICES_ERROR_NOTALLOWED, '', self::getDisplayError()); } switch ($links['from_item']['itemtype']) { case 'Computer': // Direct connections if (in_array($links['to_item']['itemtype'], array('Monitor', 'Peripheral', 'Phone', 'Printer'))) { $comp_item = new Computer_Item(); $data = array(); $data['items_id'] = $links['to_item']['id']; $data['computers_id'] = $links['from_item']['id']; $data['itemtype'] = $links['to_item']['itemtype']; if (!$comp_item->can(-1, UPDATE, $data)) { $errors[] = self::Error($protocol, WEBSERVICES_ERROR_NOTALLOWED, '', self::getDisplayError()); } else { if ($comp_item->add($data)) { $resp['Computer'][$data['computers_id']] = self::methodGetObject(array('itemtype' => 'Computer', 'id' => $data['computers_id'], 'with_printer' => 1, 'with_monitor' => 1, 'with_phone' => 1, 'with_peripheral' => 1), $protocol); } else { $errors[] = self::Error($protocol, WEBSERVICES_ERROR_FAILED, '', self::getDisplayError()); } } } // Device connection if (preg_match("/Device/", $links['to_item']['itemtype'])) { $comp_device = new Computer_Device(); $links_field = getPlural(strtolower($links['to_item']['itemtype'])) . "_id"; $data = array(); $data['computers_id'] = $links['from_item']['id']; $data[$links_field] = $links['to_item']['id']; $data['itemtype'] = $links['to_item']['itemtype']; if (!isset($links['to_item']['quantity']) || !is_numeric($links['to_item']['quantity'])) { $quantity = 1; } else { $quantity = $links['to_item']['quantity']; } if (isset($links['to_item']['specificity'])) { if (!is_numeric($links['to_item']['specificity'])) { $errors[] = self::Error($protocol, WEBSERVICES_ERROR_MISSINGPARAMETER, '', 'specificity'); } else { $data['specificity'] = $links['to_item']['specificity']; } } $linked = false; for ($i = 0; $i < $quantity; $i++) { if (!$comp_device->can(-1, UPDATE, $data)) { $errors[] = self::Error($protocol, WEBSERVICES_ERROR_NOTALLOWED, '', self::getDisplayError()); } else { if ($comp_device->add($data)) { $linked = true; } } } if ($linked) { $resp['Computer'][$data['computers_id']] = self::methodGetObject(array('itemtype' => 'Computer', 'id' => $data['computers_id']), $protocol); } else { $errors[] = self::Error($protocol, WEBSERVICES_ERROR_FAILED, '', self::getDisplayError()); } } //other link object break; //itemtype } } if (count($errors)) { $resp = array($resp, $errors); } return $resp; }
/** * Return ItemType for a table * * @param $itemtype string itemtype * * @return string table name corresponding to the itemtype parameter **/ function getTableForItemType($itemtype) { global $CFG_GLPI; // Force singular for itemtype : States case $itemtype = getSingular($itemtype); if (isset($CFG_GLPI['glpitablesitemtype'][$itemtype])) { return $CFG_GLPI['glpitablesitemtype'][$itemtype]; } else { $prefix = "glpi_"; if ($plug = isPluginItemType($itemtype)) { $prefix .= "plugin_" . strtolower($plug['plugin']) . "_"; $table = strtolower($plug['class']); } else { $table = strtolower($itemtype); } if (strstr($table, '_')) { $split = explode('_', $table); foreach ($split as $key => $part) { $split[$key] = getPlural($part); } $table = implode('_', $split); } else { $table = getPlural($table); } $CFG_GLPI['glpitablesitemtype'][$itemtype] = $prefix . $table; $CFG_GLPI['glpiitemtypetables'][$prefix . $table] = $itemtype; return $prefix . $table; } }
/** * Get all data needed for template processing * * @param $event * @param $options array **/ function getDatasForTemplate($event, $options = array()) { global $CFG_GLPI; $events = $this->getAllEvents(); $objettype = strtolower($this->obj->getType()); // Get datas from ITIL objects if ($event != 'alertnotclosed') { $this->datas = $this->getDatasForObject($this->obj, $options); } else { if (isset($options['entities_id']) && isset($options['items'])) { $entity = new Entity(); if ($entity->getFromDB($options['entities_id'])) { $this->datas["##{$objettype}.entity##"] = $entity->getField('completename'); $this->datas["##{$objettype}.shortentity##"] = $entity->getField('name'); } if ($item = getItemForItemtype($objettype)) { $objettypes = Toolbox::strtolower(getPlural($objettype)); $items = array(); foreach ($options['items'] as $object) { $item->getFromDB($object['id']); $tmp = $this->getDatasForObject($item, $options, true); $this->datas[$objettypes][] = $tmp; } } } } if ($event == 'validation' && isset($options['validation_status'])) { $this->datas["##{$objettype}.action##"] = sprintf(__('%1$s - %2$s'), __('Approval'), TicketValidation::getStatus($options['validation_status'])); } else { $this->datas["##{$objettype}.action##"] = $events[$event]; } $this->getTags(); foreach ($this->tag_descriptions[parent::TAG_LANGUAGE] as $tag => $values) { if (!isset($this->datas[$tag])) { $this->datas[$tag] = $values['label']; } } }
echo "<P>You have scored <FONT COLOR='#000099'><B>{$nrcorrect}</B></FONT> out of <FONT COLOR='#000099'><B>{$nrquestions}</B></FONT>. Initially, you scored<FONT COLOR='#000099'><B>{$initialscore}</B></FONT> out of <FONT COLOR='#000099'><b>{$nrquestions}</B></FONT>. It looks like there's still room for improvement.</P>"; } } } $finished = true; } else { if ($nrquestions == $nrcorrect) { echo "<P><FONT COLOR='#006600'><B>Congratulations, you answered all {$allQuestions} questions correctly!</B></FONT></P>"; } else { if ($nrquestions == $unanswered) { echo "<P>You left all {$unanswered} questions unanswered. Please try the quiz again!"; } else { echo "<P>You correctly answered <FONT COLOR='#000099'><B> {$nrcorrect}"; echo "</B></FONT> out of <FONT COLOR='#000099'><B>{$answered}</B></FONT> question" . getPlural($answered); if ($unanswered != 0) { echo ", and you left <FONT COLOR='#000099'><B>{$unanswered}</B></FONT> question" . getPlural($unanswered) . " unanswered"; } echo ".</P><P>If you got less than 50%, you might want to try the quiz again! Otherwise, proceed to the next chapter.</P>"; } } //if($nrquestions != $nrcorrect) // echo "<P>Please try the quiz again!</P>"; } echo "</TD></TR></TABLE></TD></TR></TABLE></TD></TR></TABLE></TD></TR></TABLE><br>"; if (isset($finished)) { return; } } ?> <form name="quizform" method="post" action="<?php
static function getAddSearchOptions($itemtype, $containers_id = false) { global $DB; $opt = array(); $where = ""; if ($containers_id !== false) { $where = "AND containers.id = {$containers_id}"; } $i = 76665; $query = "SELECT fields.name, fields.label, fields.type, fields.is_readonly,\n containers.name as container_name, containers.label as container_label, \n containers.itemtype\n FROM glpi_plugin_fields_containers containers\n INNER JOIN glpi_plugin_fields_fields fields\n ON containers.id = fields.plugin_fields_containers_id\n AND containers.is_active = 1\n WHERE containers.itemtype = '{$itemtype}'\n AND fields.type != 'header'\n {$where}\n ORDER BY fields.id ASC"; $res = $DB->query($query); while ($datas = $DB->fetch_assoc($res)) { $tablename = "glpi_plugin_fields_" . strtolower($datas['itemtype'] . getPlural(preg_replace('/s$/', '', $datas['container_name']))); $opt[$i]['table'] = $tablename; $opt[$i]['field'] = $datas['name']; $opt[$i]['name'] = $datas['container_label'] . " - " . $datas['label']; $opt[$i]['linkfield'] = $datas['name']; $opt[$i]['joinparams']['jointype'] = "itemtype_item"; $opt[$i]['pfields_type'] = $datas['type']; if ($datas['is_readonly']) { $opt[$i]['massiveaction'] = false; } if ($datas['type'] === "dropdown") { $opt[$i]['table'] = 'glpi_plugin_fields_' . $datas['name'] . 'dropdowns'; $opt[$i]['field'] = 'name'; $opt[$i]['linkfield'] = "plugin_fields_" . $datas['name'] . "dropdowns_id"; $opt[$i]['searchtype'] = 'equals'; $opt[$i]['forcegroupby'] = true; $opt[$i]['joinparams']['jointype'] = ""; $opt[$i]['joinparams']['beforejoin']['table'] = $tablename; $opt[$i]['joinparams']['beforejoin']['joinparams']['jointype'] = "itemtype_item"; } if ($datas['type'] === "dropdownuser") { $opt[$i]['table'] = 'glpi_users'; $opt[$i]['field'] = 'name'; $opt[$i]['linkfield'] = $datas['name']; $opt[$i]['forcegroupby'] = true; $opt[$i]['joinparams']['jointype'] = ""; $opt[$i]['joinparams']['beforejoin']['table'] = $tablename; $opt[$i]['joinparams']['beforejoin']['joinparams']['jointype'] = "itemtype_item"; } switch ($datas['type']) { case 'dropdown': case 'dropdownuser': $opt[$i]['datatype'] = "dropdown"; break; case 'yesno': $opt[$i]['datatype'] = "bool"; break; case 'textarea': $opt[$i]['datatype'] = "text"; break; case 'number': $opt[$i]['datatype'] = "number"; break; case 'date': case 'datetime': $opt[$i]['datatype'] = $datas['type']; break; default: $opt[$i]['datatype'] = "string"; } $i++; } return $opt; }
static function getAddSearchOptions($itemtype, $containers_id = false) { global $DB; $opt = array(); $i = 76665; $query = "SELECT fields.name, fields.label, fields.type, fields.is_readonly,\n containers.name as container_name, containers.label as container_label,\n containers.itemtypes, containers.id as container_id, fields.id as field_id\n FROM glpi_plugin_fields_containers containers\n INNER JOIN glpi_plugin_fields_fields fields\n ON containers.id = fields.plugin_fields_containers_id\n AND containers.is_active = 1\n WHERE containers.itemtypes LIKE '%{$itemtype}%'\n AND fields.type != 'header'\n ORDER BY fields.id ASC"; $res = $DB->query($query); while ($data = $DB->fetch_assoc($res)) { if ($containers_id !== false) { // Filter by container (don't filter by SQL for have $i value with few containers for a itemtype) if ($data['container_id'] != $containers_id) { $i++; continue; } } $tablename = "glpi_plugin_fields_" . strtolower($itemtype . getPlural(preg_replace('/s$/', '', $data['container_name']))); //get translations $container = ['itemtype' => PluginFieldsContainer::getType(), 'id' => $data['container_id'], 'label' => $data['container_label']]; $data['container_label'] = PluginFieldsLabelTranslation::getLabelFor($container); $field = ['itemtype' => PluginFieldsField::getType(), 'id' => $data['field_id'], 'label' => $data['label']]; $data['label'] = PluginFieldsLabelTranslation::getLabelFor($field); $opt[$i]['table'] = $tablename; $opt[$i]['field'] = $data['name']; $opt[$i]['name'] = $data['container_label'] . " - " . $data['label']; $opt[$i]['linkfield'] = $data['name']; $opt[$i]['joinparams']['jointype'] = "itemtype_item"; $opt[$i]['pfields_type'] = $data['type']; if ($data['is_readonly']) { $opt[$i]['massiveaction'] = false; } if ($data['type'] === "dropdown") { $opt[$i]['table'] = 'glpi_plugin_fields_' . $data['name'] . 'dropdowns'; $opt[$i]['field'] = 'completename'; $opt[$i]['linkfield'] = "plugin_fields_" . $data['name'] . "dropdowns_id"; $opt[$i]['forcegroupby'] = true; $opt[$i]['joinparams']['jointype'] = ""; $opt[$i]['joinparams']['beforejoin']['table'] = $tablename; $opt[$i]['joinparams']['beforejoin']['joinparams']['jointype'] = "itemtype_item"; } if ($data['type'] === "dropdownuser") { $opt[$i]['table'] = 'glpi_users'; $opt[$i]['field'] = 'name'; $opt[$i]['linkfield'] = $data['name']; $opt[$i]['right'] = 'all'; $opt[$i]['forcegroupby'] = true; $opt[$i]['joinparams']['jointype'] = ""; $opt[$i]['joinparams']['beforejoin']['table'] = $tablename; $opt[$i]['joinparams']['beforejoin']['joinparams']['jointype'] = "itemtype_item"; } switch ($data['type']) { case 'dropdown': case 'dropdownuser': $opt[$i]['datatype'] = "dropdown"; break; case 'yesno': $opt[$i]['datatype'] = "bool"; break; case 'textarea': $opt[$i]['datatype'] = "text"; break; case 'number': $opt[$i]['datatype'] = "number"; break; case 'date': case 'datetime': $opt[$i]['datatype'] = $data['type']; break; default: $opt[$i]['datatype'] = "string"; } $i++; } return $opt; }