PHRAS-1177_change-multi-on-field

- add : now the "multi" attribute on a field can be changed after creation
- change : field template
#time 3h
This commit is contained in:
Jean-Yves Gaulier
2016-06-23 20:22:25 +02:00
parent e6762531cc
commit deafb8b957
7 changed files with 103 additions and 77 deletions

View File

@@ -210,7 +210,7 @@ class FieldsController extends Controller
$this->validateTagField($data); $this->validateTagField($data);
try { try {
$field = \databox_field::create($this->app, $databox, $data['name'], $data['multi']); $field = \databox_field::create($this->app, $databox, $data['name']);
$this->updateFieldWithData($field, $data); $this->updateFieldWithData($field, $data);
$field->save(); $field->save();
} catch (\Exception $e) { } catch (\Exception $e) {
@@ -308,6 +308,7 @@ class FieldsController extends Controller
->set_business($data['business']) ->set_business($data['business'])
->set_aggregable($data['aggregable']) ->set_aggregable($data['aggregable'])
->set_indexable($data['indexable']) ->set_indexable($data['indexable'])
->set_multi($data['multi'])
->set_required($data['required']) ->set_required($data['required'])
->set_separator($data['separator']) ->set_separator($data['separator'])
->set_readonly($data['readonly']) ->set_readonly($data['readonly'])

View File

@@ -451,8 +451,9 @@ class databox extends base implements ThumbnailedElement
$multi = isset($field['multi']) ? (Boolean) (string) $field['multi'] : false; $multi = isset($field['multi']) ? (Boolean) (string) $field['multi'] : false;
$meta_struct_field = databox_field::create($this->app, $this, $fname, $multi); $meta_struct_field = databox_field::create($this->app, $this, $fname);
$meta_struct_field $meta_struct_field
->set_multi($multi)
->set_readonly(isset($field['readonly']) ? (string) $field['readonly'] : 0) ->set_readonly(isset($field['readonly']) ? (string) $field['readonly'] : 0)
->set_indexable(isset($field['index']) ? (string) $field['index'] : '1') ->set_indexable(isset($field['index']) ? (string) $field['index'] : '1')
->set_separator(isset($field['separator']) ? (string) $field['separator'] : '') ->set_separator(isset($field['separator']) ? (string) $field['separator'] : '')

View File

@@ -564,6 +564,18 @@ class databox_field implements cache_cacheableInterface
return $this; return $this;
} }
/**
*
* @param boolean $bool
* @return databox_field
*/
public function set_multi($bool)
{
$this->multi = (bool)$bool;
return $this;
}
/** /**
* Set a vocabulary * Set a vocabulary
* *
@@ -892,7 +904,7 @@ class databox_field implements cache_cacheableInterface
* *
* @throws \Exception_InvalidArgument * @throws \Exception_InvalidArgument
*/ */
public static function create(Application $app, databox $databox, $name, $multi) public static function create(Application $app, databox $databox, $name)
{ {
$sorter = 0; $sorter = 0;
@@ -911,7 +923,7 @@ class databox_field implements cache_cacheableInterface
`thumbtitle`, `multi`, `business`, `aggregable`, `thumbtitle`, `multi`, `business`, `aggregable`,
`report`, `sorter`, `separator`) `report`, `sorter`, `separator`)
VALUES (null, :name, '', 0, 0, 1, 'string', '', VALUES (null, :name, '', 0, 0, 1, 'string', '',
null, :multi, null, 0,
0, 0, 1, :sorter, '')"; 0, 0, 1, :sorter, '')";
$name = self::generateName($name); $name = self::generateName($name);
@@ -920,10 +932,8 @@ class databox_field implements cache_cacheableInterface
throw new \Exception_InvalidArgument(); throw new \Exception_InvalidArgument();
} }
$multi = $multi ? 1 : 0;
$stmt = $databox->get_connection()->prepare($sql); $stmt = $databox->get_connection()->prepare($sql);
$stmt->execute([':name' => $name, ':sorter' => $sorter, ':multi' => $multi]); $stmt->execute([':name' => $name, ':sorter' => $sorter]);
$id = $databox->get_connection()->lastInsertId(); $id = $databox->get_connection()->lastInsertId();
$stmt->closeCursor(); $stmt->closeCursor();

