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: class DetailView extends RecordView {
43:
44: public $JSClass = 'DetailView';
45:
46: 47: 48: 49:
50: public $nameLink = false;
51:
52: 53: 54: 55:
56: public $halfWidth = false;
57:
58: 59: 60: 61:
62: public $disableInlineEditingFor = array ();
63:
64: public function getPackages () {
65: return array_merge (parent::getPackages(), array(
66: 'DetailViewJS' => array(
67: 'baseUrl' => Yii::app()->baseUrl.'/js/recordView/',
68: 'js' => array(
69: 'inlineEditor.js',
70: 'DetailView.js'
71: ),
72: 'depends' => array('RecordViewJS')
73: )
74: ));
75: }
76:
77: public function getJSClassParams () {
78: return array_merge (parent::getJSClassParams(), array(
79: 'inlineEdit' => Yii::app()->controller->checkPermissions($this->model,'edit'),
80: ));
81: }
82:
83: public function getTranslations () {
84: return array (
85: 'unsavedChanges' => Yii::t('app', 'There are unsaved changes on this page')
86: );
87: }
88:
89: 90: 91:
92: public function getMainOptions () {
93: $width = $this->halfWidth ? 'half-width' : '';
94:
95: return X2Html::mergeHtmlOptions (array (
96: 'class' => "x2-layout detail-view $width",
97: 'id' => $this->namespace . 'detail-view',
98: ), $this->htmlOptions);
99: }
100:
101: 102: 103:
104: public function getItemOptions($item, Fields $field) {
105: $inlineEdit = $this->canEdit($field) ? ' inline-edit' : '';
106:
107: $parent = parent::getItemOptions($item, $field);
108: $parent['class'] .= $inlineEdit;
109:
110: return $parent;
111: }
112:
113: 114: 115: 116:
117: public function renderAttribute ($item, Fields $field) {
118: $class = '';
119: $style = '';
120: if ($field->type == 'text') {
121: $class = 'textBox';
122: $style .= 'min-height:' . $item['height'] . 'px';
123: }
124:
125: $html = X2Html::openTag('div', array(
126: 'class' => "formInputBox $class",
127: 'style' => $style,
128: ));
129:
130: if($this->canEdit($field)) {
131: $html .= $this->renderInput ($item, $field);
132: }
133:
134: $html .= X2Html::tag('span', array(
135: 'class' => 'model-attribute',
136: 'id' => $field->modelName.'_'.$field->fieldName.'_field-field'
137: ));
138:
139: if (isset($this->specialFields[$field->fieldName])) {
140: $html .= $this->specialFields[$field->fieldName];
141: } else if ($field->fieldName == 'name' && $this->nameLink &&
142: $this->model->asa('X2LinkableBehavior')) {
143: $html .= $this->model->link;
144: } else {
145: $rendered = $this->model->renderAttribute ($field->fieldName, true, false);
146: if (!$rendered) $rendered = ' ';
147: $html .= $rendered;
148: }
149:
150: $html .= '</span>';
151: $html .= '</div>';
152:
153: if ($this->canEdit($field)) {
154: $html .= $this->renderInlineButtons();
155: }
156:
157: return $html;
158: }
159:
160: 161: 162: 163: 164: 165:
166: public function renderInput ($item, $field){
167: $html = X2Html::openTag('span', array(
168: 'class' => 'model-input',
169: 'id' => $field->modelName.'_'.$field->fieldName.'_field-input',
170: 'style' => 'display:none',
171: ));
172:
173: $html .= $this->model->renderInput($field->fieldName, array(
174: 'tabindex' => $item['tabindex'],
175: 'disabled' => $item['readOnly'] ? 'disabled' : '',
176: ));
177:
178: $html .= '</span>';
179: return $html;
180: }
181:
182:
183: 184: 185:
186: public function renderInlineButtons () {
187: $html = '<div class="inline-edit-icons">';
188:
189: $html .= CHtml::link (X2Html::fa('fa-edit'), '#', array(
190: 'class' => 'edit-icon active',
191: 'title' => Yii::t('app','Edit field'),
192: ));
193:
194: $html .= CHtml::link (X2Html::fa('fa-times-circle'), '#', array(
195: 'class' => 'cancel-icon',
196: 'title' => Yii::t('app', 'Cancel changes'),
197: ));
198:
199: $html .= CHtml::link (X2Html::fa('fa-check-circle'), '#', array(
200: 'class' => 'confirm-icon',
201: 'title' => Yii::t('app', 'Confirm changes'),
202: ));
203:
204: $html .= '</div>';
205: return $html;
206: }
207:
208:
209:
210: public function getLayoutData() {
211: $layoutData = Yii::app()->cache->get('form_' . $this->modelName . '_' . $this->scenario);
212: if ($layoutData) {
213: return $layoutData;
214: }
215:
216: $layout = FormLayout::model()->findByAttributes(
217: array(
218: 'model' => ucfirst($this->modelName),
219: 'defaultView' => 1,
220: 'scenario' => $this->scenario
221: ));
222:
223: if(!$layout && $this->scenario === 'Inline') {
224: $layout = FormLayout::model()->findByAttributes(
225: array(
226: 'model' => ucfirst($this->modelName),
227: 'defaultView' => 1,
228: 'scenario' => 'Default'
229: ));
230: }
231:
232: if(isset($layout)) {
233: $layoutData = json_decode($layout->layout, true);
234: Yii::app()->cache->set('form_' . $this->modelName . '_' . $this->scenario, $this->layoutData, 0);
235: }
236:
237: return $layoutData;
238: }
239:
240: 241: 242: 243: 244:
245: public function canEdit(Fields $field) {
246: return !in_array ($field->fieldName, $this->disableInlineEditingFor) &&
247: parent::canEdit ($field) && $this->scenario !== 'Inline';
248: }
249: }
250:
251: ?>
252: