/** * Get all dropdown fields associated with an itemtype * @param itemtype the itemtype * @return an array or fields that represents the dropdown tables */ static function getDropdownForItemtype($itemtype) { global $DB; $associated_tables = array(); if (class_exists($itemtype)) { $source_table = getTableForItemType($itemtype); foreach (PluginGenericobjectSingletonObjectField::getInstance($itemtype) as $field => $value) { $table = getTableNameForForeignKeyField($field); //If it's a drodpdown if ($table && preg_match("/" . getSingular($source_table) . "/", $table)) { $associated_tables[] = $table; } } } return $associated_tables; }
function transfer($new_entity) { global $DB; if ($this->fields['id'] > 0 && $this->fields['entities_id'] != $new_entity) { //Update entity for this object $tmp['id'] = $this->fields['id']; $tmp['entities_id'] = $new_entity; $this->update($tmp); $toupdate = array('id' => $this->fields['id']); foreach (PluginGenericobjectSingletonObjectField::getInstance(get_called_class()) as $field => $data) { $table = getTableNameForForeignKeyField($field); //It is a dropdown table ! if ($field != 'entities_id' && $table != '' && isset($this->fields[$field]) && $this->fields[$field] > 0) { //Instanciate a new dropdown object $dropdown_itemtype = getItemTypeForTable($table); $dropdown = new $dropdown_itemtype(); $dropdown->getFromDB($this->fields[$field]); //If dropdown is only accessible in the other entity //do not go further if (!$dropdown->isEntityAssign() || in_array($new_entity, getAncestorsOf('glpi_entities', $dropdown->getEntityID()))) { continue; } else { $tmp = array(); $where = ""; if ($dropdown instanceof CommonTreeDropdown) { $tmp['completename'] = $dropdown->fields['completename']; $where = "`completename`='" . addslashes_deep($tmp['completename']) . "'"; } else { $tmp['name'] = $dropdown->fields['name']; $where = "`name`='" . addslashes_deep($tmp['name']) . "'"; } $tmp['entities_id'] = $new_entity; $where .= " AND `entities_id`='" . $tmp['entities_id'] . "'"; //There's a dropdown value in the target entity if ($found = $this->find($where)) { $myfound = array_pop($found); if ($myfound['id'] != $this->fields[$field]) { $toupdate[$field] = $myfound['id']; } } else { $clone = $dropdown->fields; if ($dropdown instanceof CommonTreeDropdown) { unset($clone['completename']); } unset($clone['id']); $clone['entities_id'] = $new_entity; $new_id = $dropdown->import($clone); $toupdate[$field] = $new_id; } } } } $this->update($toupdate); } return true; }
/** * Get all dropdown fields associated with an itemtype * @param itemtype the itemtype * @return an array or fields that represents the dropdown tables */ static function getDropdownForItemtype($itemtype) { global $DB; $associated_tables = array(); if (class_exists($itemtype)) { $source_table = getTableForItemType($itemtype); foreach (PluginGenericobjectSingletonObjectField::getInstance($itemtype) as $field => $value) { $table = getTableNameForForeignKeyField($field); $options = PluginGenericobjectField::getFieldOptions($field, $itemtype); if (isset($options['input_type']) and $options['input_type'] === 'dropdown' and preg_match('/^glpi_plugin_genericobject/', $table)) { $associated_tables[] = $table; } } } return $associated_tables; }
/** * Change field order in DB * @params an array which contains the itemtype, the field to move and the action (up/down) * @return nothing */ static function changeFieldOrder($params = array()) { global $DB; $itemtype = $params['itemtype']; $field = $params['field']; $table = getTableForItemType($itemtype); $fields = PluginGenericobjectSingletonObjectField::getInstance($params['itemtype']); //If action is down, reverse array first if ($params['action'] == 'down') { $fields = array_reverse($fields); } //Get array keys $keys = array_keys($fields); //Index represents current position of $field $index = 0; foreach ($keys as $id => $key) { if ($key == $field) { $index = $id; } } //Get 2 positions before and move field if ($params['action'] == 'down') { $previous = $index - 1; } else { $previous = $index - 2; } if (isset($keys[$previous])) { $parent = $fields[$keys[$previous]]; $query = "ALTER TABLE `{$table}` MODIFY `{$field}` " . $fields[$field]['Type']; $query .= " AFTER `" . $fields[$keys[$previous]]['Field'] . "`"; $DB->query($query) or die($DB->error()); } }