View File

@@ -119,45 +119,20 @@
<td colspan="2" class="dces-help-block info"></td> <td colspan="2" class="dces-help-block info"></td>
</tr> </tr>
<tr> <tr>
<td colspan="2"> <td><label for="type">{% trans %}Type{% endtrans %}</label></td>
<% if(field.multi == true) { %> <td>
<i class='icon-ok'></i> <select id="type">
<% } else { %> <option <%= field.type == '' ? 'selected' : '' %> value=""></option>
<i class='icon-remove'></i> <option <%= field.type == 'string' ? 'selected' : '' %> value="string">string</option>
<% } %> {% trans %}Multivalued{% endtrans %} <option <%= field.type == 'text' ? 'selected' : '' %> value="text">text</option>
<option <%= field.type == 'number' ? 'selected' : '' %> value="number">number</option>
<option <%= field.type == 'date' ? 'selected' : '' %> value="date">date</option>
</select>
</td> </td>
</tr> </tr>
</table> </table>
<div class="edit-form"> <div class="edit-form">
<h4>{% trans %}Advanced field parameters{% endtrans %}</h4>
<table> <table>
<tr>
<td><label for="tbranch">{% trans %}Thesaurus branch{% endtrans %}</label></td>
<td><input id="tbranch" type="text" value="<%= field.tbranch %>"/></td>
</tr>
<tr>
<td><label for="type">{% trans %}Type{% endtrans %}</label></td>
<td>
<select id="type">
<option <%= field.type == '' ? 'selected' : '' %> value=""></option>
<option <%= field.type == 'string' ? 'selected' : '' %> value="string">string</option>
<option <%= field.type == 'text' ? 'selected' : '' %> value="text">text</option>
<option <%= field.type == 'number' ? 'selected' : '' %> value="number">number</option>
<option <%= field.type == 'date' ? 'selected' : '' %> value="date">date</option>
</select>
</td>
</tr>
<tr>
<td><label for="vocabulary-type">{% trans %}Vocabulary type{% endtrans %}</label></td>
<td>
<select id="vocabulary-type">
<option <%= field['vocabulary-type'] == null ? 'selected' : '' %> value=''></option>
<% _.each(vocabularyTypes, function(vocab) { %>
<option <%= field['vocabulary-type'] == vocab.type ? 'selected' : '' %> value="<%= vocab.type %>"><%= vocab.name %></option>
<% }); %>
</select>
</td>
</tr>
<tr> <tr>
<td colspan="2"> <td colspan="2">
<% if(field['vocabulary-type'] != null && field['vocabulary-type'] != '') { %> <% if(field['vocabulary-type'] != null && field['vocabulary-type'] != '') { %>
@@ -168,36 +143,23 @@
<% } %> <% } %>
</td> </td>
</tr> </tr>
<tr>
<td colspan="2">
<label for="business" class="checkbox">
<input id="business" type="checkbox" <%= field.business ? "checked='checked'" : "" %> />
{% trans %}Business Fields{% endtrans %}
</label>
</td>
</tr>
<tr>
<td>
<label for="aggregable">{% trans %}Aggregation{% endtrans %}</label>
</td>
<td>
<select id="aggregable">
<option <%= field['aggregable'] == "0" ? 'selected' : '' %> value='0'>{% trans %}Not aggregated{% endtrans %}</option>
<option <%= field['aggregable'] == "10" ? 'selected' : '' %> value='10'>10 values</option>
<option <%= field['aggregable'] == "20" ? 'selected' : '' %> value='20'>20 values</option>
<option <%= field['aggregable'] == "50" ? 'selected' : '' %> value='50'>50 values</option>
<option <%= field['aggregable'] == "100" ? 'selected' : '' %> value='100'>100 values</option>
<option <%= field['aggregable'] == "-1" ? 'selected' : '' %> value='-1'>{% trans %}All values{% endtrans %}</option>
</select>
</td>
</tr>
<tr>
<td><label for="separator">{% trans %}Separator{% endtrans %}</label></td>
<td><input id="separator" type="text" value="<%= field.separator %>" /></td>
</tr>
</table> </table>
<h4>{% trans %}Display & action settings{% endtrans %}</h4> <h4>{% trans %}Display & action settings{% endtrans %}</h4>
<table> <table>
<tr>
<td>
<label for="multi" class="checkbox">
<input id="multi" type="checkbox" <%= field["multi"] ? "checked='checked'" : "" %> />
{% trans %}Multivalued{% endtrans %}
</label>
</td>
<td>
<label id="separatorZone" style="display:<%= field.multi ? "inherit" : "none" %>" for="separator">{% trans %}Separator{% endtrans %}
<input style="width:50px;" id="separator" type="text" value="<%= field.separator %>" />
</label>
</td>
</tr>
<tr> <tr>
<td> <td>
<label for="required" class="checkbox"> <label for="required" class="checkbox">
@@ -222,6 +184,14 @@
</label> </label>
</td> </td>
</tr> </tr>
<tr>
<td colspan="2">
<label for="business" class="checkbox">
<input id="business" type="checkbox" <%= field.business ? "checked='checked'" : "" %> />
{% trans %}Business Fields{% endtrans %}
</label>
</td>
</tr>
<tr> <tr>
<td> <td>
<label for="report" class="checkbox"> <label for="report" class="checkbox">
@@ -242,6 +212,36 @@
</select> </select>
</td> </td>
</tr> </tr>
<tr>
<td>
<label for="aggregable">{% trans %}Aggregation{% endtrans %}</label>
</td>
<td>
<select id="aggregable">
<option <%= field['aggregable'] == "0" ? 'selected' : '' %> value='0'>{% trans %}Not aggregated{% endtrans %}</option>
<option <%= field['aggregable'] == "10" ? 'selected' : '' %> value='10'>10 values</option>
<option <%= field['aggregable'] == "20" ? 'selected' : '' %> value='20'>20 values</option>
<option <%= field['aggregable'] == "50" ? 'selected' : '' %> value='50'>50 values</option>
<option <%= field['aggregable'] == "100" ? 'selected' : '' %> value='100'>100 values</option>
<option <%= field['aggregable'] == "-1" ? 'selected' : '' %> value='-1'>{% trans %}All values{% endtrans %}</option>
</select>
</td>
</tr>
<tr>
<td><label for="vocabulary-type">{% trans %}Vocabulary type{% endtrans %}</label></td>
<td>
<select id="vocabulary-type">
<option <%= field['vocabulary-type'] == null ? 'selected' : '' %> value=''></option>
<% _.each(vocabularyTypes, function(vocab) { %>
<option <%= field['vocabulary-type'] == vocab.type ? 'selected' : '' %> value="<%= vocab.type %>"><%= vocab.name %></option>
<% }); %>
</select>
</td>
</tr>
<tr>
<td><label for="tbranch">{% trans %}Thesaurus branch{% endtrans %}</label></td>
<td><input id="tbranch" type="text" value="<%= field.tbranch %>"/></td>
</tr>
</table> </table>
</div> </div>
</div> </div>

View File

@@ -154,7 +154,7 @@ class FieldsTest extends \PhraseanetAuthenticatedWebTestCase
]]; ]];
foreach ($fields as $fieldData) { foreach ($fields as $fieldData) {
$field = \databox_field::create($this->getApplication(), $databox, $fieldData['name'], $fieldData['multi']); $field = \databox_field::create($this->getApplication(), $databox, $fieldData['name']);
$field $field
->set_thumbtitle($fieldData['thumbtitle']) ->set_thumbtitle($fieldData['thumbtitle'])
->set_tag(\databox_field::loadClassFromTagName($fieldData['tag'])) ->set_tag(\databox_field::loadClassFromTagName($fieldData['tag']))
@@ -167,7 +167,8 @@ class FieldsTest extends \PhraseanetAuthenticatedWebTestCase
->set_tbranch($fieldData['tbranch']) ->set_tbranch($fieldData['tbranch'])
->set_report($fieldData['report']) ->set_report($fieldData['report'])
->setVocabularyControl(null) ->setVocabularyControl(null)
->setVocabularyRestricted(false); ->setVocabularyRestricted(false)
->set_multi($fieldData['multi']);
$field->save(); $field->save();
$fieldObjects[] = $field; $fieldObjects[] = $field;
} }
@@ -271,7 +272,7 @@ class FieldsTest extends \PhraseanetAuthenticatedWebTestCase
$app = $this->getApplication(); $app = $this->getApplication();
$databox = $this->getFirstDatabox($app); $databox = $this->getFirstDatabox($app);
$field = \databox_field::create($app, $databox, 'testfield' . mt_rand(), false); $field = \databox_field::create($app, $databox, 'testfield' . mt_rand());
$data = $field->toArray(); $data = $field->toArray();
@@ -289,7 +290,7 @@ class FieldsTest extends \PhraseanetAuthenticatedWebTestCase
$app = $this->getApplication(); $app = $this->getApplication();
$databox = $this->getFirstDatabox($app); $databox = $this->getFirstDatabox($app);
$field = \databox_field::create($app, $databox, 'testfield' . mt_rand(), false); $field = \databox_field::create($app, $databox, 'testfield' . mt_rand());
$data = $field->toArray(); $data = $field->toArray();
@@ -308,7 +309,7 @@ class FieldsTest extends \PhraseanetAuthenticatedWebTestCase
$app = $this->getApplication(); $app = $this->getApplication();
$databox = $this->getFirstDatabox($app); $databox = $this->getFirstDatabox($app);
$field = \databox_field::create($app, $databox, 'testfield' . mt_rand(), false); $field = \databox_field::create($app, $databox, 'testfield' . mt_rand());
$fieldId = $field->get_id(); $fieldId = $field->get_id();
$data = $field->toArray(); $data = $field->toArray();

