1: <?php
2:
3: /*****************************************************************************************
4: * X2Engine Open Source Edition is a customer relationship management program developed by
5: * X2Engine, Inc. Copyright (C) 2011-2016 X2Engine Inc.
6: *
7: * This program is free software; you can redistribute it and/or modify it under
8: * the terms of the GNU Affero General Public License version 3 as published by the
9: * Free Software Foundation with the addition of the following permission added
10: * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
11: * IN WHICH THE COPYRIGHT IS OWNED BY X2ENGINE, X2ENGINE DISCLAIMS THE WARRANTY
12: * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
13: *
14: * This program is distributed in the hope that it will be useful, but WITHOUT
15: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
16: * FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
17: * details.
18: *
19: * You should have received a copy of the GNU Affero General Public License along with
20: * this program; if not, see http://www.gnu.org/licenses or write to the Free
21: * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
22: * 02110-1301 USA.
23: *
24: * You can contact X2Engine, Inc. P.O. Box 66752, Scotts Valley,
25: * California 95067, USA. or at email address contact@x2engine.com.
26: *
27: * The interactive user interfaces in modified source and object code versions
28: * of this program must display Appropriate Legal Notices, as required under
29: * Section 5 of the GNU Affero General Public License version 3.
30: *
31: * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
32: * these Appropriate Legal Notices must retain the display of the "Powered by
33: * X2Engine" logo. If the display of the logo is not reasonably feasible for
34: * technical reasons, the Appropriate Legal Notices must display the words
35: * "Powered by X2Engine".
36: *****************************************************************************************/
37:
38: /**
39: * A model class for dealing with "embedded" models, whose attributes are stored
40: * in a database column as a JSON-encoded string, when using
41: * {@link JSONEmbeddedModelFieldsBehavior}.
42: *
43: * The methods for form inputs and rendering detail should be included in the
44: * models themselves, to keep each model self-contained.
45: *
46: * @package application.models.embedded
47: * @author Demitri Morgan <demitri@x2engine.com>
48: */
49: abstract class JSONEmbeddedModel extends CModel {
50:
51: /**
52: * Whether or not admin rights are required to access this model
53: * @var bool
54: */
55: public $requiresAdmin = false;
56:
57: /**
58: * Stores derived value returned by {@link attributeNames()}
59: * @var type
60: */
61: protected $_attributeNames;
62:
63: /**
64: * Name of the attribute in the containing model that contains this model
65: * @var type
66: */
67: public $exoAttr;
68:
69: /**
70: * Form field name prefix
71: * @var type
72: */
73: public $exoFormName;
74:
75: /**
76: * The model to which this embedded model belongs
77: * @var CActiveRecord
78: */
79: public $exoModel;
80:
81: public static function getProtectedFieldPlaceholder () {
82: return Yii::t('app', 'Protected field value');
83: }
84:
85: public function getProtectedFields () {
86: return array ();
87: }
88:
89: /**
90: * Overridden to skip placeholder values
91: */
92: public function setAttributes ($values, $safeOnly=true) {
93: if(!is_array($values))
94: return;
95: $protectedFields = array_flip ($this->getProtectedFields ());
96:
97: foreach ($values as $fieldName => $value) {
98: if (isset ($protectedFields[$fieldName]) &&
99: $value === $this->getProtectedFieldPlaceholder ()) {
100:
101: unset ($values[$fieldName]);
102: }
103: }
104: return parent::setAttributes ($values, $safeOnly);
105: }
106:
107: public function renderProtectedInput ($field) {
108: $htmlOptions = array ();
109: if ($this->$field) {
110: $htmlOptions['class'] = 'x2-protected-field';
111: $this->$field = self::getProtectedFieldPlaceholder ();
112: }
113: Yii::app()->clientScript->registerScript('renderProtectedInput',"
114: ");
115: echo CHtml::activeTextField($this, $field, $this->htmlOptions($field, $htmlOptions));
116: }
117:
118: public function renderInput ($field) {
119: $protectedFields = array_flip ($this->getProtectedFields ());
120: if (isset ($protectedFields[$field])) {
121: echo $this->renderProtectedInput ($field);
122: } else {
123: echo CHtml::activeTextField($this, $field, $this->htmlOptions($field));
124: }
125: }
126:
127: public function renderForm () {
128: echo '<br />';
129: echo '<br />';
130: echo CHtml::tag ('h3', array (), $this->exoModel->getAttributeLabel ($this->exoAttr));
131: echo '<hr />';
132: $this->renderInputs();
133: echo '<br />';
134: echo '<br />';
135: }
136:
137: /**
138: * Values of properties of parent model to set when embedded model is created
139: */
140: public function getMetaData () {
141: return array ();
142: }
143:
144: public function attributeNames() {
145: if(!isset($this->_attributeNames)) {
146: $this->_attributeNames = array_keys($this->attributeLabels());
147: }
148: return $this->_attributeNames;
149: }
150:
151: /**
152: * Child classes implementing this should override this function to generate
153: * a detail view. The resulting markup should be echoed out, not returned.
154: */
155: public function detailView() {
156:
157: }
158:
159: /**
160: * A UI-friendly name that the model should be called.
161: */
162: public function modelLabel() {}
163:
164: /**
165: * Child classes implementing this should generate all necessary input form
166: * elements for modifying fields of the embedded model. The resulting
167: * markup should be echoed out, not returned.
168: */
169: public function renderInputs() {}
170:
171: /**
172: * Generate form input name for an attribute so that the urlencoded post data
173: * comes in a form that can be properly interpreted by setAttributes in the
174: * container model
175: * {@link JSONEmbeddedModelFieldsBehavior}
176: * @param string $attribute
177: */
178: public function resolveName($attribute) {
179: if(!isset($this->exoFormName))
180: $this->exoFormName = CHtml::resolveName($this->exoModel,$this->exoAttr);
181: return $this->exoFormName.strtr(CHtml::resolveName($this,$attribute),array(get_class($this)=>''));
182: }
183:
184: /**
185: * Generate a list of options to send to methods within {@link CHtml} that
186: * take HTML element options/properties, so that it includes the proper name
187: * of the input.
188: * @param type $options
189: */
190: public function htmlOptions($name,$options=array()) {
191: return X2Html::mergeHtmlOptions ($options,array('name'=>$this->resolveName($name)));
192: }
193:
194: }
195:
196: ?>
197: