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: * Base widget class for all of X2Engine's widgets
39: *
40: * @property X2WebModule $module
41: * @package application.components
42: */
43: abstract class X2Widget extends CWidget {
44:
45: protected $_module;
46:
47: /**
48: * Constructor.
49: * @param CBaseController $owner owner/creator of this widget. It could be either a widget or a
50: * controller.
51: */
52: public function __construct ($owner=null) {
53: $this->attachBehaviors ($this->behaviors ());
54: $this->initNamespace ();
55: parent::__construct ($owner);
56: }
57:
58: public function behaviors () {
59: return array (
60: 'X2WidgetBehavior' => array (
61: 'class' => 'application.components.behaviors.X2WidgetBehavior'
62: ),
63: );
64: }
65:
66: /**
67: * Renders a view file.
68: * Overrides {@link CBaseController::renderFile} to check if the requested view
69: * has a version in /custom, and uses that if it exists.
70: *
71: * @param string $viewFile view file path
72: * @param array $data data to be extracted and made available to the view
73: * @param boolean $return whether the rendering result should be returned instead of being
74: * echoed
75: * @return string the rendering result. Null if the rendering result is not required.
76: * @throws CException if the view file does not exist
77: */
78: public function renderFile($viewFile,$data=null,$return=false) {
79: $viewFile = Yii::getCustomPath($viewFile);
80: return parent::renderFile($viewFile,$data,$return);
81: }
82:
83: /**
84: * Runs an arbitrary function inside a partial view. All scripts registered get processed.
85: * Allows scripts associated with a widget to be returned in AJAX response.
86: *
87: * @param function $function
88: */
89: public static function ajaxRender ($function, $return=false) {
90: return Yii::app()->controller->renderPartial (
91: 'application.components.views._ajaxWidgetContents',
92: array (
93: 'run' => $function
94: ), $return, true);
95: }
96:
97: /**
98: * Getter for {@link module}.
99: *
100: * Can automatically recognize when a component is a member of a module's
101: * collection of components.
102: * @return type
103: */
104: public function getModule(){
105: if(!isset($this->_module)){
106: // Ascertain the module to which the widget belongs by virtue of its
107: // location in the file system:
108: $rc = new ReflectionClass(get_class($this));
109: $path = $rc->getFileName();
110: $ds = preg_quote(DIRECTORY_SEPARATOR,'/');
111: $pathPattern = array(
112: 'protected',
113: 'modules',
114: '(?P<module>[a-z0-9]+)',
115: 'components',
116: '\w+\.php'
117: );
118: if(preg_match('/'.implode($ds,$pathPattern).'$/',$path,$match)) {
119: // The widget is part of a module:
120: $this->_module = Yii::app()->getModule($match['module']);
121: } else {
122: // Assume the widget's module is the currently-requested module:
123: $this->_module = Yii::app()->controller->module;
124: }
125: }
126: return $this->_module;
127: }
128:
129: public function setModule ($moduleName) {
130: $this->_module = Yii::app()->getModule($moduleName);
131: }
132:
133: public function init () {
134: if ($this->instantiateJSClassOnInit) {
135: $this->registerPackages ();
136: $this->instantiateJSClass ();
137: }
138: return parent::init ();
139: }
140:
141: }
142: ?>
143: