Skip to content

Commit

Permalink
feat(section): show conditions
Browse files Browse the repository at this point in the history
Signed-off-by: Thierry Bugier <tbugier@teclib.com>
  • Loading branch information
btry committed Dec 13, 2019
1 parent c0a3151 commit 0d41650
Show file tree
Hide file tree
Showing 11 changed files with 308 additions and 221 deletions.
33 changes: 25 additions & 8 deletions ajax/condition.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,28 +44,45 @@
}
$itemtype = $_REQUEST['itemtype'];

if (!isset($_REQUEST['plugin_formcreator_sections_id'])) {
if (!class_exists($itemtype)) {
http_response_code(400);
exit;
}
$sectionId = (int) $_REQUEST['plugin_formcreator_sections_id'];

if (!class_exists($itemtype)) {
if (!is_subclass_of($itemtype, CommonDBTM::class)) {
http_response_code(400);
exit;
}

if (!is_subclass_of($itemtype, CommonDBTM::class)) {
$form = new PluginFormcreatorForm();
switch ($itemtype) {
case PluginFormcreatorQuestion::class:
if (!isset($_REQUEST['plugin_formcreator_sections_id'])) {
http_response_code(400);
exit;
}
$sectionId = (int) $_REQUEST['plugin_formcreator_sections_id'];
$section = new PluginFormcreatorSection();
$section->getFromDB($sectionId);
$form->getFromDBBySection($section);
break;
case PluginFormcreatorSection::class:
if (!isset($_REQUEST['plugin_formcreator_forms_id'])) {
http_response_code(400);
exit;
}
$formId = (int) $_REQUEST['plugin_formcreator_forms_id'];
$form->getFromDB($formId);
break;
}

if ($form->isNewItem()) {
http_response_code(400);
exit;
}

// get an empty condition HTML table row
$item = new $itemtype();
$item->getFromDB($itemId);
$section = new PluginFormcreatorSection();
$section->getFromDB($sectionId);
$form = new PluginFormcreatorForm();
$form->getFromDBBySection($section);
$condition = new PluginFormcreatorCondition();
echo $condition->getConditionHtml($form, $itemtype, $itemId);
2 changes: 1 addition & 1 deletion css/styles.css
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ form.formcreator_form {
margin: 0 0 10px;
}

.formcreator_form > .form_section > h2 {
.formcreator_form .form_section > h2 {
background: #1B2F62; /*#E1D39E;*/
color: #FFF;
margin: 0 0 10px;
Expand Down
63 changes: 20 additions & 43 deletions inc/condition.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,14 @@ public static function getEnumShowCondition() {
];
}

public function getEnumShowRule() {
return [
self::SHOW_RULE_ALWAYS => __('Always displayed', 'formcreator'),
self::SHOW_RULE_HIDDEN => __('Hidden unless', 'formcreator'),
self::SHOW_RULE_SHOWN => __('Displayed unless', 'formcreator'),
];
}

public static function import(PluginFormcreatorLinker $linker, $input = [], $containerId = 0) {
global $DB;

Expand Down Expand Up @@ -167,34 +175,6 @@ public function export($remove_uuid = false) {
return $condition;
}

/**
* get show / hide conditions for a question
*
* @param int $questionId
* @return array
*/
public function getConditionsFromQuestion($questionId) {
global $DB;

$conditions = [];
$rows = $DB->request([
'SELECT' => ['id'],
'FROM' => self::getTable(),
'WHERE' => [
'itemtype' => PluginFormcreatorQuestion::class,
'items_id' => $questionId
],
'ORDER' => 'order ASC'
]);
foreach ($rows as $row) {
$condition = new static();
$condition->getFromDB($row['id']);
$conditions[] = $condition;
}

return $conditions;
}

/**
* get conditions applied to an item
*
Expand Down Expand Up @@ -235,10 +215,6 @@ public function getConditionsFromItem(PluginFormcreatorConditionnableInterface $
* @return void
*/
public function showConditionsForItem($form, PluginFormcreatorConditionnableInterface $item) {
$ID = 0;
if (!$item->isNewItem()) {
$ID = $item->getID();
}
$rand = mt_rand();
echo '<tr>';
echo '<th colspan="4">';
Expand All @@ -248,6 +224,7 @@ public function showConditionsForItem($form, PluginFormcreatorConditionnableInte
echo '</th>';
echo '</tr>';

// Get conditionsexisting conditions for the item
$conditions = $this->getConditionsFromItem($item);
reset($conditions);
$condition = array_shift($conditions);
Expand All @@ -260,15 +237,15 @@ public function showConditionsForItem($form, PluginFormcreatorConditionnableInte

echo '<tr">';
echo '<td colspan="4">';
Dropdown::showFromArray('show_rule', [
self::SHOW_RULE_ALWAYS => __('Always displayed', 'formcreator'),
self::SHOW_RULE_HIDDEN => __('Hidden unless', 'formcreator'),
self::SHOW_RULE_SHOWN => __('Displayed unless', 'formcreator'),
], [
'value' => $item->fields['show_rule'],
'on_change' => 'plugin_formcreator_toggleCondition(this);',
'rand' => $rand,
]);
Dropdown::showFromArray(
'show_rule',
$this->getEnumShowRule(),
[
'value' => $item->fields['show_rule'],
'on_change' => 'plugin_formcreator_toggleCondition(this, "' . get_class($item) . '");',
'rand' => $rand,
]
);
echo '</td>';
echo '</tr>';
}
Expand Down Expand Up @@ -300,15 +277,15 @@ public function getConditionHtml($form, $itemtype, $itemId = 0, $isFirst = false
}
$rand = mt_rand();

// Get list of question in the form of the item
if (!is_subclass_of($itemtype, PluginFormcreatorConditionnableInterface::class)) {
throw new Exception("$itemtype is not a " . PluginFormcreatorConditionnableInterface::class);
}
$item = new $itemtype();
$questionListCondition = [];
if ($itemtype == PluginFormcreatorQuestion::class) {
$questionListCondition = [PluginFormcreatorQuestion::getTable() . '.id' => ['<>', $itemId]];
}
$questionsInForm = $item->getQuestionsFromForm($form->getID(), $questionListCondition);
$questionsInForm = (new PluginFormcreatorQuestion)->getQuestionsFromForm($form->getID(), $questionListCondition);
$questions_tab = [];
foreach ($questionsInForm as $question) {
if (strlen($question->fields['name']) > 30) {
Expand Down
9 changes: 5 additions & 4 deletions inc/conditionnableinterface.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,10 @@

interface PluginFormcreatorConditionnableInterface
{
/**
* get the form of the item
* @return boolean|PluginFormcreatorForm the form or false if not found
/**
* Updates the conditions of the question
* @param array $input
* @return boolean true if success, false otherwise
*/
public function getForm();
public function updateConditions($input);
}
Loading

0 comments on commit 0d41650

Please sign in to comment.