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: /**
38: * Widget for record conversion links which leverage X2ModelConversionBehavior.
39: * Designed to have multiple instances on the same page (which is useful if links for multiple
40: * conversion targets are needed).
41: */
42:
43: class X2ModelConversionWidget extends X2Widget {
44:
45: /**
46: * @var X2Model $model
47: */
48: public $model;
49:
50: /**
51: * @var string $targetClass
52: */
53: public $targetClass;
54:
55: /**
56: * @var string $element
57: */
58: public $element = '#model-conversion-widget';
59:
60: public $buttonSelector;
61:
62: /**
63: * @var string $JSClass
64: */
65: public $JSClass = 'X2ModelConversionWidget';
66:
67: protected $_JSClassParams;
68: public function getJSClassParams () {
69: if (!isset ($this->_JSClassParams)) {
70: $title = X2Model::getModelTitle (get_class ($this->model), true);
71: $targetClass = $this->targetClass;
72: $behavior = $this->model->asa ('X2ModelConversionBehavior');
73: $conversionFailed =
74: $behavior->conversionFailed &&
75: $behavior->errorModel !== null && get_class ($behavior->errorModel) ===
76: $this->targetClass;
77: $this->_JSClassParams = array_merge (parent::getJSClassParams (), array (
78: 'buttonSelector' => $this->buttonSelector,
79: 'translations' => array (
80: 'conversionError' => Yii::t('app', '{model} conversion failed.', array (
81: '{model}' => $title,
82: )),
83: 'conversionWarning' => Yii::t('app', '{model} Conversion Warning', array (
84: '{model}' => $title,
85: )),
86: 'convertAnyway' => Yii::t('app', 'Convert Anyway'),
87: 'Cancel' => Yii::t('app', 'Cancel'),
88: ),
89: 'targetClass' => $this->targetClass,
90: 'modelId' => $this->model->id,
91: 'conversionFailed' => $conversionFailed,
92: 'conversionIncompatibilityWarnings' =>
93: $this->model->getConversionIncompatibilityWarnings ($this->targetClass),
94: 'errorSummary' =>
95: $conversionFailed ?
96: "<div class='form'>".
97: CHtml::errorSummary (
98: $this->model->asa ('X2ModelConversionBehavior')->errorModel,
99: Yii::t('app', '{model} conversion failed.',
100: array (
101: '{model}' => $title,
102: ))).
103: "</div>" : '',
104:
105: ));
106: }
107: return $this->_JSClassParams;
108: }
109:
110: public function getPackages () {
111: if (!isset ($this->_packages)) {
112: $this->_packages = array_merge (parent::getPackages (), array (
113: 'X2ModelConversionWidgetJS' => array(
114: 'baseUrl' => Yii::app()->request->baseUrl,
115: 'js' => array(
116: 'js/X2ModelConversionWidget.js',
117: ),
118: 'depends' => array (
119: 'X2Widget',
120: ),
121: ),
122: ));
123: }
124: return $this->_packages;
125: }
126:
127: public function run () {
128: $this->registerPackages ();
129: $this->instantiateJSClass ();
130: $this->render ('application.components.views._x2ModelConversionWidget', array (
131: 'sourceModelClass' => get_class ($this->model),
132: 'targetModelClass' => $this->targetClass,
133: 'convertMultiple' => false,
134: ));
135: }
136:
137: }
138:
139: ?>
140: