Skip to content
38 changes: 29 additions & 9 deletions inc/container.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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
Expand All @@ -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'];

Expand All @@ -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";
Expand Down
42 changes: 42 additions & 0 deletions inc/dropdownbase.class.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

class PluginFieldsDropdownBase extends CommonTreeDropdown {

//function mayBeVisible() {

// if (!isset($this->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 ;
}

}
114 changes: 68 additions & 46 deletions inc/field.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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'];
}
Expand Down Expand Up @@ -247,6 +251,7 @@ function showSummary($container) {
echo "<th>" . $LANG['mailing'][139] . "</th>";
echo "<th>" . $LANG['common'][17] . "</th>";
echo "<th>" . $LANG['common'][44] . "</th>";
echo "<th>" . $LANG['fields']['field']['label']['readonly'] . "</th>";
echo "</tr>\n";

$fields_type = self::getTypes();
Expand All @@ -269,7 +274,10 @@ function showSummary($container) {
echo "</script>\n";
echo $this->fields['label']."</td>";
echo "<td>".$fields_type[$this->fields['type']]."</td>";
echo "<td>".$this->fields['default_value']."</td>";
echo "<td>".$this->fields['default_value']."</td>";
echo "<td>";
Dropdown::showYesNo("is_readonly",$this->fields["is_readonly"],-1,array('readonly' => true));
echo "</td>";
echo "</tr>\n";
}
}
Expand All @@ -294,6 +302,7 @@ function showForm($ID, $options=array()) {
$this->check(-1,'w',$input);
}

$options['colspan'] = 3 ;
$this->showFormHeader($options);

echo "<tr>";
Expand All @@ -318,8 +327,12 @@ function showForm($ID, $options=array()) {
Html::autocompletionTextField($this, 'default_value',
array('value' => $this->fields["default_value"]));
echo "</td>";
echo "<td>".$LANG['fields']['field']['label']['readonly'].":</td>";
echo "<td>";
Dropdown::showYesNo("is_readonly",$this->fields["is_readonly"]);
echo "</td>";
echo "</tr>";

$this->showFormButtons($options);

}
Expand Down Expand Up @@ -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);
Expand All @@ -403,8 +408,7 @@ static function showForDomContainer() {

echo "Ext.onReady(function() {\n
Ext.select('#page form tr:last').each(function(el){
el.insertHtml('beforeBegin',
'<tr><td style=\"padding:0\" colspan=\"4\" id=\"dom_container\"></td></tr>');
el.insertHtml('beforeBegin', '<tr><td colspan=\"4\" id=\"dom_container\"></td></tr>');
Ext.get('dom_container').load({
url: '../plugins/fields/ajax/load_dom_fields.php',
params: {
Expand All @@ -413,7 +417,11 @@ static function showForDomContainer() {
}
});
});
});\n";


";

echo "});\n";
}

static function AjaxForDomContainer($itemtype, $items_id) {
Expand Down Expand Up @@ -486,45 +494,39 @@ static function prepareHtmlFields($fields, $items_id, $canedit = true,
//show field
if ($show_table) {
if ($odd%2 == 0) $html.= "<tr class='tab_bg_2'>";
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']." : </th>";
} else {
$html.= "<td>".$field['label']." : </td>";
}
$html.= "<td>".$field['label']." : </td>";
$html.= "<td>";
}
$readonly = $field['is_readonly'];
switch ($field['type']) {
case 'number':
case 'text':
$value = Html::cleanInputText($value);
if ($canedit) {
if ($canedit && !$readonly) {
$html.= "<input type='text' name='".$field['name']."' value=\"$value\" />";
} else {
$html.= $value;
}
break;
case 'textarea':
if ($massiveaction) continue;
if ($canedit) {
if ($canedit && !$readonly) {
$html.= "<textarea cols='45' rows='4' name='".$field['name']."'>".
"$value</textarea>";
} else {
$html.= str_replace('\n', '<br />', $value);
}
break;
case 'dropdown':
if ($canedit) {
if ($canedit && !$readonly) {
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));
Dropdown::show($dropdown_itemtype, array('value' => $value, 'condition' => 'is_visible=1' ));
$html.= ob_get_contents();
ob_end_clean();
} else {
Expand All @@ -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();
Expand All @@ -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();
Expand All @@ -559,14 +561,32 @@ 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();
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>";
Expand All @@ -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
Expand All @@ -609,12 +629,13 @@ static function showSingle($itemtype, $searchOption, $massiveaction = false) {
//display an hidden post field to store container id
echo "<input type='hidden' name='c_id' value='$c_id' />";

//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
Expand All @@ -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']
);
}

Expand Down
3 changes: 2 additions & 1 deletion inc/migration.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down
Loading