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 contact@x2engine.com.
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: abstract class ActionFormModelBase extends X2FormModel {
38: public $associationType;
39: public $associationId;
40: public $associationName;
41: public $assignedTo;
42: public $dueDate;
43: public $completeDate;
44: public $timerIds;
45: public $skipActionTimers = true;
46:
47: public function rules () {
48: return array (
49: array (
50: 'associationId', 'validateAssociationId',
51: ),
52: array (
53: 'associationName', 'validateAssociationName',
54: ),
55: array (
56: 'assignedTo', 'validateAssignedTo',
57: ),
58: array (
59: 'associationType, assignedTo', 'required',
60: ),
61: array (
62: 'timerIds', 'safe',
63: ),
64: );
65: }
66:
67: /**
68: * Set dynamic default date values
69: */
70: public function validateAssignedTo ($attribute) {
71: $value = $this->$attribute;
72: if (empty ($value)) $this->$attribute = Yii::app()->user->getName ();
73: }
74:
75: /**
76: * Set dynamic default date values
77: */
78: public function validateDate ($attribute) {
79: $value = $this->$attribute;
80: if (empty ($value)) $this->$attribute = time ();
81: }
82:
83: /**
84: * Check for negative time ranges
85: */
86: public function validateCompleteDate ($attribute) {
87: $value = $this->$attribute;
88: if ($this->dueDate > $this->completeDate)
89: // User specified a negative time range! Let's say that the
90: // starting time is equal to when it ended (which is earlier)
91: $this->dueDate = $this->completeDate;
92: }
93:
94: /**
95: * if association name is sent without id, try to lookup the record by name and type
96: */
97: public function validateAssociationId ($attribute) {
98: $value = $this->$attribute;
99: if (is_string ($this->associationName) && $this->associationName !== '') {
100: $associatedModel = X2Model::getModelOfTypeWithName (
101: $this->associationType, $this->associationName);
102: if ($associatedModel) {
103: $this->associationId = $associatedModel->id;
104: } else {
105: $this->addError ('associationName', Yii::t('actions', 'Invalid association name'));
106: }
107: }
108: if (!isset ($this->associationId)) return false;
109: }
110:
111: /**
112: * Synchronize association name with association type and id
113: */
114: public function validateAssociationName ($attribute) {
115: $association = X2Model::getAssociationModel($this->associationType, $this->associationId);
116: if ($association) {
117:
118: $this->associationName = $association->name;
119: }
120: }
121:
122: public function attributeLabels () {
123: return array (
124: 'assignedTo' => Yii::t('actions', 'Assigned To'),
125: 'dueDate' => Yii::t('actions', 'Due Date'),
126: 'priority' => Yii::t('actions', 'Priority'),
127: 'visibility' => Yii::t('actions', 'Visibility'),
128: 'reminder' => Yii::t('actions', 'Reminder'),
129: );
130: }
131:
132: private $_action;
133: public function getAction ($new=false) {
134: if (!isset ($this->_action) || $new) {
135: if(!Yii::app()->user->isGuest){
136: $model = new Actions;
137: }else{
138: $model = new Actions('guestCreate');
139: }
140: if (!empty ($this->type)) $model->disableBehavior ('changelog');
141: $this->_action = $model;
142: }
143: return $this->_action;
144: }
145:
146: public function validate ($attributes=null, $clearErrors=true) {
147: $valid = parent::validate ();
148: $attributes = $this->getAttributes ();
149: $this->action->setX2Fields ($attributes);
150: $this->action->type = $this->type;
151: $valid &= $this->action->validate ();
152: // synchronize errors
153: $this->addErrors ($this->action->getErrors ());
154: $this->action->addErrors ($this->getErrors ());
155: return $valid;
156: }
157:
158: public function mergeErrors (Actions $model) {
159: $model->addErrors ($this->getErrors ());
160: }
161: }
162:
163: ?>
164: