. * --------------------------------------------------------------------- */ if (!defined('GLPI_ROOT')) { die("Sorry. You can't access this file directly"); } /** * Printer Class **/ class Printer extends CommonDBTM { use Glpi\Features\Clonable; // From CommonDBTM public $dohistory = true; static protected $forward_entity_to = ['Infocom', 'NetworkPort', 'ReservationItem', 'Item_OperatingSystem', 'Item_Disk', 'Item_SoftwareVersion']; static $rightname = 'printer'; protected $usenotepad = true; public function getCloneRelations() :array { return [ Item_OperatingSystem::class, Item_Devices::class, Infocom::class, NetworkPort::class, Contract_Item::class, Document_Item::class, Computer_Item::class, KnowbaseItem_Item::class, Cartridge::class ]; } /** * Name of the type * * @param $nb : number of item in the type **/ static function getTypeName($nb = 0) { return _n('Printer', 'Printers', $nb); } /** * @see CommonDBTM::useDeletedToLockIfDynamic() * * @since 0.84 **/ function useDeletedToLockIfDynamic() { return false; } function defineTabs($options = []) { $ong = []; $this->addDefaultFormTab($ong); $this->addImpactTab($ong, $options); $this->addStandardTab('Item_OperatingSystem', $ong, $options); $this->addStandardTab('Item_SoftwareVersion', $ong, $options); $this->addStandardTab('Cartridge', $ong, $options); $this->addStandardTab('Item_Devices', $ong, $options); $this->addStandardTab('Item_Disk', $ong, $options); $this->addStandardTab('Computer_Item', $ong, $options); $this->addStandardTab('NetworkPort', $ong, $options); $this->addStandardTab('Infocom', $ong, $options); $this->addStandardTab('Contract_Item', $ong, $options); $this->addStandardTab('Document_Item', $ong, $options); $this->addStandardTab('KnowbaseItem_Item', $ong, $options); $this->addStandardTab('Ticket', $ong, $options); $this->addStandardTab('Item_Problem', $ong, $options); $this->addStandardTab('Change_Item', $ong, $options); $this->addStandardTab('Link', $ong, $options); $this->addStandardTab('Notepad', $ong, $options); $this->addStandardTab('Reservation', $ong, $options); $this->addStandardTab('Certificate_Item', $ong, $options); $this->addStandardTab('Domain_Item', $ong, $options); $this->addStandardTab('Appliance_Item', $ong, $options); $this->addStandardTab('Log', $ong, $options); return $ong; } /** * Can I change recusvive flag to false * check if there is "linked" object in another entity * * Overloaded from CommonDBTM * * @return boolean **/ function canUnrecurs() { global $DB; $ID = $this->fields['id']; if (($ID < 0) || !$this->fields['is_recursive']) { return true; } if (!parent::canUnrecurs()) { return false; } $entities = getAncestorsOf("glpi_entities", $this->fields['entities_id']); $entities[] = $this->fields['entities_id']; // RELATION : printers -> _port -> _wire -> _port -> device // Evaluate connection in the 2 ways $tabend = ['networkports_id_1' => 'networkports_id_2', 'networkports_id_2' => 'networkports_id_1']; foreach ($tabend as $enda => $endb) { $criteria = [ 'SELECT' => [ 'itemtype', new QueryExpression('GROUP_CONCAT(DISTINCT '.$DB->quoteName('items_id').') AS '.$DB->quoteName('ids')) ], 'FROM' => 'glpi_networkports_networkports', 'INNER JOIN' => [ 'glpi_networkports' => [ 'ON' => [ 'glpi_networkports_networkports' => $endb, 'glpi_networkports' => 'id' ] ] ], 'WHERE' => [ 'glpi_networkports_networkports.'.$enda => new QuerySubQuery([ 'SELECT' => 'id', 'FROM' => 'glpi_networkports', 'WHERE' => [ 'itemtype' => $this->getType(), 'items_id' => $ID ] ]) ], 'GROUPBY' => 'itemtype' ]; $iterator = $DB->request($criteria); while ($data = $iterator->next()) { $itemtable = getTableForItemType($data["itemtype"]); if ($item = getItemForItemtype($data["itemtype"])) { // For each itemtype which are entity dependant if ($item->isEntityAssign()) { if (countElementsInTable($itemtable, ['id' => $data["ids"], 'NOT' => [ 'entities_id' => $entities]]) > 0) { return false; } } } } } return true; } function prepareInputForAdd($input) { if (isset($input["id"]) && ($input["id"] > 0)) { $input["_oldID"]=$input["id"]; } unset($input['id']); unset($input['withtemplate']); if (isset($input['init_pages_counter'])) { $input['init_pages_counter'] = intval($input['init_pages_counter']); } else { $input['init_pages_counter'] = 0; } if (isset($input['last_pages_counter'])) { $input['last_pages_counter'] = intval($input['last_pages_counter']); } else { $input['last_pages_counter'] = $input['init_pages_counter']; } return $input; } function prepareInputForUpdate($input) { if (isset($input['init_pages_counter'])) { $input['init_pages_counter'] = intval($input['init_pages_counter']); } if (isset($input['last_pages_counter'])) { $input['last_pages_counter'] = intval($input['last_pages_counter']); } return $input; } function cleanDBonPurge() { global $DB; $DB->update( 'glpi_cartridges', [ 'printers_id' => 'NULL' ], [ 'printers_id' => $this->fields['id'] ] ); $this->deleteChildrenAndRelationsFromDb( [ Certificate_Item::class, Computer_Item::class, Item_Project::class, ] ); Item_Devices::cleanItemDeviceDBOnItemDelete($this->getType(), $this->fields['id'], (!empty($this->input['keep_devices']))); } /** * Print the printer form * * @param $ID integer ID of the item * @param $options array of possible options: * - target filename : where to go when done. * - withtemplate boolean : template or basic item * *@return boolean item found **/ function showForm($ID, $options = []) { global $CFG_GLPI; $target = $this->getFormURL(); $withtemplate = $this->initForm($ID, $options); $this->showFormHeader($options); $tplmark = $this->getAutofillMark('name', $options); echo ""; //TRANS: %1$s is a string, %2$s a second one without spaces between them : to change for RTL echo "".sprintf(__('%1$s%2$s'), __('Name'), $tplmark). "\n"; echo ""; $objectName = autoName($this->fields["name"], "name", (isset($options['withtemplate']) && ($options['withtemplate'] == 2)), $this->getType(), $this->fields["entities_id"]); Html::autocompletionTextField($this, 'name', ['value' => $objectName]); echo "\n"; echo "".__('Status')."\n"; echo ""; State::dropdown([ 'value' => $this->fields["states_id"], 'entity' => $this->fields["entities_id"], 'condition' => ['is_visible_printer' => 1] ]); echo "\n"; echo ""; echo "".Location::getTypeName(1)."\n"; echo ""; Location::dropdown(['value' => $this->fields["locations_id"], 'entity' => $this->fields["entities_id"]]); echo "\n"; echo ""._n('Type', 'Types', 1)."\n"; echo ""; PrinterType::dropdown(['value' => $this->fields["printertypes_id"]]); echo "\n"; echo ""; echo "".__('Technician in charge of the hardware')."\n"; echo ""; User::dropdown(['name' => 'users_id_tech', 'value' => $this->fields["users_id_tech"], 'right' => 'own_ticket', 'entity' => $this->fields["entities_id"]]); echo "\n"; echo "".Manufacturer::getTypeName(1)."\n"; echo ""; Manufacturer::dropdown(['value' => $this->fields["manufacturers_id"]]); echo "\n"; echo ""; echo "".__('Group in charge of the hardware').""; echo ""; Group::dropdown([ 'name' => 'groups_id_tech', 'value' => $this->fields['groups_id_tech'], 'entity' => $this->fields['entities_id'], 'condition' => ['is_assign' => 1] ]); echo ""; echo ""._n('Model', 'Models', 1)."\n"; echo ""; PrinterModel::dropdown(['value' => $this->fields["printermodels_id"]]); echo "\n"; echo ""; echo "".__('Alternate username number')."\n"; echo ""; Html::autocompletionTextField($this, "contact_num"); echo "\n"; echo "".__('Serial number')."\n"; echo ""; Html::autocompletionTextField($this, "serial"); echo "\n"; echo ""; echo "".__('Alternate username')."\n"; echo ""; Html::autocompletionTextField($this, "contact"); echo "\n"; $tplmark = $this->getAutofillMark('otherserial', $options); echo "".sprintf(__('%1$s%2$s'), __('Inventory number'), $tplmark). "\n"; echo ""; $objectName = autoName($this->fields["otherserial"], "otherserial", (isset($options['withtemplate']) && ($options['withtemplate'] == 2)), $this->getType(), $this->fields["entities_id"]); Html::autocompletionTextField($this, 'otherserial', ['value' => $objectName]); echo "\n"; echo ""; echo "".User::getTypeName(1)."\n"; echo ""; User::dropdown(['value' => $this->fields["users_id"], 'entity' => $this->fields["entities_id"], 'right' => 'all']); echo "\n"; echo "".__('Management type').""; echo ""; $globalitem = []; $globalitem['withtemplate'] = $withtemplate; $globalitem['value'] = $this->fields["is_global"]; $globalitem['target'] = $target; if ($this->can($ID, UPDATE)) { $globalitem['management_restrict'] = $CFG_GLPI["printers_management_restrict"]; } Dropdown::showGlobalSwitch($this->fields["id"], $globalitem); echo "\n"; echo ""; echo "".Group::getTypeName(1)."\n"; echo ""; Group::dropdown([ 'value' => $this->fields["groups_id"], 'entity' => $this->fields["entities_id"], 'condition' => ['is_itemgroup' => 1] ]); echo "\n"; echo ""._n('Network', 'Networks', 1)."\n"; echo ""; Network::dropdown(['value' => $this->fields["networks_id"]]); echo "\n"; // Display auto inventory informations $rowspan = 5; echo ""; echo "\n"; echo "".__('Comments')."\n"; echo ""; echo "\n"; echo ""; echo ""._n('Memory', 'Memories', 1)."\n"; echo ""; Html::autocompletionTextField($this, "memory_size"); echo "\n"; echo ""; echo "".__('Initial page counter')."\n"; echo ""; Html::autocompletionTextField($this, "init_pages_counter", ['size' => 10]); echo "\n"; echo ""; echo "".__('Current counter of pages')."\n"; echo ""; Html::autocompletionTextField($this, "last_pages_counter", ['size' => 10]); echo "\n"; echo ""; echo ""._n('Port', 'Ports', Session::getPluralNumber()).""; echo "\n"; // serial interface echo ""; // parallel interface? echo ""; // USB interface? echo ""; // ethernet interface? echo ""; // wifi ? echo "
".__('Serial').""; Dropdown::showYesNo("have_serial", $this->fields["have_serial"]); echo "".__('Parallel').""; Dropdown::showYesNo("have_parallel", $this->fields["have_parallel"]); echo "
".__('USB').""; Dropdown::showYesNo("have_usb", $this->fields["have_usb"]); echo "".__('Ethernet').""; Dropdown::showYesNo("have_ethernet", $this->fields["have_ethernet"]); echo "
".__('Wifi').""; Dropdown::showYesNo("have_wifi", $this->fields["have_wifi"]); echo "
\n"; echo ""; echo ""; // Display auto inventory informations if (!empty($ID) && $this->fields["is_dynamic"]) { echo ""; Plugin::doHook("autoinventory_information", $this); echo ""; } $this->showFormButtons($options); return true; } /** * Return the linked items (in computers_items) * * @return an array of linked items like array('Computer' => array(1,2), 'Printer' => array(5,6)) * @since 0.84.4 **/ function getLinkedItems() { global $DB; $iterator = $DB->request([ 'SELECT' => 'computers_id', 'FROM' => 'glpi_computers_items', 'WHERE' => [ 'itemtype' => $this->getType(), 'items_id' => $this->fields['id'] ] ]); $tab = []; while ($data = $iterator->next()) { $tab['Computer'][$data['computers_id']] = $data['computers_id']; } return $tab; } /** * @see CommonDBTM::getSpecificMassiveActions() **/ function getSpecificMassiveActions($checkitem = null) { $actions = parent::getSpecificMassiveActions($checkitem); if (static::canUpdate()) { Computer_Item::getMassiveActionsForItemtype($actions, __CLASS__, 0, $checkitem); KnowbaseItem_Item::getMassiveActionsForItemtype($actions, __CLASS__, 0, $checkitem); } return $actions; } function rawSearchOptions() { $tab = parent::rawSearchOptions(); $tab[] = [ 'id' => '2', 'table' => $this->getTable(), 'field' => 'id', 'name' => __('ID'), 'massiveaction' => false, 'datatype' => 'number' ]; $tab = array_merge($tab, Location::rawSearchOptionsToAdd()); $tab[] = [ 'id' => '4', 'table' => 'glpi_printertypes', 'field' => 'name', 'name' => _n('Type', 'Types', 1), 'datatype' => 'dropdown' ]; $tab[] = [ 'id' => '40', 'table' => 'glpi_printermodels', 'field' => 'name', 'name' => _n('Model', 'Models', 1), 'datatype' => 'dropdown' ]; $tab[] = [ 'id' => '31', 'table' => 'glpi_states', 'field' => 'completename', 'name' => __('Status'), 'datatype' => 'dropdown', 'condition' => ['is_visible_printer' => 1] ]; $tab[] = [ 'id' => '5', 'table' => $this->getTable(), 'field' => 'serial', 'name' => __('Serial number'), 'datatype' => 'string', 'autocomplete' => true, ]; $tab[] = [ 'id' => '6', 'table' => $this->getTable(), 'field' => 'otherserial', 'name' => __('Inventory number'), 'datatype' => 'string', 'autocomplete' => true, ]; $tab[] = [ 'id' => '7', 'table' => $this->getTable(), 'field' => 'contact', 'name' => __('Alternate username'), 'datatype' => 'string', 'autocomplete' => true, ]; $tab[] = [ 'id' => '8', 'table' => $this->getTable(), 'field' => 'contact_num', 'name' => __('Alternate username number'), 'datatype' => 'string', 'autocomplete' => true, ]; $tab[] = [ 'id' => '70', 'table' => 'glpi_users', 'field' => 'name', 'name' => User::getTypeName(1), 'datatype' => 'dropdown', 'right' => 'all' ]; $tab[] = [ 'id' => '71', 'table' => 'glpi_groups', 'field' => 'completename', 'name' => Group::getTypeName(1), 'condition' => ['is_itemgroup' => 1], 'datatype' => 'dropdown' ]; $tab[] = [ 'id' => '19', 'table' => $this->getTable(), 'field' => 'date_mod', 'name' => __('Last update'), 'datatype' => 'datetime', 'massiveaction' => false ]; $tab[] = [ 'id' => '121', 'table' => $this->getTable(), 'field' => 'date_creation', 'name' => __('Creation date'), 'datatype' => 'datetime', 'massiveaction' => false ]; $tab[] = [ 'id' => '16', 'table' => $this->getTable(), 'field' => 'comment', 'name' => __('Comments'), 'datatype' => 'text' ]; $tab[] = [ 'id' => '42', 'table' => $this->getTable(), 'field' => 'have_serial', 'name' => __('Serial'), 'datatype' => 'bool' ]; $tab[] = [ 'id' => '43', 'table' => $this->getTable(), 'field' => 'have_parallel', 'name' => __('Parallel'), 'datatype' => 'bool' ]; $tab[] = [ 'id' => '44', 'table' => $this->getTable(), 'field' => 'have_usb', 'name' => __('USB'), 'datatype' => 'bool' ]; $tab[] = [ 'id' => '45', 'table' => $this->getTable(), 'field' => 'have_ethernet', 'name' => __('Ethernet'), 'datatype' => 'bool' ]; $tab[] = [ 'id' => '46', 'table' => $this->getTable(), 'field' => 'have_wifi', 'name' => __('Wifi'), 'datatype' => 'bool' ]; $tab[] = [ 'id' => '13', 'table' => $this->getTable(), 'field' => 'memory_size', 'name' => _n('Memory', 'Memories', 1), 'datatype' => 'string', 'autocomplete' => true, ]; $tab[] = [ 'id' => '11', 'table' => $this->getTable(), 'field' => 'init_pages_counter', 'name' => __('Initial page counter'), 'datatype' => 'number', 'nosearch' => true, 'autocomplete' => true, ]; $tab[] = [ 'id' => '12', 'table' => $this->getTable(), 'field' => 'last_pages_counter', 'name' => __('Current counter of pages'), 'datatype' => 'number', 'autocomplete' => true, ]; $tab[] = [ 'id' => '9', 'table' => $this->getTable(), 'field' => '_virtual', 'linkfield' => '_virtual', 'name' => _n('Cartridge', 'Cartridges', Session::getPluralNumber()), 'datatype' => 'specific', 'massiveaction' => false, 'nosearch' => true, 'nosort' => true ]; $tab[] = [ 'id' => '17', 'table' => 'glpi_cartridges', 'field' => 'id', 'name' => __('Number of used cartridges'), 'datatype' => 'count', 'forcegroupby' => true, 'usehaving' => true, 'massiveaction' => false, 'joinparams' => [ 'jointype' => 'child', 'condition' => 'AND NEWTABLE.`date_use` IS NOT NULL AND NEWTABLE.`date_out` IS NULL' ] ]; $tab[] = [ 'id' => '18', 'table' => 'glpi_cartridges', 'field' => 'id', 'name' => __('Number of worn cartridges'), 'datatype' => 'count', 'forcegroupby' => true, 'usehaving' => true, 'massiveaction' => false, 'joinparams' => [ 'jointype' => 'child', 'condition' => 'AND NEWTABLE.`date_out` IS NOT NULL' ] ]; $tab[] = [ 'id' => '32', 'table' => 'glpi_networks', 'field' => 'name', 'name' => _n('Network', 'Networks', 1), 'datatype' => 'dropdown' ]; $tab[] = [ 'id' => '23', 'table' => 'glpi_manufacturers', 'field' => 'name', 'name' => Manufacturer::getTypeName(1), 'datatype' => 'dropdown' ]; $tab[] = [ 'id' => '24', 'table' => 'glpi_users', 'field' => 'name', 'linkfield' => 'users_id_tech', 'name' => __('Technician in charge of the hardware'), 'datatype' => 'dropdown', 'right' => 'own_ticket' ]; $tab[] = [ 'id' => '49', 'table' => 'glpi_groups', 'field' => 'completename', 'linkfield' => 'groups_id_tech', 'name' => __('Group in charge of the hardware'), 'condition' => ['is_assign' => 1], 'datatype' => 'dropdown' ]; $tab[] = [ 'id' => '61', 'table' => $this->getTable(), 'field' => 'template_name', 'name' => __('Template name'), 'datatype' => 'text', 'massiveaction' => false, 'nosearch' => true, 'nodisplay' => true, 'autocomplete' => true, ]; $tab[] = [ 'id' => '80', 'table' => 'glpi_entities', 'field' => 'completename', 'name' => Entity::getTypeName(1), 'massiveaction' => false, 'datatype' => 'dropdown' ]; $tab[] = [ 'id' => '82', 'table' => $this->getTable(), 'field' => 'is_global', 'name' => __('Global management'), 'datatype' => 'bool', 'massiveaction' => false ]; // add objectlock search options $tab = array_merge($tab, ObjectLock::rawSearchOptionsToAdd(get_class($this))); $tab = array_merge($tab, Notepad::rawSearchOptionsToAdd()); $tab = array_merge($tab, Item_Devices::rawSearchOptionsToAdd(get_class($this))); return $tab; } /** * Add a printer. If already exist in trashbin restore it * * @param $name the printer's name (need to be addslashes) * @param $manufacturer the software's manufacturer (need to be addslashes) * @param $entity the entity in which the software must be added * @param $comment comment (default '') **/ function addOrRestoreFromTrash($name, $manufacturer, $entity, $comment = '') { global $DB; //Look for the software by his name in GLPI for a specific entity $iterator = $DB->request([ 'SELECT' => ['id', 'is_deleted'], 'FROM' => self::getTable(), 'WHERE' => [ 'name' => $name, 'is_template' => 0, 'entities_id' => $entity ] ]); if (count($iterator) > 0) { //Printer already exists for this entity, get its ID $data = $iterator->next(); $ID = $data["id"]; // restore software if ($data['is_deleted']) { $this->removeFromTrash($ID); } } else { $ID = 0; } if (!$ID) { $ID = $this->addPrinter($name, $manufacturer, $entity, $comment); } return $ID; } /** * Create a new printer * * @param $name the printer's name (need to be addslashes) * @param $manufacturer the printer's manufacturer (need to be addslashes) * @param $entity the entity in which the printer must be added * @param $comment (default '') * * @return the printer's ID **/ function addPrinter($name, $manufacturer, $entity, $comment = '') { global $DB; $manufacturer_id = 0; if ($manufacturer != '') { $manufacturer_id = Dropdown::importExternal('Manufacturer', $manufacturer); } //If there's a printer in a parent entity with the same name and manufacturer $iterator = $DB->request([ 'SELECT' => 'id', 'FROM' => self::getTable(), 'WHERE' => [ 'manufacturers_id' => $manufacturer_id, 'name' => $name, ] + getEntitiesRestrictCriteria(self::getTable, 'entities_id', $entity, true) ]); if ($printer = $iterator->next()) { $id = $printer["id"]; } else { $input["name"] = $name; $input["manufacturers_id"] = $manufacturer_id; $input["entities_id"] = $entity; $id = $this->add($input); } return $id; } /** * Restore a software from trashbin * * @param $ID the ID of the software to put in trashbin * * @return boolean (success) **/ function removeFromTrash($ID) { return $this->restore(["id" => $ID]); } static function getIcon() { return "fas fa-print"; } }