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(
59 Yii::app()->cache->flush();
69 $basePath = Yii::app()->getModule(
'user')->getBasePath() .
'/views/asset';
70 $baseUrl = Yii::app()->getModule(
'user')->getAssetsUrl();
71 $cs = Yii::app()->getClientScript();
72 $coreBaseUrl = $cs->getCoreScriptUrl();
73 $cs->registerCoreScript(
'jquery');
74 $cs->registerCssFile($coreBaseUrl .
'/jui/css/base/jquery-ui.css',
'', null,
false);
75 $cs->registerCssFile($baseUrl .
'/css/style.css');
76 $cs->registerScriptFile($coreBaseUrl .
'/jui/js/jquery-ui.min.js');
77 $cs->registerScriptFile($baseUrl .
'/js/form.js');
78 $cs->registerScriptFile($baseUrl .
'/js/jquery.json.js');
80 $widgets = self::getWidgets();
82 $wgByTypes = ProfileField::itemAlias(
'field_type');
83 foreach ($wgByTypes as $k => $v) {
84 $wgByTypes[$k] = array();
87 foreach ($widgets[1] as $widget) {
88 if (isset($widget[
'fieldType']) && count($widget[
'fieldType'])) {
89 foreach ($widget[
'fieldType'] as $type) {
90 array_push($wgByTypes[$type], $widget[
'name']);
97 var name = $('#name'),
99 allFields = $([]).add(name).add(value),
100 tips = $('.validateTips');
102 var listWidgets = jQuery.parseJSON('" . str_replace(
"'",
"\'", CJavaScript::jsonEncode($widgets[0])) .
"');
103 var widgets = jQuery.parseJSON('" . str_replace(
"'",
"\'", CJavaScript::jsonEncode($widgets[1])) .
"');
104 var wgByType = jQuery.parseJSON('" . str_replace(
"'",
"\'", CJavaScript::jsonEncode($wgByTypes)) .
"');
108 'hide':['match','other_validator','widgetparams'],
117 'hide':['widgetparams'],
126 'hide':['field_size','range','widgetparams'],
135 'hide':['field_size','field_size_min','match','range','widgetparams'],
138 'default':'0000-00-00',
144 'hide':['match','other_validator','widgetparams'],
153 'hide':['field_size','field_size_min','match','widgetparams'],
162 'hide':['field_size','field_size_min','match','widgetparams'],
171 'hide':['field_size','field_size_min','match','widgetparams'],
181 function showWidgetList(type) {
182 $('div.widget select').empty();
183 $('div.widget select').append('<option value=\"\">" .
UserModule::t(
'No') .
"</option>');
184 if (wgByType[type]) {
185 for (var k in wgByType[type]) {
186 $('div.widget select').append('<option value=\"'+wgByType[type][k]+'\">'+widgets[wgByType[type][k]]['label']+'</option>');
191 function setFields(type) {
192 if (fieldType[type]) {
193 if (" . ((isset($_GET[
'id'])) ? 0 : 1) .
") {
194 showWidgetList(type);
195 $('#widgetlist option:first').attr('selected', 'selected');
198 $('div.row').addClass('toshow').removeClass('tohide');
199 if (fieldType[type].hide.length) $('div.'+fieldType[type].hide.join(', div.')).addClass('tohide').removeClass('toshow');
200 if ($('div.widget select').val()) {
201 $('div.widgetparams').removeClass('tohide');
203 $('div.toshow').show(500);
204 $('div.tohide').hide(500);
205 " . ((!isset($_GET[
'id'])) ?
"
206 for (var k in fieldType[type].val) {
207 $('div.'+k+' input').val(fieldType[type].val[k]);
212 function isArray(obj) {
213 if (obj.constructor.toString().indexOf('Array') == -1)
219 $('#dialog-form').dialog({
228 $('#dialog-form fieldset .wparam').each(function(){
229 if ($(this).val()) wparam[$(this).attr('name')] = $(this).val();
232 var tab = $('#tabs ul li.ui-tabs-selected').text();
234 $('#dialog-form fieldset .tab-'+tab).each(function(){
235 if ($(this).val()) fparam[tab][$(this).attr('name')] = $(this).val();
238 if ($.JSON.encode(wparam)!='{}') $('div.widgetparams input').val($.JSON.encode(wparam));
239 if ($.JSON.encode(fparam[tab])!='{}') $('div.other_validator input').val($.JSON.encode(fparam));
241 $(this).dialog('close');
244 $(this).dialog('close');
252 $('#widgetparams').focus(function() {
253 var widget = widgets[$('#widgetlist').val()];
255 var wparam = ($('div.widgetparams input').val())?$.JSON.decode($('div.widgetparams input').val()):{};
256 var fparam = ($('div.other_validator input').val())?$.JSON.decode($('div.other_validator input').val()):{};
259 for (var k in widget.params) {
260 html += '<label for=\"name\">'+((widget.paramsLabels[k])?widget.paramsLabels[k]:k)+'</label>';
261 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])+'\" />';
264 if (widget.other_validator) {
267 for (var t in widget.other_validator) {
268 tabs += '<div id=\"tab-'+t+'\" class=\"tab\">';
269 li += '<li'+((fparam[t])?' class=\"ui-tabs-selected\"':'')+'><a href=\"#tab-'+t+'\">'+t+'</a></li>';
271 for (var k in widget.other_validator[t]) {
272 tabs += '<label for=\"name\">'+((widget.paramsLabels[k])?widget.paramsLabels[k]:k)+'</label>';
273 if (isArray(widget.other_validator[t][k])) {
274 tabs += '<select type=\"text\" name=\"'+k+'\" id=\"filter_'+k+'\" class=\"text fparam ui-widget-content ui-corner-all tab-'+t+'\">';
275 for (var i in widget.other_validator[t][k]) {
276 tabs += '<option value=\"'+widget.other_validator[t][k][i]+'\"'+((fparam[t]&&fparam[t][k])?' selected=\"selected\"':'')+'>'+widget.other_validator[t][k][i]+'</option>';
280 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])+'\" />';
285 html += '<div id=\"tabs\"><ul>'+li+'</ul>'+tabs+'</div>';
288 $('#dialog-form fieldset').html(html);
293 $('#dialog-form').dialog('open');
296 $('#field_type').change(function() {
297 setFields($(this).val());
300 $('#widgetlist').change(function() {
302 $('div.widgetparams').show(500);
304 $('div.widgetparams').hide(500);
310 $('div.form p.note').append('<br/><a href=\"#\" id=\"showAll\">" .
UserModule::t(
'Show all') .
"</a>');
311 $('#showAll').click(function(){
312 $('div.row').show(500);
317 setFields($('#field_type').val());
320 $cs->registerScript(__CLASS__ .
'#dialog', $js);
330 $scheme = get_class(Yii::app()->db->schema);
331 if (isset($_POST[
'ProfileField'])) {
332 $model->attributes = $_POST[
'ProfileField'];
334 if ($model->validate()) {
335 $sql =
'ALTER TABLE ' .
Profile::model()->tableName() .
' ADD `' . $model->varname .
'` ';
336 $sql .= $this->
fieldType($model->field_type);
338 $model->field_type !=
'TEXT'
339 && $model->field_type !=
'DATE'
340 && $model->field_type !=
'BOOL'
341 && $model->field_type !=
'BLOB'
342 && $model->field_type !=
'BINARY'
344 $sql .=
'(' . $model->field_size .
')';
345 $sql .=
' NOT NULL ';
347 if ($model->field_type !=
'TEXT' && $model->field_type !=
'BLOB' || $scheme !=
'CMysqlSchema') {
349 $sql .=
" DEFAULT '" . $model->default .
"'";
352 $model->field_type ==
'TEXT'
353 || $model->field_type ==
'VARCHAR'
354 || $model->field_type ==
'BLOB'
355 || $model->field_type ==
'BINARY'
356 ) ?
" DEFAULT ''" : (($model->field_type ==
'DATE') ?
" DEFAULT '0000-00-00'" :
" DEFAULT 0"));
358 $model->dbConnection->createCommand($sql)->execute();
360 $this->redirect(array(
'view',
'id' => $model->id));
365 $this->render(
'create', array(
377 if (isset($_POST[
'ProfileField'])) {
378 $model->attributes = $_POST[
'ProfileField'];
380 $this->redirect(array(
'view',
'id' => $model->id));
384 $this->render(
'update', array(
398 $scheme = get_class(Yii::app()->db->schema);
400 if ($scheme ==
'CSqliteSchema') {
402 unset($attr[$model->varname]);
403 $attr = array_keys($attr);
404 $connection = Yii::app()->db;
405 $transaction = $connection->beginTransaction();
409 $connection->createCommand(
410 "CREATE TEMPORARY TABLE " .
Profile::model()->tableName() .
"_backup (" . implode(
',', $attr) .
")"
413 $connection->createCommand(
417 $connection->createCommand(
421 $connection->createCommand(
422 "CREATE TABLE " .
Profile::model()->tableName() .
" (" . implode(
',', $attr) .
")"
425 $connection->createCommand(
426 "INSERT INTO " .
Profile::model()->tableName() .
" SELECT " . implode(
',', $attr) .
" FROM " .
Profile::model()->tableName() .
"_backup"
429 $connection->createCommand(
433 $transaction->commit();
434 }
catch (Exception $e) {
435 $transaction->rollBack();
443 $sql =
'ALTER TABLE ' .
Profile::model()->tableName() .
' DROP `' . $model->varname .
'`';
444 if ($model->dbConnection->createCommand($sql)->execute()) {
450 if (!isset($_POST[
'ajax'])) {
451 $this->redirect(array(
'admin'));
464 $model->field_size = null;
465 $model->required = null;
466 $model->position = null;
467 $model->visible = null;
468 if (isset($_GET[
'ProfileField'])) {
469 $model->attributes = $_GET[
'ProfileField'];
472 $this->render(
'admin', array(
483 if ($this->_model === null) {
484 if (isset($_GET[
'id']))
486 if ($this->_model === null)
487 throw new CHttpException(404,
'The requested page does not exist.');
489 return $this->_model;
499 $type = str_replace(
'UNIX-DATE',
'INTEGER', $type);
503 public static function getWidgets($fieldType =
'')
506 $basePath = Yii::app()->getModule(
'user')->getBasePath() .
'/components';
509 if (self::$_widgets) {
510 $widgets = self::$_widgets;
513 while (
false !== ($file = $d->read())) {
514 if (strpos($file,
'UW') === 0) {
515 list($className) = explode(
'.', $file);
516 if (class_exists($className)) {
517 $widgetClass =
new $className;
518 if ($widgetClass->init()) {
519 $widgets[$className] = $widgetClass->init();
521 if (in_array($fieldType, $widgets[$className][
'fieldType'])) $list[$className] = $widgets[$className][
'label'];
523 $list[$className] = $widgets[$className][
'label'];
531 return array($list, $widgets);