10 private static $_widgets = array();
11 public $defaultAction =
'admin';
18 return CMap::mergeArray(parent::filters(), array(
36 'actions' => array(
'create',
'update',
'view',
'admin',
'delete'),
37 'roles' => array(
'Admin'),
40 'users' => array(
'*'),
50 $this->render(
'view', array(
61 $basePath = Yii::app()->getModule(
'user')->getBasePath() .
'/views/asset';
62 $baseUrl = Yii::app()->getModule(
'user')->getAssetsUrl();
63 $cs = Yii::app()->getClientScript();
64 $coreBaseUrl = $cs->getCoreScriptUrl();
65 $cs->registerCoreScript(
'jquery');
66 $cs->registerCssFile($coreBaseUrl .
'/jui/css/base/jquery-ui.css',
'', null,
false);
67 $cs->registerCssFile($baseUrl .
'/css/style.css');
68 $cs->registerScriptFile($coreBaseUrl .
'/jui/js/jquery-ui.min.js');
69 $cs->registerScriptFile($baseUrl .
'/js/form.js');
70 $cs->registerScriptFile($baseUrl .
'/js/jquery.json.js');
72 $widgets = self::getWidgets();
74 $wgByTypes = ProfileField::itemAlias(
'field_type');
75 foreach ($wgByTypes as $k => $v) {
76 $wgByTypes[$k] = array();
79 foreach ($widgets[1] as $widget) {
80 if (isset($widget[
'fieldType']) && count($widget[
'fieldType'])) {
81 foreach ($widget[
'fieldType'] as $type) {
82 array_push($wgByTypes[$type], $widget[
'name']);
89 var name = $('#name'),
91 allFields = $([]).add(name).add(value),
92 tips = $('.validateTips');
94 var listWidgets = jQuery.parseJSON('" . str_replace(
"'",
"\'", CJavaScript::jsonEncode($widgets[0])) .
"');
95 var widgets = jQuery.parseJSON('" . str_replace(
"'",
"\'", CJavaScript::jsonEncode($widgets[1])) .
"');
96 var wgByType = jQuery.parseJSON('" . str_replace(
"'",
"\'", CJavaScript::jsonEncode($wgByTypes)) .
"');
100 'hide':['match','other_validator','widgetparams'],
109 'hide':['widgetparams'],
118 'hide':['field_size','range','widgetparams'],
127 'hide':['field_size','field_size_min','match','range','widgetparams'],
130 'default':'0000-00-00',
136 'hide':['match','other_validator','widgetparams'],
145 'hide':['field_size','field_size_min','match','widgetparams'],
154 'hide':['field_size','field_size_min','match','widgetparams'],
163 'hide':['field_size','field_size_min','match','widgetparams'],
173 function showWidgetList(type) {
174 $('div.widget select').empty();
175 $('div.widget select').append('<option value=\"\">" .
UserModule::t(
'No') .
"</option>');
176 if (wgByType[type]) {
177 for (var k in wgByType[type]) {
178 $('div.widget select').append('<option value=\"'+wgByType[type][k]+'\">'+widgets[wgByType[type][k]]['label']+'</option>');
183 function setFields(type) {
184 if (fieldType[type]) {
185 if (" . ((isset($_GET[
'id'])) ? 0 : 1) .
") {
186 showWidgetList(type);
187 $('#widgetlist option:first').attr('selected', 'selected');
190 $('div.row').addClass('toshow').removeClass('tohide');
191 if (fieldType[type].hide.length) $('div.'+fieldType[type].hide.join(', div.')).addClass('tohide').removeClass('toshow');
192 if ($('div.widget select').val()) {
193 $('div.widgetparams').removeClass('tohide');
195 $('div.toshow').show(500);
196 $('div.tohide').hide(500);
197 " . ((!isset($_GET[
'id'])) ?
"
198 for (var k in fieldType[type].val) {
199 $('div.'+k+' input').val(fieldType[type].val[k]);
204 function isArray(obj) {
205 if (obj.constructor.toString().indexOf('Array') == -1)
211 $('#dialog-form').dialog({
220 $('#dialog-form fieldset .wparam').each(function(){
221 if ($(this).val()) wparam[$(this).attr('name')] = $(this).val();
224 var tab = $('#tabs ul li.ui-tabs-selected').text();
226 $('#dialog-form fieldset .tab-'+tab).each(function(){
227 if ($(this).val()) fparam[tab][$(this).attr('name')] = $(this).val();
230 if ($.JSON.encode(wparam)!='{}') $('div.widgetparams input').val($.JSON.encode(wparam));
231 if ($.JSON.encode(fparam[tab])!='{}') $('div.other_validator input').val($.JSON.encode(fparam));
233 $(this).dialog('close');
236 $(this).dialog('close');
244 $('#widgetparams').focus(function() {
245 var widget = widgets[$('#widgetlist').val()];
247 var wparam = ($('div.widgetparams input').val())?$.JSON.decode($('div.widgetparams input').val()):{};
248 var fparam = ($('div.other_validator input').val())?$.JSON.decode($('div.other_validator input').val()):{};
251 for (var k in widget.params) {
252 html += '<label for=\"name\">'+((widget.paramsLabels[k])?widget.paramsLabels[k]:k)+'</label>';
253 html += '<input type=\"text\" name=\"'+k+'\" id=\"widget_'+k+'\" class=\"text wparam ui-widget-content ui-corner-all\" value=\"'+((wparam[k])?wparam[k]:widget.params[k])+'\" />';
256 if (widget.other_validator) {
259 for (var t in widget.other_validator) {
260 tabs += '<div id=\"tab-'+t+'\" class=\"tab\">';
261 li += '<li'+((fparam[t])?' class=\"ui-tabs-selected\"':'')+'><a href=\"#tab-'+t+'\">'+t+'</a></li>';
263 for (var k in widget.other_validator[t]) {
264 tabs += '<label for=\"name\">'+((widget.paramsLabels[k])?widget.paramsLabels[k]:k)+'</label>';
265 if (isArray(widget.other_validator[t][k])) {
266 tabs += '<select type=\"text\" name=\"'+k+'\" id=\"filter_'+k+'\" class=\"text fparam ui-widget-content ui-corner-all tab-'+t+'\">';
267 for (var i in widget.other_validator[t][k]) {
268 tabs += '<option value=\"'+widget.other_validator[t][k][i]+'\"'+((fparam[t]&&fparam[t][k])?' selected=\"selected\"':'')+'>'+widget.other_validator[t][k][i]+'</option>';
272 tabs += '<input type=\"text\" name=\"'+k+'\" id=\"filter_'+k+'\" class=\"text fparam ui-widget-content ui-corner-all tab-'+t+'\" value=\"'+((fparam[t]&&fparam[t][k])?fparam[t][k]:widget.other_validator[t][k])+'\" />';
277 html += '<div id=\"tabs\"><ul>'+li+'</ul>'+tabs+'</div>';
280 $('#dialog-form fieldset').html(html);
285 $('#dialog-form').dialog('open');
288 $('#field_type').change(function() {
289 setFields($(this).val());
292 $('#widgetlist').change(function() {
294 $('div.widgetparams').show(500);
296 $('div.widgetparams').hide(500);
302 $('div.form p.note').append('<br/><a href=\"#\" id=\"showAll\">" .
UserModule::t(
'Show all') .
"</a>');
303 $('#showAll').click(function(){
304 $('div.row').show(500);
309 setFields($('#field_type').val());
312 $cs->registerScript(__CLASS__ .
'#dialog', $js);
322 $scheme = get_class(Yii::app()->db->schema);
323 if (isset($_POST[
'ProfileField'])) {
324 $model->attributes = $_POST[
'ProfileField'];
326 if ($model->validate()) {
327 $sql =
'ALTER TABLE ' .
Profile::model()->tableName() .
' ADD `' . $model->varname .
'` ';
328 $sql .= $this->
fieldType($model->field_type);
330 $model->field_type !=
'TEXT'
331 && $model->field_type !=
'DATE'
332 && $model->field_type !=
'BOOL'
333 && $model->field_type !=
'BLOB'
334 && $model->field_type !=
'BINARY'
336 $sql .=
'(' . $model->field_size .
')';
337 $sql .=
' NOT NULL ';
339 if ($model->field_type !=
'TEXT' && $model->field_type !=
'BLOB' || $scheme !=
'CMysqlSchema') {
341 $sql .=
" DEFAULT '" . $model->default .
"'";
344 $model->field_type ==
'TEXT'
345 || $model->field_type ==
'VARCHAR'
346 || $model->field_type ==
'BLOB'
347 || $model->field_type ==
'BINARY'
348 ) ?
" DEFAULT ''" : (($model->field_type ==
'DATE') ?
" DEFAULT '0000-00-00'" :
" DEFAULT 0"));
350 $model->dbConnection->createCommand($sql)->execute();
352 $this->redirect(array(
'view',
'id' => $model->id));
357 $this->render(
'create', array(
369 if (isset($_POST[
'ProfileField'])) {
370 $model->attributes = $_POST[
'ProfileField'];
372 $this->redirect(array(
'view',
'id' => $model->id));
376 $this->render(
'update', array(
390 $scheme = get_class(Yii::app()->db->schema);
392 if ($scheme ==
'CSqliteSchema') {
394 unset($attr[$model->varname]);
395 $attr = array_keys($attr);
396 $connection = Yii::app()->db;
397 $transaction = $connection->beginTransaction();
401 $connection->createCommand(
402 "CREATE TEMPORARY TABLE " .
Profile::model()->tableName() .
"_backup (" . implode(
',', $attr) .
")"
405 $connection->createCommand(
409 $connection->createCommand(
413 $connection->createCommand(
414 "CREATE TABLE " .
Profile::model()->tableName() .
" (" . implode(
',', $attr) .
")"
417 $connection->createCommand(
418 "INSERT INTO " .
Profile::model()->tableName() .
" SELECT " . implode(
',', $attr) .
" FROM " .
Profile::model()->tableName() .
"_backup"
421 $connection->createCommand(
425 $transaction->commit();
426 }
catch (Exception $e) {
427 $transaction->rollBack();
435 $sql =
'ALTER TABLE ' .
Profile::model()->tableName() .
' DROP `' . $model->varname .
'`';
436 if ($model->dbConnection->createCommand($sql)->execute()) {
442 if (!isset($_POST[
'ajax'])) {
443 $this->redirect(array(
'admin'));
456 $model->field_size = null;
457 $model->required = null;
458 $model->position = null;
459 $model->visible = null;
460 if (isset($_GET[
'ProfileField'])) {
461 $model->attributes = $_GET[
'ProfileField'];
464 $this->render(
'admin', array(
475 if ($this->_model === null) {
476 if (isset($_GET[
'id']))
478 if ($this->_model === null)
479 throw new CHttpException(404,
'The requested page does not exist.');
481 return $this->_model;
491 $type = str_replace(
'UNIX-DATE',
'INTEGER', $type);
495 public static function getWidgets($fieldType =
'')
498 $basePath = Yii::app()->getModule(
'user')->getBasePath() .
'/components';
501 if (self::$_widgets) {
502 $widgets = self::$_widgets;
505 while (
false !== ($file = $d->read())) {
506 if (strpos($file,
'UW') === 0) {
507 list($className) = explode(
'.', $file);
508 if (class_exists($className)) {
509 $widgetClass =
new $className;
510 if ($widgetClass->init()) {
511 $widgets[$className] = $widgetClass->init();
513 if (in_array($fieldType, $widgets[$className][
'fieldType'])) $list[$className] = $widgets[$className][
'label'];
515 $list[$className] = $widgets[$className][
'label'];
523 return array($list, $widgets);