1: <?php
2:
3: App::uses('AppHelper', 'View/Helper');
4: App::uses('ExtendedFieldsAccessControl', 'ExtendedScaffold.Lib');
5:
6: class DetailHelper extends AppHelper {
7:
8: public $helpers = array(
9: 'AccessControl.AccessControl',
10: 'Base.CakeLayers',
11: 'ExtendedScaffold.Lists',
12: 'ExtendedScaffold.ViewUtil',
13: 'ExtendedScaffold.ExtendedFieldSet',
14: 'ExtendedScaffold.ListFieldSet',
15: );
16:
17: 18: 19: 20:
21: public $settings = array(
22: 'listSettings' => array(),
23: );
24:
25: 26: 27: 28: 29:
30: public function commonViewFieldList($fields) {
31: $b = "\n<table class='viewFieldList'>\n";
32: foreach ($fields as $label => $value) {
33: $b .= "\t" . (is_array($value) ? $this->viewField($value) : $this->viewField($label, $value)) . "\n";
34: }
35: $b .= "\n</table>\n";
36: return $b;
37: }
38:
39: 40: 41: 42: 43: 44: 45:
46: public function scaffoldViewFieldList($instance, $fields, $options = array()) {
47: if (empty($options['modelClass'])) {
48: $modelClass = $this->CakeLayers->getControllerDefaultModelClass();
49: } else {
50: $modelClass = $options['modelClass'];
51: }
52:
53: if (empty($options['associations'])) {
54: $associations = $this->CakeLayers->getModelAssociations($modelClass);
55: } else {
56: $associations = $options['associations'];
57: }
58:
59: if (ExtendedFieldsParser::isExtendedFieldsDefinition($fields)) {
60: return $this->_scaffoldExtendedFieldList(
61: $instance
62: , ExtendedFieldsAccessControl::parseFieldsets($fields, true)
63: , $associations
64: , $modelClass
65: );
66: } else {
67: return $this->_scaffoldCommonFieldList($instance, $fields, $associations, $modelClass);
68: }
69: }
70:
71: 72: 73: 74: 75: 76: 77: 78:
79: public function viewField($label, $value = null,
80: $valueType = ViewUtilHelper::VALUE_TYPE_UNKNOWN) {
81: $class = null;
82: if ($this->viewFieldCount++ % 2 == 0) {
83: $class = ' class="altrow"';
84: }
85:
86: $field = $this->_extractFieldData($label, $value, $valueType);
87:
88: switch ($field['type']) {
89: case ViewUtilHelper::VALUE_TYPE_BOOLEAN:
90: $value = $this->ViewUtil->yesNo($field['value']);
91: break;
92: case ViewUtilHelper::VALUE_TYPE_UNKNOWN:
93: default:
94: $value = $this->ViewUtil->autoFormat($field['value']);
95: }
96:
97: $buffer = "<tr{$class}>";
98: $buffer .= "\t\t<th>" . __d('extended_scaffold', Inflector::humanize($field['label']), true) . "</th>\n";
99: $buffer .= "\t\t<td>\n\t\t\t" . $value . "\n \t\t</td>\n";
100: $buffer .= "</tr>";
101:
102: return $buffer;
103: }
104:
105: private function _scaffoldCommonFieldList($instance, $fields, $associations,
106: $modelClass) {
107: $i = 0;
108: $viewFields = array();
109: foreach ($fields as $_field) {
110:
111: $isKey = false;
112:
113: if (!empty($associations['belongsTo'])) {
114: foreach ($associations['belongsTo'] as $_alias => $_details) {
115: if ($_field === $_details['foreignKey']) {
116: $isKey = true;
117: $viewFields[] = array(
118: 'label' => __d('extended_scaffold', Inflector::humanize($_alias), true),
119: 'value' => $this->AccessControl->linkOrText(
120: ModelTraverser::value($this->_getCurrentController()->{$modelClass}, $instance, "$_alias.{$_details['displayField']}")
121: , array(
122: 'controller' => $_details['controller']
123: , 'action' => 'view'
124: , ModelTraverser::value($this->_getCurrentController()->{$modelClass}, $instance, "$_alias.{$_details['primaryKey']}")
125: )
126: )
127: , 'fieldName' => $_field
128: );
129:
130: break;
131: }
132: }
133: }
134: if ($isKey !== true) {
135: $viewFields[] = array(
136: 'label' => __d('extended_scaffold', Inflector::humanize($_field), true),
137: 'value' => ModelTraverser::value($this->_getCurrentController()->{$modelClass}, $instance, $_field),
138: 'fieldName' => $_field,
139: );
140: }
141: }
142:
143: return $this->commonViewFieldList($viewFields);
144: }
145:
146: private function _scaffoldExtendedFieldList($instance, $fieldsets,
147: $associations, $modelClass) {
148: $b = '';
149:
150: foreach ($fieldsets as $fieldset) {
151: $fieldSetResult = $this->_scaffoldExtendedViewFieldListFieldset($fieldset, compact('instance', 'associations', 'modelClass'));
152: if ($fieldSetResult) {
153: $b .= $fieldSetResult;
154: }
155: }
156:
157: return $b;
158: }
159:
160: private function _scaffoldExtendedViewFieldListFieldset(\FieldSetDefinition $fieldset,
161: $scaffoldVars) {
162: return $fieldset->getListAssociation() ?
163: $this->ListFieldSet->fieldSet($fieldset, $scaffoldVars, $this->settings['listSettings']) :
164: $this->ExtendedFieldSet->fieldSet($fieldset, $scaffoldVars);
165: }
166:
167: private function _getCurrentController() {
168: return $this->CakeLayers->getController();
169: }
170:
171: private function ($label, $value, $type) {
172: $fieldName = null;
173: if (is_array($label)) {
174: $labelArray = $label;
175: foreach (array('label', 'fieldName', 'value', 'type') as $option) {
176: if (!empty($labelArray[$option])) {
177: ${$option} = $labelArray[$option];
178: }
179: }
180: }
181:
182: if (empty($type)) {
183: $type = ViewUtilHelper::VALUE_TYPE_UNKNOWN;
184: }
185:
186: if ($type == ViewUtilHelper::VALUE_TYPE_UNKNOWN && !empty($fieldName)) {
187: if (($fieldInfo = $this->_getFieldInfo($fieldName))) {
188: if ($fieldInfo['type'] == 'boolean') {
189: $type = ViewUtilHelper::VALUE_TYPE_BOOLEAN;
190: }
191: }
192: }
193:
194: return compact('label', 'fieldName', 'value', 'type');
195: }
196:
197: private function _getFieldInfo($fieldName) {
198: $schema = $this->_getCurrentController()->{$this->_getCurrentController()->modelClass}->schema();
199:
200: if (!empty($schema[$fieldName])) {
201: return $schema[$fieldName];
202: } else {
203: return false;
204: }
205: }
206:
207: }
208: