diff --git a/inc/container.class.php b/inc/container.class.php index 4f2777ff..814fb297 100644 --- a/inc/container.class.php +++ b/inc/container.class.php @@ -329,10 +329,14 @@ static function getEntries($type = 'tab', $full = false) { } //profiles restriction - $found = $profile->find("`profiles_id` = '".$_SESSION['glpiactiveprofile']['id']."' - AND `plugin_fields_containers_id` = '".$item['id']."'"); - $first_found = array_shift($found); - if ($first_found['right'] == NULL) continue; + if( isset($_SESSION['glpiactiveprofile']['id'])) { + $found = $profile->find("`profiles_id` = '".$_SESSION['glpiactiveprofile']['id']."' + AND `plugin_fields_containers_id` = '".$item['id']."'"); + $first_found = array_shift($found); + if ($first_found['right'] == NULL) continue; + } else + continue ; + //show more info or not if ($full) { @@ -606,7 +610,7 @@ static function getAddSearchOptions($itemtype, $containers_id = false) { } $i = 76665; - $query = "SELECT fields.name, fields.label, fields.type, + $query = "SELECT fields.name, fields.label, fields.type, fields.is_readonly, containers.name as container_name, containers.label as container_label, containers.itemtype FROM glpi_plugin_fields_containers containers @@ -626,7 +630,10 @@ static function getAddSearchOptions($itemtype, $containers_id = false) { $opt[$i]['name'] = $datas['container_label']." - ".$datas['label']; $opt[$i]['linkfield'] = $datas['name']; //$opt[$i]['condition'] = "glpi_plugin_fields_fields.name = '".$datas['name']."'"; - //$opt[$i]['massiveaction'] = false; + + if( $datas['is_readonly'] ) + $opt[$i]['massiveaction'] = false; + $opt[$i]['joinparams']['jointype'] = "itemtype_item"; $opt[$i]['pfields_type'] = $datas['type']; @@ -635,14 +642,27 @@ static function getAddSearchOptions($itemtype, $containers_id = false) { $opt[$i]['field'] = 'name'; $opt[$i]['linkfield'] = "plugin_fields_".$datas['name']."dropdowns_id"; $opt[$i]['searchtype'] = 'equals'; + $opt[$i]['forcegroupby'] = true ; // to fix a bug in mySQL: see http://bugs.mysql.com/bug.php?id=69268 and http://bugs.mysql.com/bug.php?id=68897 fixed in mySQL 5.6.13 + //$opt[$i]['condition'] = "is_visible=1" ; $opt[$i]['joinparams']['jointype'] = ""; $opt[$i]['joinparams']['beforejoin']['table'] = $tablename; $opt[$i]['joinparams']['beforejoin']['joinparams']['jointype'] = "itemtype_item"; - } + } elseif ($datas['type'] === "dropdownuser") { + $opt[$i]['table'] = 'glpi_users'; + $opt[$i]['field'] = 'name'; + $opt[$i]['linkfield'] = $datas['name']; + //$opt[$i]['searchtype'] = 'equals'; + $opt[$i]['forcegroupby'] = true ; // to fix a bug in mySQL: see http://bugs.mysql.com/bug.php?id=69268 and http://bugs.mysql.com/bug.php?id=68897 fixed in mySQL 5.6.13 + //$opt[$i]['condition'] = "is_visible=1" ; + $opt[$i]['joinparams']['jointype'] = ""; + $opt[$i]['joinparams']['beforejoin']['table'] = $tablename; + $opt[$i]['joinparams']['beforejoin']['joinparams']['jointype'] = "itemtype_item"; + } switch ($datas['type']) { - case 'dropdown': - $opt[$i]['datatype'] = "dropdown"; + case 'dropdown': + case 'dropdownuser': + $opt[$i]['datatype'] = "dropdown"; break; case 'yesno': $opt[$i]['datatype'] = "bool"; diff --git a/inc/dropdownbase.class.php b/inc/dropdownbase.class.php new file mode 100644 index 00000000..4a826be4 --- /dev/null +++ b/inc/dropdownbase.class.php @@ -0,0 +1,42 @@ +fields['id'])) { + // $this->getEmpty(); + // } + // return array_key_exists('is_visible', $this->fields); + //} + + function getAdditionalFields() { + global $LANG; + + $val = parent::getAdditionalFields() ; + //if( $this->mayBeVisible( ) ) { + $val[] = array('name' => 'is_visible', + 'label' => $LANG['group'][0], + 'type' => 'bool', + 'list' => false) ; + //} + + return $val; + } + + function getSearchOptions() { + global $LANG; + + $val = parent::getSearchOptions( ) ; + + //if( $this->mayBeVisible( ) ) { + $val[] = array( 'table' => $this->getTable(), + 'field' => 'is_visible', + 'name' => $LANG['group'][0], + 'datatype' => 'bool') ; + //} + + return $val ; + } + +} \ No newline at end of file diff --git a/inc/field.class.php b/inc/field.class.php index 3af138eb..92a53eeb 100644 --- a/inc/field.class.php +++ b/inc/field.class.php @@ -23,6 +23,10 @@ static function install(Migration $migration) { KEY `plugin_fields_containers_id` (`plugin_fields_containers_id`) ) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;"; $DB->query($query) or die ($DB->error()); + } else { //table exists, but we may add fields + + $migration->addField( $table, 'is_readonly', 'bool', array('default' => false)) ; + $migration->executeMigration(); } return true; @@ -148,7 +152,7 @@ function prepareName($input) { } //for dropdown, if already exist, link to it - if ($input['type'] === "dropdown") { + if (isset( $input['type'] ) && $input['type'] === "dropdown") { $found = $this->find("name = '".$input['name']."'"); if (!empty($found)) return $input['name']; } @@ -247,6 +251,7 @@ function showSummary($container) { echo "" . $LANG['mailing'][139] . ""; echo "" . $LANG['common'][17] . ""; echo "" . $LANG['common'][44] . ""; + echo "" . $LANG['fields']['field']['label']['readonly'] . ""; echo "\n"; $fields_type = self::getTypes(); @@ -269,7 +274,10 @@ function showSummary($container) { echo "\n"; echo $this->fields['label'].""; echo "".$fields_type[$this->fields['type']].""; - echo "".$this->fields['default_value'].""; + echo "".$this->fields['default_value'].""; + echo ""; + Dropdown::showYesNo("is_readonly",$this->fields["is_readonly"],-1,array('readonly' => true)); + echo ""; echo "\n"; } } @@ -294,6 +302,7 @@ function showForm($ID, $options=array()) { $this->check(-1,'w',$input); } + $options['colspan'] = 3 ; $this->showFormHeader($options); echo ""; @@ -318,8 +327,12 @@ function showForm($ID, $options=array()) { Html::autocompletionTextField($this, 'default_value', array('value' => $this->fields["default_value"])); echo ""; + echo "".$LANG['fields']['field']['label']['readonly'].":"; + echo ""; + Dropdown::showYesNo("is_readonly",$this->fields["is_readonly"]); + echo ""; echo ""; - + $this->showFormButtons($options); } @@ -364,35 +377,27 @@ static function showForTabContainer($c_id, $items_id) { static function showForDomContainer() { //parse http_referer to get current url (this code is loaded by javacript) - $current_url = $_SERVER['HTTP_REFERER']; - if (strpos($current_url, ".form.php") === false - && strpos($current_url, ".injector.php") === false - && strpos($current_url, ".public.php") === false) { - return false; - } + if( isset( $_SERVER['HTTP_REFERER'] ) ) + $current_url = $_SERVER['HTTP_REFERER']; + else return false ; + + if (strpos($current_url, ".form.php") === false) return false; $expl_url = explode("?", $current_url); + //if add item form, do nothing + if (!isset($expl_url[1]) || strpos($expl_url[1], "id=") === false) return false; + //get current id - if(isset($expl_url[1])) { - parse_str($expl_url[1], $params); - if(isset($params['id'])) { - $items_id = $params['id']; - } else { - $items_id = 0; - } - } else { - $items_id = 0; + parse_str($expl_url[1], $params); + if( !isset($params['id'])) { + return false ; } + $items_id = $params['id']; //get itemtype $tmp = explode("/", $expl_url[0]); $script_name = array_pop($tmp); - - if(in_array($script_name, array("helpdesk.public.php","tracking.injector.php"))) { - $current_itemtype = "Ticket"; - } else { - $current_itemtype = ucfirst(str_replace(".form.php", "", $script_name)); - } + $current_itemtype = ucfirst(str_replace(".form.php", "", $script_name)); //Retrieve dom container $itemtypes = PluginFieldsContainer::getEntries('dom', true); @@ -403,8 +408,7 @@ static function showForDomContainer() { echo "Ext.onReady(function() {\n Ext.select('#page form tr:last').each(function(el){ - el.insertHtml('beforeBegin', - ''); + el.insertHtml('beforeBegin', ''); Ext.get('dom_container').load({ url: '../plugins/fields/ajax/load_dom_fields.php', params: { @@ -413,7 +417,11 @@ static function showForDomContainer() { } }); }); - });\n"; + + + "; + + echo "});\n"; } static function AjaxForDomContainer($itemtype, $items_id) { @@ -486,21 +494,15 @@ static function prepareHtmlFields($fields, $items_id, $canedit = true, //show field if ($show_table) { if ($odd%2 == 0) $html.= ""; - 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.= "".$field['label']." : "; - } else { - $html.= "".$field['label']." : "; - } + $html.= "".$field['label']." : "; $html.= ""; } + $readonly = $field['is_readonly']; switch ($field['type']) { case 'number': case 'text': $value = Html::cleanInputText($value); - if ($canedit) { + if ($canedit && !$readonly) { $html.= ""; } else { $html.= $value; @@ -508,7 +510,7 @@ static function prepareHtmlFields($fields, $items_id, $canedit = true, break; case 'textarea': if ($massiveaction) continue; - if ($canedit) { + if ($canedit && !$readonly) { $html.= ""; } else { @@ -516,7 +518,7 @@ static function prepareHtmlFields($fields, $items_id, $canedit = true, } break; case 'dropdown': - if ($canedit) { + if ($canedit && !$readonly) { ob_start(); if (strpos($field['name'], "dropdowns_id") !== false) { $dropdown_itemtype = getItemTypeForTable( @@ -524,7 +526,7 @@ static function prepareHtmlFields($fields, $items_id, $canedit = true, } else { $dropdown_itemtype = PluginFieldsDropdown::getClassname($field['name']); } - Dropdown::show($dropdown_itemtype, array('value' => $value)); + Dropdown::show($dropdown_itemtype, array('value' => $value, 'condition' => 'is_visible=1' )); $html.= ob_get_contents(); ob_end_clean(); } else { @@ -537,7 +539,7 @@ static function prepareHtmlFields($fields, $items_id, $canedit = true, //otherwise double display of field if ($massiveaction) continue; - if ($canedit) { + if ($canedit && !$readonly) { ob_start(); Dropdown::showYesNo($field['name'], $value); $html.= ob_get_contents(); @@ -548,7 +550,7 @@ static function prepareHtmlFields($fields, $items_id, $canedit = true, break; case 'date': if ($massiveaction) continue; - if ($canedit) { + if ($canedit && !$readonly) { ob_start(); Html::showDateFormItem($field['name'], $value); $html.= ob_get_contents(); @@ -559,7 +561,7 @@ static function prepareHtmlFields($fields, $items_id, $canedit = true, break; case 'datetime': if ($massiveaction) continue; - if ($canedit) { + if ($canedit && !$readonly) { ob_start(); Html::showDateTimeFormItem($field['name'], $value); $html.= ob_get_contents(); @@ -567,6 +569,24 @@ static function prepareHtmlFields($fields, $items_id, $canedit = true, } 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.= ""; @@ -593,7 +613,7 @@ static function showSingle($itemtype, $searchOption, $massiveaction = false) { $searchOption['linkfield']); //find field - $query_f = "SELECT fields.plugin_fields_containers_id + $query_f = "SELECT fields.plugin_fields_containers_id, fields.is_readonly FROM glpi_plugin_fields_fields fields LEFT JOIN glpi_plugin_fields_containers containers ON containers.id = fields.plugin_fields_containers_id @@ -609,12 +629,13 @@ static function showSingle($itemtype, $searchOption, $massiveaction = false) { //display an hidden post field to store container id echo ""; - //preapre arary for function prepareHtmlFields + //prepare array for function prepareHtmlFields $fields = array(array( 'id' => 0, 'type' => $searchOption['pfields_type'], 'plugin_fields_containers_id' => $c_id, - 'name' => $cleaned_linkfield + 'name' => $cleaned_linkfield, + 'is_readonly' => $row_f['is_readonly'] )); //show field @@ -633,7 +654,8 @@ static function getTypes() { 'dropdown' => $LANG['fields']['field']['type']['dropdown'], 'yesno' => $LANG['fields']['field']['type']['yesno'], 'date' => $LANG['fields']['field']['type']['date'], - 'datetime' => $LANG['fields']['field']['type']['datetime'] + 'datetime' => $LANG['fields']['field']['type']['datetime'], + 'dropdownuser' => $LANG['fields']['field']['type']['dropdownuser'] ); } diff --git a/inc/migration.class.php b/inc/migration.class.php index 35e551cc..252b9863 100644 --- a/inc/migration.class.php +++ b/inc/migration.class.php @@ -47,7 +47,8 @@ static function getSQLType($field_type) { 'dropdown' => 'INT(11) NOT NULL DEFAULT 0', 'yesno' => 'INT(11) NOT NULL DEFAULT 0', 'date' => 'VARCHAR(255) DEFAULT NULL', - 'datetime' => 'VARCHAR(255) DEFAULT NULL' + 'datetime' => 'VARCHAR(255) DEFAULT NULL', + 'dropdownuser' => 'INT(11) NOT NULL DEFAULT 0' ); return $types[$field_type]; diff --git a/locales/en_GB.php b/locales/en_GB.php index a68f0748..2d1e5402 100644 --- a/locales/en_GB.php +++ b/locales/en_GB.php @@ -31,6 +31,10 @@ $LANG['fields']['field']['type']['yesno'] = "Yes/No"; $LANG['fields']['field']['type']['date'] = "Date"; $LANG['fields']['field']['type']['datetime'] = "Date & time"; +$LANG['fields']['field']['type']['dropdownuser'] = "User Dropdown"; + $LANG['fields']['field']['label']['add'] = "Add a new field"; $LANG['fields']['field']['label']['no_fields'] = "No field for this bloc"; + +$LANG['fields']['field']['label']['readonly'] = "Read only"; diff --git a/locales/fr_FR.php b/locales/fr_FR.php index d938982c..99b04617 100644 --- a/locales/fr_FR.php +++ b/locales/fr_FR.php @@ -31,6 +31,10 @@ $LANG['fields']['field']['type']['yesno'] = "Oui/Non"; $LANG['fields']['field']['type']['date'] = "Date"; $LANG['fields']['field']['type']['datetime'] = "Date & heure"; +$LANG['fields']['field']['type']['dropdownuser'] = "Liste Utilisateurs"; + $LANG['fields']['field']['label']['add'] = "Ajouter un nouveau champ"; $LANG['fields']['field']['label']['no_fields'] = "Pas de champs pour ce bloc"; + +$LANG['fields']['field']['label']['readonly'] = "Lecture seule"; diff --git a/setup.php b/setup.php index c79ac4c8..d3ddf6cb 100644 --- a/setup.php +++ b/setup.php @@ -6,7 +6,8 @@ function plugin_init_fields() { $plugin = new Plugin(); if (isset($_SESSION['glpiactiveentities']) && $plugin->isInstalled('fields') - && $plugin->isActivated('fields')) { + && $plugin->isActivated('fields') + && Session::getLoginUserID() ) { Plugin::registerClass('PluginFieldsContainer', array('addtabon' => PluginFieldsContainer::getEntries())); @@ -42,8 +43,6 @@ function plugin_init_fields() { "preItemUpdate"); $PLUGIN_HOOKS['pre_item_purge'] ['fields'][$itemtype] = array("PluginFieldsContainer", "preItemPurge"); - $PLUGIN_HOOKS['item_add']['fields'][$itemtype] = array("PluginFieldsContainer", - "preItemUpdate"); } } } @@ -53,8 +52,8 @@ function plugin_init_fields() { function plugin_version_fields() { global $LANG; return array ('name' => $LANG["fields"]["title"][1], - 'version' => '2.0', - 'author' => 'Alexandre Delaunay & Walid Nouh', + 'version' => '2.1.0', + 'author' => 'Alexandre Delaunay & Walid Nouh & Olivier Moron', 'homepage' => 'teclib.com', 'license' => 'restricted', 'minGlpiVersion' => '0.83.3'); diff --git a/templates/dropdown.class.tpl b/templates/dropdown.class.tpl index 5530ac25..205ec243 100644 --- a/templates/dropdown.class.tpl +++ b/templates/dropdown.class.tpl @@ -1,6 +1,6 @@