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: class FieldFormatter extends FieldFormatterBase {
42:
43: 44: 45:
46: public $owner;
47:
48: private $_validFieldTypes;
49: public function getValidFieldTypes () {
50: if (!isset ($this->_validFieldTypes)) {
51: $this->_validFieldTypes = array_keys (Fields::getFieldTypes ());
52: }
53: return $this->_validFieldTypes;
54: }
55:
56: public function renderAttribute(
57: $fieldName, $makeLinks = true, $textOnly = true, $encode = true) {
58:
59: $field = $this->owner->getField($fieldName);
60: if (!isset($field))
61: return null;
62:
63: if (!YII_UNIT_TESTING && Yii::app()->params->noSession) {
64: $webRequestAttributes = array(
65: 'rating',
66: 'assignment',
67: 'optionalAssignment',
68: 'url',
69: 'text',
70: );
71: if (in_array($field->type, $webRequestAttributes))
72: return $this->render($this->owner->$fieldName, $encode);
73: }
74:
75: $renderByNameFn = 'render'.ucfirst ($field->fieldName);
76: $renderByTypeFn = 'render'.ucfirst ($field->type);
77:
78:
79: if (method_exists ($this, $renderByNameFn) &&
80: !in_array ($field->fieldName, $this->getValidFieldTypes ())) {
81:
82: return $this->$renderByNameFn ($field, $makeLinks, $textOnly, $encode);
83: } elseif (method_exists ($this, $renderByTypeFn)) {
84: return $this->$renderByTypeFn ($field, $makeLinks, $textOnly, $encode);
85: } else {
86: return $this->render ($this->owner->$fieldName, $encode);
87: }
88: }
89:
90: 91: 92:
93: protected function renderUpdatedBy ($field, $makeLinks, $textOnly, $encode) {
94: $fieldName = $field->fieldName;
95: $val = $this->owner->$fieldName;
96: $val = empty ($val) ? $val : User::getUserLinks ($val, false, false);
97: return $encode ? CHtml::encode($val) : $val;
98: }
99:
100: protected function renderDate ($field, $makeLinks, $textOnly, $encode) {
101: $fieldName = $field->fieldName;
102: if (empty($this->owner->$fieldName))
103: return ' ';
104: elseif (is_numeric($this->owner->$fieldName))
105: return Formatter::formatLongDate($this->owner->$fieldName);
106: else
107: return $this->render ($this->owner->$fieldName, $encode);
108: }
109:
110: protected function renderDateTime ($field, $makeLinks, $textOnly, $encode) {
111: $fieldName = $field->fieldName;
112: if (empty($this->owner->$fieldName))
113: return ' ';
114: elseif (is_numeric($this->owner->$fieldName))
115: return Formatter::formatCompleteDate($this->owner->$fieldName);
116: else
117: return $this->render ($this->owner->$fieldName, $encode);
118: }
119:
120: protected function renderRating ($field, $makeLinks, $textOnly, $encode) {
121: $fieldName = $field->fieldName;
122: if ($textOnly) {
123: return $this->render ($this->owner->$fieldName, $encode);
124: } else {
125: return Yii::app()->controller->widget('CStarRating', array(
126: 'model' => $this->owner,
127: 'name' => str_replace(
128: ' ', '-', get_class($this->owner) . '-' . $this->owner->id . '-rating-' .
129: $field->fieldName),
130: 'attribute' => $field->fieldName,
131: 'readOnly' => true,
132:
133: 'allowEmpty' => !$field->required,
134: 'minRating' => 1,
135: 'maxRating' => 5,
136: 'starCount' => 5,
137: 'cssFile' => Yii::app()->theme->getBaseUrl() .
138: '/css/rating/jquery.rating.css',
139: ), true);
140: }
141: }
142:
143: protected function renderAssignment ($field, $makeLinks, $textOnly, $encode) {
144: $fieldName = $field->fieldName;
145: return User::getUserLinks($this->owner->$fieldName, $makeLinks);
146: }
147:
148: protected function renderOptionalAssignment ($field, $makeLinks, $textOnly, $encode) {
149: $fieldName = $field->fieldName;
150: if ($this->owner->$fieldName == '')
151: return '';
152: else
153: return User::getUserLinks($this->owner->$fieldName);
154: }
155:
156: protected function renderVisibility ($field, $makeLinks, $textOnly, $encode) {
157: $fieldName = $field->fieldName;
158: switch ($this->owner->$fieldName) {
159: case '1':
160: return Yii::t('app', 'Public');
161: case '0':
162: return Yii::t('app', 'Private');
163: case '2':
164: return Yii::t('app', 'User\'s Groups');
165: default:
166: return '';
167: }
168: }
169:
170: protected function renderEmail ($field, $makeLinks, $textOnly, $encode) {
171: $fieldName = $field->fieldName;
172: if (empty($this->owner->$fieldName)) {
173: return '';
174: } else {
175: $mailtoLabel = (isset($this->owner->name) && !is_numeric($this->owner->name)) ?
176: '"' . $this->owner->name . '" <' . $this->owner->$fieldName . '>' :
177: $this->owner->$fieldName;
178: return $makeLinks ?
179: CHtml::mailto(CHtml::encode($this->owner->$fieldName), $mailtoLabel) :
180: $this->render ($this->owner->$fieldName, $encode);
181: }
182: }
183:
184: protected function renderPhone ($field, $makeLinks, $textOnly, $encode) {
185: $fieldName = $field->fieldName;
186: $value = X2Model::getPhoneNumber(
187: $fieldName, get_class($this->owner), $this->owner->id, $encode, $makeLinks,
188: $this->owner->$fieldName);
189: return $value;
190: }
191:
192: protected function renderUrl ($field, $makeLinks, $textOnly, $encode) {
193: $fieldName = $field->fieldName;
194: if (!$makeLinks)
195: return CHtml::encode($this->owner->$fieldName);
196:
197: if (empty($this->owner->$fieldName)) {
198: $text = '';
199: } elseif (!empty($field->linkType)) {
200: switch ($field->linkType) {
201: case 'skype':
202: $text = '<a href="callto:' . $this->render ($this->owner->$fieldName, $encode) . '">' .
203: $this->render ($this->owner->$fieldName, $encode) . '</a>';
204: break;
205: case 'googleplus':
206: $text = '<a href="http://plus.google.com/' .
207: $this->render ($this->owner->$fieldName, $encode) . '">' . $this->render($this->owner->$fieldName, $encode) .
208: '</a>';
209: break;
210: case 'twitter':
211: $text = '<a href="http://www.twitter.com/#!/' .
212: $this->render ($this->owner->$fieldName, $encode) . '">' . $this->render($this->owner->$fieldName, $encode) .
213: '</a>';
214: break;
215: case 'linkedin':
216: $text = '<a href="http://www.linkedin.com/in/' .
217: $this->render ($this->owner->$fieldName, $encode) . '">' . $this->render($this->owner->$fieldName, $encode) .
218: '</a>';
219: break;
220: default:
221: $text = '<a href="http://www.' . $field->linkType . '.com/' .
222: $this->render ($this->owner->$fieldName, $encode) . '">' .
223: $this->render ($this->owner->$fieldName, $encode) .
224: '</a>';
225: }
226: } else {
227: $text = trim(preg_replace(
228: array(
229: '/<a([^>]*)target="?[^"\']+"?/i',
230: '/<a([^>]+)>/i',
231: ), array(
232: '<a\\1 target="_blank"',
233: '<a\\1 target="_blank">',
234: ), $this->render ($this->owner->$fieldName, $encode)
235: ));
236: $oldText = $text;
237: if (!function_exists('linkReplaceCallback')) {
238:
239: function linkReplaceCallback($matches) {
240: return stripslashes(
241: (strlen($matches[2]) > 0 ?
242: '<a href=\"' . $matches[2] . '\" target=\"_blank\">' .
243: $matches[0] . '</a>' :
244: $matches[0]));
245: }
246:
247: }
248:
249: $text = trim(preg_replace_callback(
250: array(
251: '/(?(?=<a[^>]*>.+<\/a>)(?:<a[^>]*>.+<\/a>)|([^="\']?)((?:https?|ftp|bf2|):\/\/[^<> \n\r]+))/ix',
252: ), 'linkReplaceCallback', $this->render ($this->owner->$fieldName, $encode)
253: ));
254: if ($text == trim($oldText)) {
255: if (!function_exists('linkReplaceCallback2')) {
256:
257: function linkReplaceCallback2($matches) {
258: return stripslashes(
259: (strlen($matches[2]) > 0 ?
260: '<a href=\"http://' . $matches[2] .
261: '\" target=\"_blank\">' . $matches[0] . '</a>' :
262: $matches[0]));
263: }
264:
265: }
266:
267: $text = trim(preg_replace_callback(
268: array(
269: '/(^|\s|>)(www.[^<> \n\r]+)/ix',
270: ), 'linkReplaceCallback2', $this->render ($this->owner->$fieldName, $encode)
271: ));
272: }
273: }
274: return $text;
275: }
276:
277: protected function renderLink ($field, $makeLinks, $textOnly, $encode) {
278: $fieldName = $field->fieldName;
279: $linkedModel = $this->owner->getLinkedModel($fieldName, false);
280: if ($linkedModel === null) {
281: return $this->render ($this->owner->$fieldName, $encode);
282: } else {
283: return $makeLinks ? $linkedModel->getLink() : $linkedModel->name;
284: }
285: }
286:
287: 288: 289:
290: protected function renderBoolean ($field, $makeLinks, $textOnly, $encode) {
291: $fieldName = $field->fieldName;
292: $bool = $this->owner->$fieldName;
293: if ($textOnly) {
294: $text = $bool ? Yii::t('app', 'Yes') : Yii::t('app', 'No');
295: return $this->render ($text, $encode);
296: }
297:
298: $html = $bool ?
299: X2Html::fa ('check', array('class'=> 'field-checked')) :
300: X2Html::fa ('times', array('class' => 'field-unchecked'));
301: return $html;
302: }
303:
304:
305:
306:
307:
308:
309:
310:
311:
312:
313:
314:
315:
316: protected function renderCurrency ($field, $makeLinks, $textOnly, $encode) {
317: $fieldName = $field->fieldName;
318: if ($this->owner instanceof Product) {
319: $currency = Yii::app()->locale->numberFormatter->formatCurrency(
320: $this->owner->$fieldName, $this->owner->currency);
321: } else {
322: $currency = empty($this->owner->$fieldName) ?
323: '' : Yii::app()->locale->numberFormatter->formatCurrency(
324: $this->owner->$fieldName, Yii::app()->params['currency']);
325: }
326: if ($encode)
327: return CHtml::encode($currency);
328: else
329: return $currency;
330: }
331:
332: protected function renderPercentage ($field, $makeLinks, $textOnly, $encode) {
333: $fieldName = $field->fieldName;
334: return $this->owner->$fieldName !== null && $this->owner->$fieldName !== '' ?
335: (string) ($this->render ($this->owner->$fieldName, $encode)) . "%" : null;
336: }
337:
338: protected function renderDropdown ($field, $makeLinks, $textOnly, $encode) {
339: $fieldName = $field->fieldName;
340: return $this->render($field->getDropdownValue ($this->owner->$fieldName), $encode);
341: }
342:
343: protected function renderParentCase ($field, $makeLinks, $textOnly, $encode) {
344: $fieldName = $field->fieldName;
345: return $this->render (
346: Yii::t(strtolower(Yii::app()->controller->id), $this->owner->$fieldName), $encode);
347: }
348:
349: protected function renderText ($field, $makeLinks, $textOnly, $encode) {
350: $fieldName = $field->fieldName;
351: return Yii::app()->controller->convertUrls(
352: $this->render ($this->owner->$fieldName, false));
353: }
354:
355: protected function renderCredentials ($field, $makeLinks, $textOnly, $encode) {
356: $fieldName = $field->fieldName;
357: $sysleg = Yii::t('app', 'System default (legacy)');
358: if ($this->owner->$fieldName == -1) {
359: return $sysleg;
360: } else {
361: $creds = Credentials::model()->findByPk($this->owner->$fieldName);
362: if (!empty($creds))
363: return $this->render ($creds->name, $encode);
364: else
365: return $sysleg;
366: }
367: }
368:
369: protected function renderTimerSum ($field, $makeLinks, $textOnly, $encode) {
370: $fieldName = $field->fieldName;
371: $t_seconds = $this->owner->$fieldName;
372: $t[] = floor($t_seconds / 3600);
373: $t[] = floor($t_seconds / 60) % 60;
374: $t[] = $t_seconds % 60;
375: $pad = function($t) {
376: return str_pad((string) $t, 2, '0', STR_PAD_LEFT);
377: };
378: return implode(':', array_map($pad, $t));
379: }
380:
381: protected function renderInt ($field, $makeLinks, $textOnly, $encode) {
382: $fieldName = $field->fieldName;
383: if ($fieldName != 'id')
384: return Yii::app()->locale->numberFormatter->formatDecimal($this->owner->$fieldName);
385: else
386: return $this->owner->$fieldName;
387: }
388:
389: protected function renderFloat ($field, $makeLinks, $textOnly, $encode) {
390: return $this->renderInt ($field, $makeLinks, $textOnly, $encode);
391: }
392:
393: protected function renderCustom ($field, $makeLinks, $textOnly, $encode) {
394: $fieldName = $field->fieldName;
395: if ($field->linkType == 'display') {
396:
397:
398: $fieldText = preg_replace(
399: '/%7B([\w\.]+)%7D/', '{$1}', $field->data);
400: return Formatter::replaceVariables($fieldText, $this->owner, '', false);
401: } elseif ($field->linkType == 'formula') {
402: $evald = Formatter::parseFormula($field->data, array(
403: 'model' => $this->owner,
404: ));
405: if ($evald[0]) {
406: return $this->render ($evald[1], $encode);
407: } else {
408: return Yii::t('app', 'Error parsing formula.') . ' ' . $evald[1];
409: }
410: } else {
411: return $this->render ($this->owner->$fieldName, $encode);
412: }
413: }
414: }
415:
416: ?>
417: