1: <?php
2: /*****************************************************************************************
3: * X2Engine Open Source Edition is a customer relationship management program developed by
4: * X2Engine, Inc. Copyright (C) 2011-2016 X2Engine Inc.
5: *
6: * This program is free software; you can redistribute it and/or modify it under
7: * the terms of the GNU Affero General Public License version 3 as published by the
8: * Free Software Foundation with the addition of the following permission added
9: * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
10: * IN WHICH THE COPYRIGHT IS OWNED BY X2ENGINE, X2ENGINE DISCLAIMS THE WARRANTY
11: * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
12: *
13: * This program is distributed in the hope that it will be useful, but WITHOUT
14: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15: * FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
16: * details.
17: *
18: * You should have received a copy of the GNU Affero General Public License along with
19: * this program; if not, see http://www.gnu.org/licenses or write to the Free
20: * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21: * 02110-1301 USA.
22: *
23: * You can contact X2Engine, Inc. P.O. Box 66752, Scotts Valley,
24: * California 95067, USA. or at email address [email protected].
25: *
26: * The interactive user interfaces in modified source and object code versions
27: * of this program must display Appropriate Legal Notices, as required under
28: * Section 5 of the GNU Affero General Public License version 3.
29: *
30: * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
31: * these Appropriate Legal Notices must retain the display of the "Powered by
32: * X2Engine" logo. If the display of the logo is not reasonably feasible for
33: * technical reasons, the Appropriate Legal Notices must display the words
34: * "Powered by X2Engine".
35: *****************************************************************************************/
36:
37: class X2ActiveRecord extends CActiveRecord {
38:
39: protected $fieldFormatterClass = 'X2ActiveRecordFieldFormatter';
40: protected $fieldInputRendererClass;
41:
42: private $_formatter;
43: private $_inputRenderer;
44:
45: public function rules () {
46: $rules = $this->getBehaviorRules ();
47: return $rules;
48: }
49:
50: /**
51: * @return array All error messages for this model as an array of strings
52: */
53: public function getAllErrorMessages() {
54: $errors = $this->getErrors();
55: $errorMessages = array();
56: foreach ($errors as $attrErrors) {
57: foreach ($attrErrors as $errorMessage) {
58: if ($errorMessage != '') {
59: $errorMessages[] = $errorMessage;
60: }
61: }
62: }
63: return $errorMessages;
64: }
65:
66: public function setFormatter ($class) {
67: if (is_string ($class)) {
68: $this->_formatter = Yii::createComponent (array (
69: 'class' => $class,
70: 'owner' => $this,
71: ));
72: } else if ($class instanceof FieldFormatter) {
73: $this->_formatter = $class;
74: } else {
75: throw new CException ('Invalid formatter object');
76: }
77: }
78:
79: public function getFormatter () {
80: if (!isset ($this->_formatter)) {
81: $this->_formatter = Yii::createComponent (array (
82: 'class' => $this->fieldFormatterClass,
83: 'owner' => $this,
84: ));
85: }
86: return $this->_formatter;
87: }
88:
89: public function setInputRenderer ($class) {
90: if (is_string ($class)) {
91: $this->_inputRenderer = Yii::createComponent (array (
92: 'class' => $class,
93: 'owner' => $this,
94: ));
95: } else if ($class instanceof FieldInputRenderer) {
96: $this->_inputRenderer = $class;
97: } else {
98: throw new CException ('Invalid input renderer object');
99: }
100: }
101:
102: public function getInputRenderer () {
103: if (!isset ($this->_inputRenderer) && $this->fieldInputRendererClass) {
104: $this->_inputRenderer = Yii::createComponent (array (
105: 'class' => $this->fieldInputRendererClass,
106: 'owner' => $this,
107: ));
108: }
109: return $this->_inputRenderer;
110: }
111:
112: public function getName () {
113: if (property_exists ($this, 'name') || $this->hasAttribute ('name')) return $this->name;
114: else return $this->id;
115: }
116:
117: /**
118: * Renders an attribute of the model based on its field type
119: * @param string $fieldName the name of the attribute to be rendered
120: * @param boolean $makeLinks whether to create HTML links for certain field types
121: * @param boolean $textOnly whether to generate HTML or plain text
122: * @return string the HTML or text for the formatted attribute
123: */
124: public function renderAttribute(
125: $fieldName, $makeLinks = true, $textOnly = true, $encode = true) {
126:
127: $formatter = $this->getFormatter ();
128: return $formatter->renderAttribute ($fieldName, $makeLinks, $textOnly, $encode);
129: }
130:
131: protected function getBehaviorRules () {
132: $rules = array ();
133: foreach ($this->behaviors () as $name => $config) {
134: if ($this->asa ($name) && $this->asa ($name)->getEnabled () &&
135: $this->asa ($name) instanceof X2ActiveRecordBehavior) {
136:
137: $rules = array_merge ($this->asa ($name)->rules (), $rules);
138: }
139: }
140: return $rules;
141: }
142:
143: }
144:
145: ?>
146: