158 lines
4.1 KiB
PHP
158 lines
4.1 KiB
PHP
<?php
|
|
|
|
namespace YesWiki\Custom\Field;
|
|
|
|
use Psr\Container\ContainerInterface;
|
|
use YesWiki\Bazar\Service\EntryManager;
|
|
use YesWiki\Bazar\Field\EnumField;
|
|
// use Exception;
|
|
// use YesWiki\Bazar\Exception\UserFieldException;
|
|
// use YesWiki\Bazar\Service\FormManager;
|
|
// use YesWiki\Core\Controller\AuthController;
|
|
// use YesWiki\Core\Controller\UserController;
|
|
// use YesWiki\Core\Exception\UserNameAlreadyUsedException;
|
|
// use YesWiki\Core\Service\ConfigurationService;
|
|
// use YesWiki\Core\Service\Mailer;
|
|
// use YesWiki\Core\Service\UserManager;
|
|
// use YesWiki\Wiki;
|
|
|
|
/**
|
|
* @Field({"annuaire"})
|
|
*/
|
|
class AnnuaireField extends EnumField
|
|
{
|
|
|
|
protected $entriesIds;
|
|
|
|
protected const FIELD_ENTRIES_IDS = 3;
|
|
public function __construct(array $values, ContainerInterface $services)
|
|
{
|
|
parent::__construct($values, $services);
|
|
$this->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 '<a class="tag-label label label-info modalbox" href="' . $this->getWiki()->href('', $tag) . '" title="' . htmlspecialchars($fiche['bf_titre']) . '">' . $fiche['bf_titre'] . '</a>';
|
|
}, $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++;
|
|
}
|
|
}
|
|
}
|