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: 38: 39: 40: 41: 42: 43: 44:
45: class InlineRelationshipsWidget extends GridViewWidget {
46:
47:
48:
49: public $viewFile = '_inlineRelationshipsWidget';
50:
51: public $model;
52:
53: public $template = '<div class="submenu-title-bar widget-title-bar">{widgetLabel}{titleBarButtons}{closeButton}{minimizeButton}{settingsMenu}</div>{widgetContents}';
54:
55: 56: 57: 58:
59: public $defaultsByRelatedModelType = array ();
60:
61: protected $compactResultsPerPage = true;
62:
63: private $_relatedModels;
64:
65: private static $_JSONPropertiesStructure;
66:
67: public static function getJSONPropertiesStructure () {
68: if (!isset (self::$_JSONPropertiesStructure)) {
69: self::$_JSONPropertiesStructure = array_merge (
70: parent::getJSONPropertiesStructure (),
71: array (
72: 'label' => 'Relationships',
73: 'hidden' => false,
74: 'resultsPerPage' => 10,
75: 'showHeader' => false,
76: 'displayMode' => 'grid',
77: 'height' => '200',
78: 'hideFullHeader' => true,
79: )
80: );
81: }
82: return self::$_JSONPropertiesStructure;
83: }
84:
85: private $_filterModel;
86: public function getFilterModel () {
87: if (!isset ($this->_filterModel)) {
88: $model = $this->model;
89: $filterModel = new RelationshipsGridModel ('search');
90: $filterModel->myModel = $model;
91: $filterModel->init ();
92: $this->_filterModel = $filterModel;
93: }
94: return $this->_filterModel;
95: }
96:
97: public function getDataProvider () {
98: $model = $this->model;
99: $filterModel = $this->getFilterModel ();
100:
101:
102: $gridModels = array ();
103: foreach ($model->visibleRelatedX2Models as $relatedModel) {
104: $gridModel = Yii::createComponent (array (
105: 'class' => 'RelationshipsGridModel',
106: 'relatedModel' => $relatedModel,
107: 'myModel' => $model,
108: ));
109: $gridModel->init ();
110: $gridModels[] = $gridModel;
111: }
112:
113:
114: $gridModels = $filterModel->filterModels ($gridModels);
115:
116:
117: $sort = Yii::createComponent (array (
118: 'class' => 'SmartSort',
119: 'uniqueId' => $this->widgetKey,
120: 'sortVar' => $this->widgetKey.'_sort',
121: 'attributes'=>array('name','relatedModelName','label','createDate','assignedTo'),
122: 'defaultOrder' => 'id desc',
123: 'alwaysApplyDefaultOrder' => true
124: ));
125:
126: $relationshipsDataProvider = new CArrayDataProvider($gridModels, array(
127: 'id' => 'relationships-gridview',
128: 'sort' => $sort,
129: 'pagination' => array('pageSize'=>$this->getWidgetProperty ('resultsPerPage'))
130: ));
131: return $relationshipsDataProvider;
132: }
133:
134: public function renderTitleBarButtons () {
135: echo '<div class="x2-button-group">';
136: if ($this->checkModuleUpdatePermissions ()) {
137: echo
138: "<a class='x2-button rel-title-bar-button' id='new-relationship-button'
139: title='".CHtml::encode (Yii::t('app', 'Create a new relationship'))."'>".
140: X2Html::fa ('fa-plus', array (), ' ', 'span').
141: "</a>";
142: }
143:
144: echo '</div>';
145: }
146:
147: public function renderWidgetLabel () {
148: $label = $this->getWidgetLabel ();
149: $relationshipCount = count ($this->model->getVisibleRelatedX2Models ());
150: echo "<div class='widget-title'>".
151: htmlspecialchars($label).
152: " (<span id='relationship-count'>$relationshipCount</span>)</div>";
153: }
154:
155: public function getSetupScript () {
156: if (!isset ($this->_setupScript)) {
157: $widgetClass = get_called_class ();
158: if (isset ($_GET['ajax'])) {
159: $this->_setupScript = "";
160: } else {
161: $modelsWhichSupportQuickCreate =
162: QuickCreateRelationshipBehavior::getModelsWhichSupportQuickCreate ();
163:
164:
165: $createUrls = QuickCreateRelationshipBehavior::getCreateUrlsForModels (
166: $modelsWhichSupportQuickCreate);
167:
168:
169: $tooltips = QuickCreateRelationshipBehavior::getDialogTooltipsForModels (
170: $modelsWhichSupportQuickCreate, get_class ($this->model));
171:
172:
173: $dialogTitles = QuickCreateRelationshipBehavior::getDialogTitlesForModels (
174: $modelsWhichSupportQuickCreate);
175: $this->_setupScript = "
176: $(function () {
177: x2.inlineRelationshipsWidget = new x2.InlineRelationshipsWidget (".
178: CJSON::encode (array_merge ($this->getJSSortableWidgetParams (), array (
179: 'displayMode' => $this->getWidgetProperty ('displayMode'),
180: 'widgetClass' => $widgetClass,
181: 'setPropertyUrl' => Yii::app()->controller->createUrl (
182: '/profile/setWidgetSetting'),
183: 'cssSelectorPrefix' => $this->widgetType,
184: 'widgetType' => $this->widgetType,
185: 'widgetUID' => $this->widgetUID,
186: 'enableResizing' => true,
187: 'height' => $this->getWidgetProperty ('height'),
188: 'recordId' => $this->model->id,
189: 'recordType' => get_class ($this->model),
190: 'defaultsByRelatedModelType' =>
191: $this->defaultsByRelatedModelType,
192: 'createUrls' => $createUrls,
193: 'dialogTitles' => $dialogTitles,
194: 'tooltips' => $tooltips,
195: 'modelsWhichSupportQuickCreate' =>
196: array_values ($modelsWhichSupportQuickCreate),
197: 'ajaxGetModelAutocompleteUrl' =>
198: Yii::app()->controller->createUrl ('ajaxGetModelAutocomplete'),
199: 'createRelationshipUrl' =>
200: Yii::app()->controller->createUrl (
201: '/relationships/addRelationship'),
202: 'hasUpdatePermissions' => $this->checkModuleUpdatePermissions (),
203: )))."
204: );
205: });
206: ";
207: }
208: }
209: return $this->_setupScript;
210: }
211:
212: 213: 214:
215: public function getPackages () {
216: if (!isset ($this->_packages)) {
217: $this->_packages = array_merge (
218: parent::getPackages (),
219: array (
220: 'InlineRelationshipsJSExt' => array(
221: 'baseUrl' => Yii::app()->getTheme ()->getBaseUrl ().'/css/gridview/',
222: 'js' => array (
223: 'jquery.yiigridview.js',
224: ),
225: 'depends' => array ('auxlib')
226: ),
227: 'InlineRelationshipsJS' => array(
228: 'baseUrl' => Yii::app()->request->baseUrl,
229: 'js' => array (
230: 'js/sortableWidgets/InlineRelationshipsWidget.js',
231: ),
232: 'depends' => array ('SortableWidgetJS')
233: ),
234: )
235: );
236: }
237: return $this->_packages;
238: }
239:
240: public function getViewFileParams () {
241: if (!isset ($this->_viewFileParams)) {
242: $linkableModels = Relationships::getRelationshipTypeOptions ();
243: asort ($linkableModels);
244:
245:
246:
247: $linkableModelsOptions = $linkableModels;
248:
249: $hasUpdatePermissions = $this->checkModuleUpdatePermissions ();
250:
251: $this->_viewFileParams = array_merge (
252: parent::getViewFileParams (),
253: array (
254: 'model' => $this->model,
255: 'modelName' => get_class ($this->model),
256: 'linkableModelsOptions' => $linkableModelsOptions,
257: 'hasUpdatePermissions' => $hasUpdatePermissions,
258:
259: 'height' => $this->getWidgetProperty ('height'),
260: )
261: );
262: }
263: return $this->_viewFileParams;
264: }
265:
266: protected function () {
267: return
268: '<li class="expand-detail-views">'.
269: X2Html::fa('fa-toggle-down').
270: Yii::t('profile', 'Toggle Detail Views').
271: '</li>'.
272: parent::getSettingsMenuContentEntries ();
273: }
274:
275:
276: private $_moduleUpdatePermissions;
277: private function checkModuleUpdatePermissions () {
278: if (!isset ($this->_moduleUpdatePermissions)) {
279: $this->_moduleUpdatePermissions =
280: Yii::app()->controller->checkPermissions ($this->model, 'edit');
281: }
282: return $this->_moduleUpdatePermissions;
283: }
284:
285: public function init ($skipGridViewInit=false) {
286: return parent::init (true);
287: }
288: }
289:
290: ?>
291: