Skip to content

Commit

Permalink
fixes and enhancements about dropdown fields
Browse files Browse the repository at this point in the history
- change 'global' option to 'isolated' since it was misleading.
- enhance the way fields names/tables/classes are detected when they are
  assigned to object.
  • Loading branch information
kiniou committed Aug 28, 2014
1 parent f12d1e4 commit afb2072
Show file tree
Hide file tree
Showing 5 changed files with 113 additions and 50 deletions.
10 changes: 6 additions & 4 deletions fields/field.constant.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,21 +69,23 @@
$GO_FIELDS['types_id']['name'] = __("Type");
$GO_FIELDS['types_id']['linkfield'] = 'type';
$GO_FIELDS['types_id']['input_type'] = 'dropdown';
$GO_FIELDS['types_id']['dropdown_type'] = 'global'; //Means that
// The 'isolated' dropdown type will create a isolated table for each type that will be assigned
// with this field.
$GO_FIELDS['types_id']['dropdown_type'] = 'isolated';

$GO_FIELDS['models_id']['name'] = __("Model");
$GO_FIELDS['models_id']['input_type'] = 'dropdown';
$GO_FIELDS['models_id']['dropdown_type'] = 'global';
$GO_FIELDS['models_id']['dropdown_type'] = 'isolated';

$GO_FIELDS['categories_id']['name'] = __("Category");
$GO_FIELDS['categories_id']['input_type'] = 'dropdown';
$GO_FIELDS['categories_id']['dropdown_type'] = 'global';
$GO_FIELDS['categories_id']['dropdown_type'] = 'isolated';

$GO_FIELDS['entities_id']['name'] = __("Entity");
$GO_FIELDS['entities_id']['input_type'] = 'dropdown';
$GO_FIELDS['entities_id']['massiveaction'] = false;

$GO_FIELDS['template_name']['name'] = __("Template name");
$GO_FIELDS['template_name']['name'] = __("Template name");
$GO_FIELDS['template_name']['input_type'] = 'text';
$GO_FIELDS['template_name']['massiveaction'] = false;

Expand Down
5 changes: 5 additions & 0 deletions front/field.form.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@
$itemtype = $type->fields['itemtype'];
PluginGenericobjectType::registerOneType($itemtype);

