1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35:
36:
37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55:
56: abstract class RecordView extends X2Widget {
57:
58: 59: 60:
61: public $htmlOptions = array (
62: 'class' => 'x2-layout-island'
63: );
64:
65: 66: 67: 68:
69: public $JSClass = 'RecordView';
70:
71: 72: 73: 74:
75: public $modelName;
76:
77: 78: 79: 80:
81: public $model;
82:
83: 84: 85: 86:
87: public $scenario = 'Default';
88:
89: 90: 91: 92:
93: public $suppressFields = array();
94:
95: 96: 97: 98:
99: public $specialFields = array();
100:
101: 102: 103: 104:
105: public $layoutData;
106:
107: 108: 109: 110:
111: protected $_formSettings;
112:
113: 114: 115: 116:
117: protected $_fieldPermissions;
118:
119: 120: 121: 122:
123: protected $_fields;
124:
125: 126: 127: 128: 129: 130: 131: 132:
133: public abstract function renderAttribute ($item, Fields $field);
134:
135: 136: 137: 138:
139: public abstract function getLayoutData ();
140:
141: public function setFormSettings (array $formSettings) {
142: $this->_formSettings = $formSettings;
143: }
144:
145: 146: 147:
148: public function init () {
149: parent::init();
150:
151:
152: if (!isset($this->modelName)) {
153: $this->modelName = get_class($this->model);
154: }
155:
156:
157: if (!isset($this->layoutData)) {
158: $this->layoutData = $this->getLayoutData();
159: }
160:
161:
162: if (!isset ($this->_formSettings))
163: $this->_formSettings = Profile::getFormSettings ($this->modelName);
164:
165:
166: $this->_fieldPermissions = $this->getFieldPermissions();
167:
168:
169: $this->_fields = $this->getFields();
170: }
171:
172: public function run () {
173:
174: if(!$this->layoutData) return;
175: if(!$this->_fields) return;
176:
177:
178: echo $this->renderMain ();
179:
180:
181: $this->registerPackages();
182:
183:
184: $this->instantiateJSClass (true);
185:
186: }
187:
188: public function getPackages () {
189: return array_merge (parent::getPackages (), array(
190: 'RecordViewJS' => array (
191: 'baseUrl' => Yii::app()->baseUrl,
192: 'js' => array (
193: 'js/recordView/RecordView.js'
194: ),
195: 'depends' => array('auxlib')
196: )
197: ));
198: }
199:
200: public function getJSClassParams () {
201: return array_merge (parent::getJSClassParams(), array(
202: 'modelId' => $this->model->id,
203: 'modelName' => $this->modelName,
204: ));
205: }
206:
207: 208: 209: 210:
211: public function getMainOptions () {
212: return array (
213: 'class' => 'x2-layout'
214: );
215: }
216:
217: 218: 219: 220:
221: public function getSectionOptions ($section, $collapsed) {
222: $visibility = $collapsed ? 'hideSection' : 'showSection';
223: $collapsible = $section['collapsible'] ? 'collapsible' : '';
224:
225: return array (
226: 'class' => "formSection $collapsible $visibility"
227: );
228: }
229:
230: 231: 232: 233:
234: public function getRowOptions($row) {
235: return array (
236: 'class' => 'formSectionRow'
237: );
238: }
239:
240: 241: 242: 243:
244: public function getColumnOptions($col, $count) {
245: $width = $col['width'];
246: if (!preg_match('/^\d+(\.\d+)?%$/', $col['width']) && $count > 0) {
247: $width = (100.0/$count).'%';
248: }
249:
250: return array(
251: 'style' => "width:$width",
252: 'class' => "formSectionColumn"
253: );
254: }
255:
256: 257: 258: 259:
260: public function getItemOptions ($item, Fields $field) {
261:
262:
263: $labelClass = $item['labelType'].'Label';
264: if ($item['labelType'] == 'none') {
265: $labelClass = 'noLabel';
266: }
267:
268: $inputClass = $field->type == 'text' ? " textarea" : "";
269:
270: return array (
271: 'id' => "{$field->modelName}_{$field->fieldName}_field",
272: 'class' => "formItem $labelClass $inputClass",
273: );
274: }
275:
276: 277: 278: 279:
280: public function renderMain () {
281: $html = X2Html::openTag('div', $this->getMainOptions());
282:
283: $html .= $this->renderSections ();
284:
285: $html .= '</div>';
286: return $html;
287: }
288:
289: protected function renderSections () {
290: $html = '';
291: foreach($this->layoutData['sections'] as $i => $section) {
292:
293:
294: $collapsed =
295: (isset($this->_formSettings[$i]) && !$this->_formSettings[$i]) ||
296: (!isset ($this->_formSettings[$i]) && isset ($section['collapsedByDefault']) &&
297: $section['collapsedByDefault']);
298:
299:
300:
301: $html .= $this->renderSection($section, $collapsed);
302: }
303: return $html;
304: }
305:
306: 307: 308: 309:
310: public function renderSection ($section, $collapsed) {
311: $section = array_merge (array(
312: 'title' => '',
313: 'collapsible' => false,
314: 'rows' => array()
315: ), $section);
316:
317:
318: $collapsed &= $section['collapsible'];
319: $html = X2Html::openTag ('div', $this->getSectionOptions($section, $collapsed));
320:
321: $html .= $this->renderSectionHeader ($section);
322: $html .= X2Html::openTag ('div', array(
323: 'class' => 'tableWrapper',
324: 'style' => $collapsed ? 'display:none' : ''
325: ));
326:
327: foreach($section['rows'] as $row) {
328: $html .= $this->renderRow ($row);
329: }
330:
331: $html .= '</div>';
332: $html .= '</div>';
333:
334: return $html;
335: }
336:
337: 338: 339: 340:
341: public function ($section) {
342: $html = X2Html::openTag('div', array(
343: 'class' => 'formSectionHeader'
344: ));
345:
346:
347: if ($section['collapsible']) {
348: $html .= X2Html::link (
349: X2Html::fa('fa-caret-down'),
350: 'javascript:void(0)',
351: array(
352: 'class' => 'formSectionHide'
353: )
354: );
355:
356: $html .= X2Html::link (
357: X2Html::fa('fa-caret-right'),
358: 'javascript:void(0)',
359: array(
360: 'class' => 'formSectionShow'
361: )
362: );
363: }
364:
365: $html .= X2Html::tag('span', array(
366: 'class' => 'sectionTitle',
367: 'title' => addslashes($section['title'])
368: ), Yii::t(strtolower(Yii::app()->controller->id), $section['title'])
369: );
370:
371: $html .= '</div>';
372: return $html;
373: }
374:
375: 376: 377:
378: public function renderRow ($row) {
379: $row = array_merge(array(
380: 'cols' => array(),
381: ), $row);
382:
383: $html = X2Html::openTag('div', $this->getRowOptions($row));
384: foreach($row['cols'] as $col) {
385: $html .= $this->renderColumn($col, count($row['cols']));
386: }
387: $html .= '</div>';
388:
389: return $html;
390: }
391:
392: 393: 394: 395: 396: 397:
398: public function renderColumn ($col, $count) {
399: $col = array_merge(array(
400: 'width' => '',
401: 'items' => array()
402: ), $col);
403:
404: $html = X2Html::openTag('table', $this->getColumnOptions($col, $count));
405: foreach($col['items'] as &$item) {
406: $html .= $this->renderItem ($item);
407: }
408: $html .= '</table>';
409:
410: return $html;
411: }
412:
413:
414: public function renderItem ($item) {
415: $item = array_merge (array(
416: 'name' => '',
417: 'labelType' => 'left',
418: 'readOnly' => '',
419: 'height' => '',
420: 'tabindex' => ''
421: ), $item);
422: if (!is_string ($item['labelType']) ||
423: !in_array (strtolower ($item['labelType']), array ('none', 'left', 'top', 'inline'))) {
424: $item['labelType'] = 'left';
425: }
426:
427:
428: $fieldName = preg_replace('/^formItem_/u', '', $item['name']);
429: if (!isset($this->_fields[$fieldName])) {
430: return;
431: }
432:
433:
434: $field = $this->_fields[$fieldName];
435: if (in_array($fieldName, $this->suppressFields) || !$this->canView($field)) {
436: return;
437: }
438:
439: if (!$this->canEdit($field)) {
440: $item['readOnly'] = true;
441: }
442:
443: $html = '';
444:
445: if ($item['labelType'] != 'top') {
446: $html .= X2Html::openTag ('tr', $this->getItemOptions($item, $field));
447: }
448:
449:
450:
451: $fn = 'render'.$item['labelType'].'label';
452:
453: $html .= $this->$fn($item, $field);
454:
455: $html .= '</tr>';
456:
457: return $html;
458: }
459:
460: 461: 462: 463:
464: public function renderNoneLabel ($item, Fields $field) {
465: $html = '';
466:
467: $html .= "<td class='attribute' colspan='2'>";
468: $html .= $this->renderAttribute ($item, $field);
469: $html .= '</td>';
470:
471: return $html;
472: }
473:
474: 475: 476:
477: public function renderInlineLabel ($item, Fields $field) {
478: return $this->renderNoneLabel($item, $field);
479: }
480:
481: 482: 483: 484: 485: 486: 487: 488:
489: public function renderTopLabel ($item, Fields $field) {
490: $html = '';
491:
492: $html .= '<tr class="formItem topLabel">';
493: $html .= "<td class='label' colspan='2'>";
494: $html .= $this->renderLabel ($field);
495: $html .= '</td>';
496: $html .= '</tr>';
497:
498: $html .= X2Html::openTag ('tr', $this->getItemOptions($item, $field));
499: $html .= $this->renderNoneLabel($item, $field);
500:
501: return $html;
502: }
503:
504: 505: 506:
507: public function renderLeftLabel ($item, Fields $field) {
508: $html = '';
509:
510: $html .= "<td class='label' >";
511: $html .= $this->renderLabel ($field);
512: $html .= '</td>';
513:
514: $html .= "<td class='attribute'>";
515: $html .= $this->renderAttribute ($item, $field);
516: $html .= '</td>';
517:
518: return $html;
519: }
520:
521: 522: 523:
524: public function renderLabel ($field) {
525: return X2Html::label ($this->model->getAttributeLabel($field->fieldName), false);
526: }
527:
528: 529: 530: 531:
532: public function getFieldPermissions () {
533:
534: if (Yii::app()->params->isAdmin || empty(Yii::app()->params->roles)) {
535: return;
536: }
537:
538: return $this->model->getFieldPermissions();
539: }
540:
541: 542: 543: 544:
545: public function getFields () {
546: $fields = array();
547: if (method_exists($this->model, 'getFields')) {
548: $fields = $this->model->getFields(true);
549: } else {
550: foreach (X2Model::model('Fields')->findAllByAttributes(
551: array('modelName' => ucfirst($this->modelName))) as $fieldModel) {
552: $fields[$fieldModel->fieldName] = $fieldModel;
553: }
554: }
555:
556: return $fields;
557: }
558:
559: 560: 561: 562:
563: public function canEdit(Fields $field) {
564: if(Yii::app()->params->isAdmin){
565: return true;
566: }
567:
568:
569: if($field->readOnly) {
570: return false;
571: }
572:
573:
574: if (!isset($this->fieldPermissions[$field->fieldName])) {
575: return true;
576: }
577:
578:
579: if ($this->fieldPermissions[$field->fieldName] === 2) {
580: return true;
581: }
582:
583:
584: return false;
585: }
586:
587: public function canView($field) {
588: if(Yii::app()->params->isAdmin){
589: return true;
590: }
591:
592:
593: if (!isset($this->fieldPermissions[$field->fieldName])) {
594: return true;
595: }
596:
597:
598: if ($this->fieldPermissions[$field->fieldName] >= 1) {
599: return true;
600: }
601:
602:
603: return false;
604: }
605:
606: }
607:
608: ?>
609: