forked from ifdattic/EUpdateDialog
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathEUpdateDialog.php
More file actions
130 lines (111 loc) · 3.44 KB
/
Copy pathEUpdateDialog.php
File metadata and controls
130 lines (111 loc) · 3.44 KB
1
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
<?php
/**
* EUpdateDialog class file.
*
* @author Andrius Marcinkevicius <andrew.web@ifdattic.com>
* @copyright Copyright © 2011 Andrius Marcinkevicius
* @license Licensed under MIT license. http://ifdattic.com/MIT-license.txt
* @version 2.0
*/
/**
* EUpdateDialog allows to make CRUD actions using JUI Dialog.
*
* @author Andrius Marcinkevicius <andrew.web@ifdattic.com>
*/
class EUpdateDialog extends CWidget
{
/**
* @var array an array with options for JUI Dialog.
*/
public $dialogOptions = array();
/**
* @var array an array with options for extension.
*/
public $options = array();
/**
* @var array an array with scripts to preload for performance.
*/
public $preload = array();
/**
* @var string open update dialog after clicking these elements.
*/
public $target = '.update-dialog-open-link';
/**
* @var string message category used for Yii::t method.
*/
public $tCategory = 'app';
/**
* Initializes the widget.
*/
public function init()
{
// Create default options array
$defaultOptions = array(
'autoOpen' => false,
'modal' => true,
);
// Merge with user set options if array is provided
if( is_array( $this->dialogOptions ) )
{
$this->dialogOptions = CMap::mergeArray(
$defaultOptions, $this->dialogOptions );
}
else
throw new CException( Yii::t( $this->tCategory,
'Widget options need to be an array' ) );
}
/**
* Add the update dialog to current page.
*/
public function run()
{
// Create jQuery UI dialog
$this->beginWidget( 'zii.widgets.jui.CJuiDialog', array(
'id' => 'update-dialog',
'options' => $this->dialogOptions,
)); ?>
<div class="update-dialog-content"></div>
<?php $this->endWidget();
// Publish extension assets
$assets = Yii::app()->getAssetManager()->publish( Yii::getPathOfAlias(
'ext.EUpdateDialog' ) . '/assets' );
// Register extension assets
$cs = Yii::app()->getClientScript();
$cs->registerScriptFile( $assets . '/EUpdateDialog.js',
CClientScript::POS_END );
// Add cookie script and CSRF cookie if using CSRF validation
if( Yii::app()->request->enableCsrfValidation )
{
// Include jQuery cookie plugin
$cs->registerCoreScript( 'cookie' );
// Add CSRF cookie
Yii::app()->request->getCsrfToken();
// Set CSRF token name for extension
$this->options['csrfTokenName'] = Yii::app()->request->csrfTokenName;
}
// Open update dialog the clicking target elements
$cs->registerScript( 'eupdatedialog', "
jQuery( '{$this->target}' ).live( 'click', updateDialogOpen );",
CClientScript::POS_END );
// Register additional options for an extension
if( $this->options !== array() )
{
// Initialize script variable
$js = '';
// Go through all options
foreach( $this->options as $option => $value )
{
// Encode option value
$value = CJavaScript::encode( $value );
// Add option to script variable
$js .= "updateDialog.{$option} = {$value};";
}
// Register options for an extension
$cs->registerScript( 'eupdatedialogoptions', $js, CClientScript::POS_END );
}
// Preload scripts for performance
foreach( $this->preload as $script )
$cs->registerScriptFile( $script, CClientScript::POS_END );
}
}
?>