Toolbox::logDebug(array(
'itemtype' => $itemtype,
'getTableForItemType'=>getTableForItemType($itemtype),
'new_field' => $_POST['new_field']
));
PluginGenericobjectField::addNewField(getTableForItemType($itemtype), $_POST["new_field"]);
Session::addMessageAfterRedirect(__("Field added successfully", "genericobject"));
}
Expand Down
126 changes: 87 additions & 39 deletions inc/field.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,10 @@ public static function showObjectFieldsForm($id) {
//Reset fields definition only to keep the itemtype ones
$GO_FIELDS = array();
plugin_genericobject_includeCommonFields(true);
$file = GENERICOBJECT_FIELDS_PATH.
"/fields/constants/".
$object_type->fields['name'].".constant.php";

if (file_exists($file)) {
include $file;
}
PluginGenericobjectType::includeConstants($itemtype, true);
//ToolBox::logDebug(PluginGenericobjectSingletonObjectField::$_dbfields);

PluginGenericobjectType::includeConstants($object_type->fields['name'], true);

foreach ($GO_BLACKLIST_FIELDS as $autofield) {
if (!in_array($autofield, $used_fields)) {
Expand Down Expand Up @@ -86,16 +82,8 @@ public static function showObjectFieldsForm($id) {
if (!in_array($field, $GO_READONLY_FIELDS)) {
$index++;
}
//If it's a plugin dropdowns, get it's real name
//(it may not be the one from the DB, in case it's a global field)
$table = getTableNameForForeignKeyField($field);
/*if ($table != '' && isPluginItemType(getItemTypeForTable($table))) {
$classname = getItemTypeForTable($table);
$class = new $classname();
$used_fields[$class->getFieldName()] = $class->getFieldName();
} else {*/
$used_fields[$field] = $field;
//}
$used_fields[$field] = $field;
$global_index++;
}
echo "</table>";
Expand All @@ -118,19 +106,66 @@ public static function showObjectFieldsForm($id) {

/**
* Get the name of the field, as defined in a constant file
* The name may be the same, or not depending if it's a global dropdown or not
* The name may be the same, or not depending if it's an isolated dropdown or not
*/
static function getFieldGlobalName($field, $itemtype, $options, $remove_prefix = false) {
if (isset($options['dropdown_type'])
&& $options['dropdown_type'] == 'global') {

$fk = getForeignKeyFieldForTable(getTableForItemType($itemtype));
if (!$remove_prefix) {
$field = preg_replace("/s_id$/",$field, $fk);
} else {
$fk = preg_replace("/s_id$/","", $fk);
$field = preg_replace("/".$fk."/","", $field);
}
static function getFieldName($field, $itemtype, $options, $remove_prefix = false) {
$field_orig = $field;
$field_table = null;
$input_type = isset($options['input_type'])
? $options['input_type']
: null;
switch($input_type) {

case 'dropdown':
$dropdown_type = isset($options['dropdown_type'])
? $options['dropdown_type']
: null;
$fk = getForeignKeyFieldForTable(getTableForItemType($itemtype));

if ( $dropdown_type == 'isolated' ) {
if (!$remove_prefix) {
$field = preg_replace("/s_id$/",$field, $fk);
} else {
$fk = preg_replace("/s_id$/","", $fk);
$field = preg_replace("/".$fk."/","", $field);
}
}
Toolbox::logDebug(array(
"field_orig" => $field_orig,
"field" => $field,
"fk" => $fk,
"options" => $options
));
$field_table = getTableNameForForeignKeyField($field);
Toolbox::logDebug(
$field, "\n",
substr($field_table, strlen('glpi_')), "\n",
strlen($field)-strlen('_id'),
'"'.substr($field, 0, strlen($field)-strlen('_id')).'"'
);
//Prepend plugin's table prefix if this dropdown is not already handled by GLPI natively
if (
substr($field, 0, strlen('plugin_genericobject')) !== 'plugin_genericobject'
and (
substr($field_table, strlen('glpi_'))
=== substr($field, 0, strlen($field) -strlen('_id'))
)
and !TableExists($field_table)
) {
if (!$remove_prefix) { $field = 'plugin_genericobject_' . $field;}
Toolbox::logDebug($field);
} else {
Toolbox::logDebug($field, "is already handled by GLPI");
}


Toolbox::logDebug("Get Table for dropdown", $field,":", $field_table);

break;

default:
Toolbox::logDebug($field, "Type:", $input_type);

}
return $field;
}
Expand All @@ -151,7 +186,7 @@ static function dropdownFields($name,$itemtype, $used = array()) {
$dropdown_types = array();
foreach ($GO_FIELDS as $field => $values) {
$message = "";
$field = self::getFieldGlobalName($field, $itemtype, $values, false);
$field = self::getFieldName($field, $itemtype, $values, false);
if(!in_array($field, $used)) {
if (!isset($dropdown_types[$field])) {
//Global management :
Expand Down Expand Up @@ -187,23 +222,34 @@ static function dropdownFields($name,$itemtype, $used = array()) {

/**
*
* Get field definition for a field
* Get field's options defined in constant files.
* If this field has not been defined, it means that this field has been defined globally and
* must be dynamically created.
*
* @param $field the current field
* @param $itemtype the itemtype
* @return an array which contains the full field definition
*/
static function getOptionsWithGlobal($field, $itemtype) {
static function getFieldOptions($field, $itemtype) {
global $GO_FIELDS;

if (!isset($GO_FIELDS[$field])) {
$tmpfield = self::getFieldGlobalName($field, $itemtype,
array('dropdown_type' => 'global'), true);
$cleaned_field = preg_replace("/^plugin_genericobject_/",'', $field);
if (!isset($GO_FIELDS[$cleaned_field])) {
// This field has been dynamically defined because it's an isolated dropdown
Toolbox::logDebug("'$cleaned_field' not found in GO_FIELDS !!!!");
$tmpfield = self::getFieldName(
$field, $itemtype,
array(
'dropdown_type' => 'isolated',
'input_type' => 'dropdown'
),
true
);
$options = $GO_FIELDS[$tmpfield];
$options['realname'] = $tmpfield;
} else {
$options = $GO_FIELDS[$field];
$options['realname'] = $field;
$options = $GO_FIELDS[$cleaned_field];
$options['realname'] = $cleaned_field;
}
return $options;
}
Expand All @@ -213,7 +259,7 @@ public static function displayFieldDefinition($target, $itemtype, $field, $index

$readonly = in_array($field, $GO_READONLY_FIELDS);
$blacklist = in_array($field, $GO_BLACKLIST_FIELDS);
$options = self::getOptionsWithGlobal($field, $itemtype);
$options = self::getFieldOptions($field, $itemtype);

echo "<tr class='tab_bg_".(($index%2)+1)."' align='center'>";
$sel ="";
Expand All @@ -223,7 +269,7 @@ public static function displayFieldDefinition($target, $itemtype, $field, $index
echo "<input type='checkbox' name='fields[" .$field. "]' value='1' $sel>";
}
echo "</td>";
echo "<td>" . $options['name'] . "</td>";
echo "<td>" . __($options['name'], 'genericobject') . "</td>";
echo "<td>" . $field . "</td>";

echo "<td width='10'>";
Expand Down Expand Up @@ -255,8 +301,10 @@ public static function addNewField($table, $field, $after=false) {
global $DB;

$itemtype = getItemTypeForTable($table);
Toolbox::logDebug("Will add field '".$field."' to table '".$table."'");
if (!FieldExists($table, $field, false)) {
$options = self::getOptionsWithGlobal($field, $itemtype);
$options = self::getFieldOptions($field, $itemtype);
Toolbox::logDebug($options);
$query = "ALTER TABLE `$table` ADD `$field` ";
switch ($options['input_type']) {
case 'dropdown_yesno' :
Expand Down
14 changes: 10 additions & 4 deletions inc/object.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -463,7 +463,7 @@ static function getFieldsToHide() {
function displayField($canedit, $name, $value, $template, $description = array()) {
global $GO_BLACKLIST_FIELDS;

$searchoption = PluginGenericobjectField::getOptionsWithGlobal($name, get_called_class());
$searchoption = PluginGenericobjectField::getFieldOptions($name, get_called_class());

if (!empty($searchoption)
&& !in_array($name, self::getFieldsToHide())) {
Expand Down Expand Up @@ -726,9 +726,15 @@ function getObjectSearchOptions($with_linkfield = false) {
$index = 3;
$options = array();
$table = getTableForItemType(get_called_class());
foreach (PluginGenericobjectSingletonObjectField::getInstance(get_called_class()) as $field => $values) {
$searchoption = PluginGenericobjectField::getOptionsWithGlobal($field,
$this->objecttype->fields['itemtype']);

foreach (
PluginGenericobjectSingletonObjectField::getInstance(get_called_class())
as $field => $values
) {
$searchoption = PluginGenericobjectField::getFieldOptions(
$field,
$this->objecttype->fields['itemtype']
);

//Some fields have fixed index values...
$currentindex = $index;
Expand Down
8 changes: 5 additions & 3 deletions inc/type.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -952,7 +952,7 @@ public static function addDropdownFrontformFile($name) {


public static function addDropdownClassFile($name, $field, $options) {
$params['tree'] = false;
$params['is_tree'] = false;
$params['realname'] = false;
$params['linked_itemtype'] = false;
foreach ($options as $key => $value) {
Expand All @@ -962,7 +962,7 @@ public static function addDropdownClassFile($name, $field, $options) {
array(
'CLASSNAME' => self::getClassByName($name),
'EXTENDS' =>
'PluginGenericobject' . ($params['tree']?'CommonTree':'Common') . 'Dropdown',
'PluginGenericobject' . ($params['is_tree']?'CommonTree':'Common') . 'Dropdown',
'FIELDNAME' => $params['realname'],
'LINKED_ITEMTYPE' => $params['linked_itemtype']
),
Expand Down Expand Up @@ -1403,7 +1403,8 @@ static function includeLocales($name) {


static function includeConstants($name, $force = false) {
$file = GENERICOBJECT_FIELDS_PATH . "/fields/constants/$name.constant.php";
//Toolbox::logDebug("includeConstants",$name);
$file = GENERICOBJECT_FIELDS_PATH . "/$name.constant.php";
if (file_exists($file)) {
if (!$force) {
include_once($file);
Expand All @@ -1426,6 +1427,7 @@ static function getDropdownForItemtype($itemtype) {
$source_table = getTableForItemType($itemtype);
foreach (PluginGenericobjectSingletonObjectField::getInstance($itemtype) as $field => $value) {
$table = getTableNameForForeignKeyField($field);
//Toolbox::logDebug($field, $value, $table, $source_table, getSingular($source_table));
//If it's a drodpdown
if ($table && preg_match("/".getSingular($source_table)."/",$table)) {
$associated_tables[] = $table;
Expand Down

0 comments on commit afb2072

Please sign in to comment.