1 <?php
2
3 4 5 6 7 8 9 10
11
12 namespace Icybee\Modules\Nodes;
13
14 use Brickrouge\Element;
15
16 class PopNode extends \Brickrouge\Widget
17 {
18 const T_CONSTRUCTOR = '#popnode-constructor';
19
20 static protected function add_assets(\Brickrouge\Document $document)
21 {
22 parent::add_assets($document);
23
24 $document->css->add(DIR . 'public/module.css');
25 $document->js->add(DIR . 'public/module.js');
26 }
27
28 public function __construct(array $attributes=[])
29 {
30 parent::__construct('div', $attributes + [
31
32 self::T_CONSTRUCTOR => 'nodes',
33
34 'placeholder' => 'Sélectionner un enregistrement',
35 'class' => 'spinner',
36 'data-adjust' => 'adjust-node',
37 'tabindex' => 0
38
39 ]);
40 }
41
42 protected function alter_dataset(array $dataset)
43 {
44 return parent::alter_dataset($dataset + [
45
46 'constructor' => $this[self::T_CONSTRUCTOR],
47 'placeholder' => $this['placeholder']
48
49 ]);
50 }
51
52 protected function render_inner_html()
53 {
54 global $core;
55
56 $rc = parent::render_inner_html();
57
58 $constructor = $this[self::T_CONSTRUCTOR];
59 $value = $this['value'] ?: $this[self::DEFAULT_VALUE];
60 $record = null;
61
62 if ($value)
63 {
64 $model = $core->models[$constructor];
65
66 try
67 {
68 $record = is_numeric($value) ? $model[$value] : $this->getEntry($model, $value);
69 }
70 catch (\Exception $e)
71 {
72 \ICanBoogie\log_error('Missing record %nid', [ '%nid' => $value ]);
73 }
74 }
75
76 if (!$record)
77 {
78 $this->add_class('placeholder');
79 $value = null;
80 }
81
82 $rc .= new Element('input', [ 'type' => 'hidden', 'name' => $this['name'], 'value' => $value ]);
83
84 $placeholder = $this['placeholder'];
85
86 if ($placeholder)
87 {
88 $rc .= '<em class="spinner-placeholder">' . \ICanBoogie\escape($placeholder) . '</em>';
89 }
90
91 $rc .= '<span class="spinner-content">' . $this->getPreview($record) . '</span>';
92
93 return $rc;
94 }
95
96 protected function getEntry($model, $value)
97 {
98 return $model->where('title = ? OR slug = ?', $value, $value)->order('created_at DESC')->one;
99 }
100
101 protected function getPreview($entry)
102 {
103 if (!$entry)
104 {
105 return '';
106 }
107
108 $title = $entry->title;
109 $label = \ICanBoogie\shorten($title, 32, .75, $shortened);
110
111 $rc = '<span class="title"' . ($shortened ? ' title="' . \Brickrouge\escape($title) . '"' : '') . '>';
112 $rc .= \Brickrouge\escape($label) . '</span>';
113
114 return $rc;
115 }
116 }