View File

@@ -32,10 +32,12 @@ class databox_fieldTest extends \PhraseanetTestCase
$this->object_multi = $this->databox->get_meta_structure()->get_element_by_name($this->name_multi); $this->object_multi = $this->databox->get_meta_structure()->get_element_by_name($this->name_multi);
if (! $this->object_mono instanceof databox_field) { if (! $this->object_mono instanceof databox_field) {
$this->object_mono = databox_field::create(self::$DI['app'], $this->databox, $this->name_mono, false); $this->object_mono = databox_field::create(self::$DI['app'], $this->databox, $this->name_mono);
$this->object_mono->set_multi(false);
} }
if (! $this->object_multi instanceof databox_field) { if (! $this->object_multi instanceof databox_field) {
$this->object_multi = databox_field::create(self::$DI['app'], $this->databox, $this->name_multi, true); $this->object_multi = databox_field::create(self::$DI['app'], $this->databox, $this->name_multi);
$this->object_mono->set_multi(true);
} }
} }

View File

@@ -93,7 +93,8 @@ define([
"keyup input.input-label": "labelChangedAction", "keyup input.input-label": "labelChangedAction",
"change input[type=checkbox]": "fieldChangedAction", "change input[type=checkbox]": "fieldChangedAction",
"change select": "selectionChangedAction", "change select": "selectionChangedAction",
"click .lng-label a": "_toggleLabels" "click .lng-label a": "_toggleLabels",
"click input#multi": "multiClickdAction"
}, },
triggerControlledVocabulary: function (e) { triggerControlledVocabulary: function (e) {
if ($(e.target, this.$el).find("option:selected").val() === "") { if ($(e.target, this.$el).find("option:selected").val() === "") {
@@ -133,6 +134,16 @@ define([
return this; return this;
}, },
multiClickdAction: function (e) {
if($(e.target).is(":checked")) {
$("#separatorZone").show();
}
else {
$("#separatorZone").hide();
}
return this;
},
tagFieldChangedAction: function (e) { tagFieldChangedAction: function (e) {
var $this = this; var $this = this;
var fieldTag = $(e.target); var fieldTag = $(e.target);
@@ -154,7 +165,7 @@ define([
} else { } else {
$this.fieldChangedAction(e); $this.fieldChangedAction(e);
} }
} };
if ("" !== fieldTagValue) { if ("" !== fieldTagValue) {
var jqxhr = $.get("/admin/fields/tags/" + fieldTagValue, onFieldValid).fail(function () { var jqxhr = $.get("/admin/fields/tags/" + fieldTagValue, onFieldValid).fail(function () {