diff --git a/fields/AnnuaireField.php b/fields/AnnuaireField.php new file mode 100644 index 0000000..ae42dde --- /dev/null +++ b/fields/AnnuaireField.php @@ -0,0 +1,158 @@ +entriesIds = array_map('trim', explode(',', $values[self::FIELD_ENTRIES_IDS])); + $this->propertyName = $this->name; + } + + protected function renderInput($entry) + { + + + $allTags = ''; + $response = $this->getOptions(); + if (isset($response)) { + $allTags = preg_replace('/"([^"]+)"\s*:\s*/', '$1:', json_encode($response)); + } + $value = $this->getValue($entry); + if (!isset($value)) { + if (isset($_GET[$this->propertyName])) { + $value = stripslashes($_GET[$this->propertyName]); + } else { + $value = stripslashes($this->default); + } + } + $script = '$(function(){ + var tagsexistants = ' . $allTags . '; + var pagetag = $(\'#formulaire .yeswiki-input-pagetag[name="' . $this->getName() . '"]\'); + pagetag.tagsinput({ + typeahead: { + afterSelect: function(val) { pagetag.tagsinput(\'input\').val(""); }, + source: tagsexistants, + autoSelect: false + }, + itemValue: function(item) { + return item.id; + }, + itemText: function(item) { + return item.name; + }, + trimValue: true, + confirmKeys: [13, 186, 188], + freeInput: false + });' . "\n"; + if (!empty($value)) { + $tags = explode(',', $value); + foreach ($tags as $t) { + $f = baz_valeurs_fiche($t); + $script .= ' pagetag.tagsinput(\'add\', {id:"' . $t . '", name:"' . htmlspecialchars($f['bf_titre']) . '"});' . "\n"; + } + } + $script .= ' +});'; + + $GLOBALS['wiki']->AddJavascript($script); + + + $GLOBALS['wiki']->AddJavascriptFile('tools/tags/libs/vendor/bootstrap-tagsinput.min.js'); + + return $this->render('@bazar/inputs/annuaire.twig', [ + 'value' => $value, + 'allTags' => $allTags + ]); + } + + public function formatValuesBeforeSave($entry) + { + $value = $this->getValue($entry); + + return [$this->propertyName => $value]; + } + + protected function renderStatic($entry) + { + $value = $this->getValue($entry); + + $tags = explode(',', $value); + + if (count($tags) > 0 && !empty($tags[0])) { + sort($tags); + $tags = array_map(function ($tag) { + $fiche = baz_valeurs_fiche($tag); + return '' . $fiche['bf_titre'] . ''; + }, $tags); + + return $this->render('@bazar/fields/annuaire.twig', [ + 'value' => join(' ', $tags) ?? '' + ]); + } else { + return ""; + } + } + + public function getName() + { + return $this->name; + } + + public function getOptions() + { + if (empty($this->options)) { + $this->loadOptionsFromTags(); + } + return parent::getOptions(); + } + + private function loadOptionsFromTags() + { + $entryManager = $this->getService(EntryManager::class); + + $fiches = $entryManager->search( + [ + 'queries' => '', + 'formsIds' => $this->entriesIds, + 'keywords' => '' + ], + true, // filter on read ACL + true // use Guard + ); + if (is_array($fiches)) { + asort($fiches); + } + + $this->options = []; + $i = 0; + foreach ($fiches as $fiche) { + $this->options[$i]['id'] = $fiche['id_fiche']; + $this->options[$i]['name'] = $fiche['bf_titre']; + $i++; + } + } +}