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: 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: 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: 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: