Example #1
0
function plugin_fields_checkFiles()
{
    $plugin = new Plugin();
    if (isset($_SESSION['glpiactiveentities']) && $plugin->isInstalled('fields') && $plugin->isActivated('fields')) {
        Plugin::registerClass('PluginFieldsContainer');
        Plugin::registerClass('PluginFieldsDropdown');
        Plugin::registerClass('PluginFieldsField');
        if (TableExists("glpi_plugin_fields_containers")) {
            $container_obj = new PluginFieldsContainer();
            $containers = $container_obj->find();
            foreach ($containers as $container) {
                $classname = "PluginFields" . ucfirst($container['itemtype'] . preg_replace('/s$/', '', $container['name']));
                if (!class_exists($classname)) {
                    PluginFieldsContainer::generateTemplate($container);
                }
            }
        }
        if (TableExists("glpi_plugin_fields_fields")) {
            $fields_obj = new PluginFieldsField();
            $fields = $fields_obj->find("`type` = 'dropdown'");
            foreach ($fields as $field) {
                PluginFieldsDropdown::create($field);
            }
        }
    }
}
Example #2
0
 static function prepareHtmlFields($fields, $items_id, $canedit = true, $show_table = true, $massiveaction = false)
 {
     if (empty($fields)) {
         return false;
     }
     //get object associated with this fields
     $tmp = $fields;
     $first_field = array_shift($tmp);
     $container_obj = new PluginFieldsContainer();
     $container_obj->getFromDB($first_field['plugin_fields_containers_id']);
     $items_itemtype = ucfirst($container_obj->fields['itemtype']);
     $classname = "PluginFields" . $items_itemtype . preg_replace('/s$/', '', $container_obj->fields['name']);
     $obj = new $classname();
     //find row for this object with the items_id
     $found_values = $obj->find("plugin_fields_containers_id = " . $first_field['plugin_fields_containers_id'] . " AND items_id = " . $items_id);
     $found_v = array_shift($found_values);
     // find profiles (to check if current profile can edit fields)
     $fprofile = new PluginFieldsProfile();
     $found_p = $fprofile->find("`profiles_id` = '" . $_SESSION['glpiactiveprofile']['id'] . "'\n                                  AND `plugin_fields_containers_id` = '" . $first_field['plugin_fields_containers_id'] . "'");
     $first_found_p = array_shift($found_p);
     // test status for "CommonITILObject" objects
     if (is_subclass_of($items_itemtype, "CommonITILObject")) {
         $items_obj = new $items_itemtype();
         $items_obj->getFromDB($items_id);
         if (in_array($items_obj->fields['status'], $items_obj->getClosedStatusArray()) || in_array($items_obj->fields['status'], $items_obj->getSolvedStatusArray()) || $first_found_p['right'] != CREATE) {
             $canedit = false;
         }
     }
     //show all fields
     $html = "";
     $odd = 0;
     foreach ($fields as $field) {
         if ($field['type'] === 'header') {
             $html .= "<tr class='tab_bg_2'>";
             $html .= "<th colspan='4'>" . $field['label'] . "</td>";
             $html .= "</tr>";
             $odd = 0;
         } else {
             //get value
             $value = "";
             if (is_array($found_v)) {
                 if ($field['type'] == "dropdown") {
                     $value = $found_v["plugin_fields_" . $field['name'] . "dropdowns_id"];
                 } else {
                     $value = $found_v[$field['name']];
                 }
             }
             if (isset($_SESSION['plugin']['fields']['values_sent'])) {
                 if ($field['type'] == "dropdown") {
                     $value = $_SESSION['plugin']['fields']['values_sent']["plugin_fields_" . $field['name'] . "dropdowns_id"];
                 } else {
                     $value = $_SESSION['plugin']['fields']['values_sent'][$field['name']];
                 }
             }
             //get default value
             if (empty($value) && !empty($field['default_value'])) {
                 $value = $field['default_value'];
             }
             //show field
             if ($show_table) {
                 if ($odd % 2 == 0) {
                     $html .= "<tr class='tab_bg_2'>";
                 }
                 $required = $field['mandatory'] == 1 ? "<span class='red'>*</span>" : '';
                 if ($container_obj->fields['itemtype'] == 'Ticket' && $container_obj->fields['type'] == 'dom' && strpos($_SERVER['HTTP_REFERER'], ".injector.php") === false && strpos($_SERVER['HTTP_REFERER'], ".public.php") === false) {
                     $html .= "<th width='13%'>" . $field['label'] . " : {$required}</th>";
                 } else {
                     $html .= "<td>" . $field['label'] . " : {$required}</td>";
                 }
                 $html .= "<td>";
             }
             $readonly = $field['is_readonly'];
             switch ($field['type']) {
                 case 'number':
                 case 'text':
                     $value = Html::cleanInputText($value);
                     if ($canedit && !$readonly) {
                         $html .= "<input type='text' name='" . $field['name'] . "' value=\"{$value}\" />";
                     } else {
                         $html .= $value;
                     }
                     break;
                 case 'textarea':
                     if ($massiveaction) {
                         continue;
                     }
                     if ($canedit && !$readonly) {
                         $html .= "<textarea cols='45' rows='4' name='" . $field['name'] . "'>" . "{$value}</textarea>";
                     } else {
                         $html .= nl2br($value);
                     }
                     break;
                 case 'dropdown':
                     if ($canedit && !$readonly) {
                         //find entity on current object
                         $obj = new $container_obj->fields['itemtype']();
                         $obj->getFromDB($items_id);
                         ob_start();
                         if (strpos($field['name'], "dropdowns_id") !== false) {
                             $dropdown_itemtype = getItemTypeForTable(getTableNameForForeignKeyField($field['name']));
                         } else {
                             $dropdown_itemtype = PluginFieldsDropdown::getClassname($field['name']);
                         }
                         Dropdown::show($dropdown_itemtype, array('value' => $value, 'entity' => $obj->getEntityID()));
                         $html .= ob_get_contents();
                         ob_end_clean();
                     } else {
                         $dropdown_table = "glpi_plugin_fields_" . $field['name'] . "dropdowns";
                         $html .= Dropdown::getDropdownName($dropdown_table, $value);
                     }
                     break;
                 case 'yesno':
                     //in massive action, we must skip display for yesno (possible bug in framework)
                     //otherwise double display of field
                     if ($massiveaction) {
                         continue;
                     }
                     if ($canedit && !$readonly) {
                         ob_start();
                         Dropdown::showYesNo($field['name'], $value);
                         $html .= ob_get_contents();
                         ob_end_clean();
                     } else {
                         $html .= Dropdown::getYesNo($value);
                     }
                     break;
                 case 'date':
                     if ($massiveaction) {
                         continue;
                     }
                     if ($canedit && !$readonly) {
                         ob_start();
                         Html::showDateFormItem($field['name'], $value);
                         $html .= ob_get_contents();
                         ob_end_clean();
                     } else {
                         $html .= Html::convDate($value);
                     }
                     break;
                 case 'datetime':
                     if ($massiveaction) {
                         continue;
                     }
                     if ($canedit && !$readonly) {
                         ob_start();
                         Html::showDateTimeFormItem($field['name'], $value);
                         $html .= ob_get_contents();
                         ob_end_clean();
                     } else {
                         $html .= Html::convDateTime($value);
                     }
                 case 'dropdownuser':
                     if ($massiveaction) {
                         continue;
                     }
                     if ($canedit && !$readonly) {
                         ob_start();
                         User::dropdown(array('name' => $field['name'], 'value' => $value, 'entity' => -1, 'right' => 'all', 'condition' => 'is_active=1 && is_deleted=0'));
                         $html .= ob_get_contents();
                         ob_end_clean();
                     } else {
                         $showuserlink = 0;
                         if (Session::haveRight('user', 'r')) {
                             $showuserlink = 1;
                         }
                         $html .= getUserName($value, $showuserlink);
                     }
             }
             if ($show_table) {
                 $html .= "</td>";
                 if ($odd % 2 == 1) {
                     $html .= "</tr>";
                 }
                 $odd++;
             }
         }
     }
     if ($show_table && $odd % 2 == 1) {
         $html .= "</tr>";
     }
     unset($_SESSION['plugin']['fields']['values_sent']);
     return $html;
 }