1 <?php
  2 
  3   4   5   6   7   8   9  10 
 11 
 12 namespace Icybee\Modules\Nodes;
 13 
 14 use ICanBoogie\I18n;
 15 
 16 use Brickrouge;
 17 use Brickrouge\Element;
 18 use Brickrouge\Text;
 19 
 20 class TitleSlugCombo extends \Brickrouge\Widget
 21 {
 22     const T_NODEID = '#node-id';
 23     const T_SLUG_NAME = '#slug-name';
 24 
 25     static protected function add_assets(\Brickrouge\Document $document)
 26     {
 27         parent::add_assets($document);
 28 
 29         $document->css->add(DIR . 'public/module.css');
 30         $document->js->add(DIR . 'public/module.js');
 31     }
 32 
 33     private $title_el;
 34     private $slug_tease;
 35     private $slug_el;
 36 
 37     public function __construct(array $attributes=[])
 38     {
 39         $attributes += [
 40 
 41             self::T_SLUG_NAME => null,
 42             Element::LABEL => null,
 43             Element::LABEL_POSITION => 'before'
 44 
 45         ];
 46 
 47         $label = $attributes[Element::LABEL];
 48 
 49         parent::__construct('div', $attributes + [
 50 
 51             Element::CHILDREN => [
 52 
 53                 $this->title_el = new Text([
 54 
 55                     Element::LABEL_POSITION => $attributes[Element::LABEL_POSITION],
 56                     Element::REQUIRED => true
 57 
 58                 ]),
 59 
 60                 $this->slug_tease = new Element('span', [
 61 
 62                     self::INNER_HTML => ' ',
 63 
 64                     'class' => 'slug-reminder small'
 65 
 66                 ]),
 67 
 68                 '<a href="#slug-collapse" class="small">' . I18n\t('fold', [], [ 'scope' => 'titleslugcombo.element' ]) . '</a>',
 69 
 70                 '<div class="slug">',
 71 
 72                 $this->slug_el = new Text([
 73 
 74                     Element::LABEL => 'slug',
 75                     Element::LABEL_POSITION => 'above',
 76                     Element::DESCRIPTION => 'slug',
 77 
 78                     'name' => $attributes[self::T_SLUG_NAME]
 79 
 80                 ]),
 81 
 82                 '</div>'
 83             ],
 84 
 85             'data-auto-label' => '<em>' . I18n\t('auto', [], [ 'scope' => 'titleslugcombo.element' ]) . '</em>'
 86 
 87         ]);
 88     }
 89 
 90     public function offsetSet($offset, $value)
 91     {
 92         if ($offset == 'name')
 93         {
 94             $this->title_el['name'] = $value;
 95 
 96             if (!$this->slug_el['name'])
 97             {
 98                 $this->slug_el['name'] = $value . 'slug';
 99             }
100         }
101 
102         parent::offsetSet($offset, $value);
103     }
104 
105     protected function render_inner_html()
106     {
107         global $core;
108 
109         $slug = $this->slug_el['value'];
110 
111         $tease = '<strong>Slug :</strong> ';
112         $tease .= '<a href="#slug-edit" title="' . I18n\t('edit', [], [ 'scope' => 'titleslugcombo.element' ]) . '">' . ($slug ? \ICanBoogie\escape(\ICanBoogie\shorten($slug)) : $this->dataset['auto-label']) . '</a>';
113         $tease .= ' <span>– <a href="slug-delete" class="warn">' . I18n\t('reset', [], [ 'scope' => 'titleslugcombo.element' ]) . '</a></span>';
114 
115         $this->slug_tease->inner_html = $tease;
116 
117         $rc = parent::render_inner_html();
118 
119         $nid = $this[self::T_NODEID];
120 
121         if ($nid)
122         {
123             $node = $core->models['nodes'][$nid];
124 
125             if ($node && $node->url && $node->url[0] != '#')
126             {
127                 $url = $node->url;
128                 $url_label = \ICanBoogie\shorten($url, 64);
129 
130                 $rc .= '<p class="small light">';
131                 $rc .= '<strong>URL :</strong> ' . $url_label;
132             }
133         }
134 
135         return $rc;
136     }
137 }