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: class CreatePageFormModel extends X2FormModel {
38:
39: public $topLinkUrl;
40: public $topLinkText;
41: public $recordType;
42: public $recordId;
43: public $recordName;
44: public $openInNewTab = false;
45: public $dummyAttribute;
46: public $record;
47:
48: private $_validTypes;
49: public function getValidTypes () {
50: if (!isset ($this->_validTypes)) {
51: $this->_validTypes = X2Model::getModelTypes (true, function ($elem) {
52: return X2Model::model ($elem)->asa ('X2LinkableBehavior');
53: });
54: }
55: return $this->_validTypes;
56: }
57:
58: public function rules () {
59: return array (
60: array (
61: 'topLinkUrl', 'application.components.validators.X2UrlValidator',
62: 'allowEmpty' => true, 'defaultScheme' => 'http',
63: 'message' => Yii::t('app', 'Invalid URL')
64: ),
65: array (
66: 'recordId', 'numerical', 'allowEmpty' => true, 'integerOnly' => true,
67: ),
68: array (
69: 'recordType', 'validateRecordType',
70: ),
71: array (
72: 'openInNewTab', 'boolean',
73: ),
74: array (
75: 'topLinkUrl', 'requireOneExclusive',
76: 'and' => 'topLinkText', 'xor' => 'recordId,recordType'
77: ),
78: array (
79: 'topLinkText,topLinkUrl', 'length', 'max' => 250,
80: ),
81: array (
82: 'recordName', 'safe',
83: ),
84: );
85: }
86:
87: public function attributeLabels () {
88: return array (
89: 'topLinkUrl' => Yii::t('app', 'Link URL'),
90: 'topLinkText' => Yii::t('app', 'Link name'),
91: 'recordName' => Yii::t('app', 'Select a record:'),
92: 'recordType' => Yii::t('app', 'Record type'),
93: 'recordName' => Yii::t('app', 'Record name'),
94: 'openInNewTab' => Yii::t('app', 'Open link in new tab when clicked?'),
95: );
96: }
97:
98: public function getSelection () {
99: if (!isset ($this->recordType) && !isset ($this->recordId)) {
100: return 'topLinkUrl';
101: } else {
102: return 'recordName';
103: }
104: }
105:
106: /**
107: * Ensure that only (link href and link text) xor (record type and id) is specified
108: */
109: public function requireOneExclusive ($attr, $params) {
110: $xor = explode (',', $params['xor']);
111: $and = explode (',', $params['and']);
112: $that = $this;
113: if (!(!empty ($this->$attr) &&
114: (array_reduce ($and, function ($carry, $item) use ($that) {
115: return $carry && !empty ($that->$item);
116: }, true))) ^
117: (array_reduce ($xor, function ($carry, $item) use ($that) {
118: return $carry && !empty ($that->$item);
119: }, true))) {
120:
121: $this->addError ('dummyAttribute', '');
122: Yii::app()->user->setFlash (
123: 'error', Yii::t('app', 'Please specify a URL or select a record.'));
124: }
125: }
126:
127: /**
128: * Ensure that specified record exists
129: */
130: public function validateRecordType ($attr) {
131: if (!isset ($this->$attr) && !isset ($this->recordId)) return;
132: $val = $this->$attr;
133: if (!in_array ($val, array_keys ($this->getValidTypes ()))) {
134: Yii::app()->controller->badRequest ();
135: }
136: if (!isset ($this->recordId) ||
137: !($record = X2Model::model ($val)->findByPk ($this->recordId))) {
138:
139: $this->addError ('recordName', Yii::t('app', 'Record could not be found') );
140: } else {
141: $this->record = $record;
142: }
143: }
144:
145: }
146:
147: ?>
148: