1: <?php
2:
3: namespace Charcoal\Admin\Widget\Cms;
4:
5: use Exception;
6: use RuntimeException;
7:
8:
9: use Charcoal\Model\ModelInterface;
10:
11:
12: use Charcoal\Factory\FactoryInterface;
13:
14:
15: use Charcoal\Property\PropertyInterface;
16:
17:
18: use Charcoal\Cms\AbstractSection;
19:
20: 21: 22:
23: trait SectionTableTrait
24: {
25: 26: 27: 28: 29:
30: private $sectionFactory;
31:
32: 33: 34: 35: 36: 37:
38: protected function setSectionFactory(FactoryInterface $factory)
39: {
40: $this->sectionFactory = $factory;
41:
42: return $this;
43: }
44:
45: 46: 47: 48: 49: 50:
51: public function sectionFactory()
52: {
53: if (!isset($this->sectionFactory)) {
54: throw new RuntimeException(
55: sprintf('Section Model Factory is not defined for "%s"', get_class($this))
56: );
57: }
58:
59: return $this->sectionFactory;
60: }
61:
62: 63: 64: 65: 66: 67: 68: 69: 70: 71:
72: protected function parsePropertyCell(
73: ModelInterface $object,
74: PropertyInterface $property,
75: $propertyValue
76: ) {
77: $propertyIdent = $property->ident();
78:
79: switch ($propertyIdent) {
80: case 'template_ident':
81: if ($object->templateIdent() === 'volleyball/template/page') {
82: $propertyValue = sprintf(
83: '<span aria-hidden="true">─</span><span class="sr-only">%s</span>',
84: $this->translator()->translation([
85: 'en' => 'Default Template',
86: 'fr' => 'Modèle par défaut'
87: ])
88: );
89: }
90: break;
91:
92: case 'title':
93: case 'menu_label':
94: $sectionTag = null;
95: $sectionType = $object->sectionType();
96:
97: switch ($sectionType) {
98: case AbstractSection::TYPE_EXTERNAL:
99: $externalUrl = (string)$object->externalUrl();
100: $linkExcerpt = '';
101: $tagTemplate = '<span class="glyphicon glyphicon-link" data-toggle="tooltip" '.
102: 'data-placement="auto" title="%1$s"></span>';
103:
104: if ($externalUrl) {
105: $tagTemplate = '<a class="btn btn-default btn-xs" href="%2$s" target="_blank">'.
106: '<span class="glyphicon glyphicon-link" aria-hidden="true"></span> '.
107: '<span class="sr-only">URL:</span> %3$s'.
108: '</a>';
109:
110: $linkExcerpt = $this->abridgeUri($externalUrl);
111: }
112:
113: $p = $object->p('section_type');
114: $propertyValue .= sprintf(
115: ' '.$tagTemplate,
116: $p->displayVal($p->val()),
117: $externalUrl,
118: $linkExcerpt
119: );
120: break;
121: }
122: break;
123: }
124:
125: if ($propertyIdent === 'title') {
126: if (is_callable([ $object, 'navMenu' ]) && $object->navMenu()) {
127: $propertyValue .= sprintf(
128: ' '.
129: '<span class="glyphicon glyphicon-list" data-toggle="tooltip" '.
130: 'data-placement="auto" title="%s"></span>',
131: $this->translator()->translation([
132: 'en' => 'Present in a menu',
133: 'fr' => 'Présent dans un menu'
134: ])
135: );
136: }
137:
138: if (is_callable([ $object, 'locked' ]) && $object->locked()) {
139: $propertyValue .= sprintf(
140: ' '.
141: '<span class="glyphicon glyphicon-lock" data-toggle="tooltip" '.
142: 'data-placement="auto" title="%s"></span>',
143: $object->p('locked')->label()
144: );
145: }
146: }
147:
148: return parent::parsePropertyCell($object, $property, $propertyValue);
149: }
150:
151: 152: 153: 154: 155: 156:
157: private function abridgeUri($uri)
158: {
159: $i = 30;
160: $j = 12;
161:
162: if (mb_strlen($uri) <= $i) {
163: return $uri;
164: }
165:
166: $host = rtrim(parse_url($uri, PHP_URL_HOST), '/');
167: $path = '/'.ltrim(parse_url($uri, PHP_URL_PATH), '/');
168:
169: if ($host === getenv('HTTP_HOST')) {
170: $i = 50;
171: $j = 22;
172:
173: $host = '';
174: } else {
175: if (mb_strlen($host) > $i && mb_strlen($path) > $i) {
176: return $this->abridgeStr($uri, 50, 22);
177: }
178:
179: $host = $this->abridgeStr($host, 20, 7);
180: }
181:
182: $path = $this->abridgeStr($path, $i, $j);
183:
184: return $host.$path;
185: }
186:
187: 188: 189: 190: 191: 192: 193: 194: 195:
196: private function abridgeStr($str, $l = 30, $a = 12, $z = 12)
197: {
198: if (mb_strlen($str) > $l) {
199: $str = (($a > 0) ? mb_substr($str, 0, $a) : '').'…'.(($z > 0) ? mb_substr($str, -$z) : '');
200: }
201:
202: return $str;
203: }
204: }
205: