cruge / components / CrugeUi.php

  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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
<?php
/** CrugeUi


provee herramientas para la interfaz de usuario:

1. Url (en forma de array, relativas al modulo:  /cruge/ui/xxxx)
2. Links
3. array para menues (para usar en los portlets basicamente)
4. rutas a recursos (getResource)
5. dar formato (a fechas por ejemplo, con formatDate)

dependencias:

1. CrugeUtil
2. CrugeTranslator

se accede exclusivamente mediante:

Yii::app()->user->ui


los links principales son:

<?php echo Yii::app()->user->ui->loginLink; ?>
<?php echo Yii::app()->user->ui->logoutLink; ?>
<?php echo Yii::app()->user->ui->passwordRecoveryLink; ?>
<?php echo Yii::app()->user->ui->userManagementAdminLink; ?>

@author: Christian Salazar H. <christiansalazarh@gmail.com> @salazarchris74
@license protected/modules/cruge/LICENSE
 */
class CrugeUi extends CComponent
{

    public function getResource($filename = "")
    {
        return Yii::app()->getController()->basePath . "/" . $filename;
    }

    public function formatDate($longdatevalue)
    {
        if ($longdatevalue <= 0) {
            return "";
        }
        return Yii::app()->format->formatDateTime($longdatevalue);
    }

    public function getLoginUrl()
    {
        return CrugeUtil::uiaction('login');
    }

    public function getLogoutUrl()
    {
        return CrugeUtil::uiaction('logout');
    }

    public function getPasswordRecoveryUrl()
    {
        return CrugeUtil::uiaction('pwdrec');
    }

    public function getUserManagementAdminUrl()
    {
        return CrugeUtil::uiaction('usermanagementadmin');
    }

    public function getUserManagementCreateUrl()
    {
        return CrugeUtil::uiaction('usermanagementcreate');
    }

    public function getEditProfileUrl()
    {
        return CrugeUtil::uiaction('editprofile');
    }

    public function getUserManagementDeleteUrl()
    {
        return CrugeUtil::uiaction('usermanagementdelete');
    }

    public function getRegistrationUrl()
    {
        return CrugeUtil::uiaction('registration');
    }

    public function getFieldsAdminListUrl()
    {
        return CrugeUtil::uiaction('fieldsadminlist');
    }

    public function getFieldsAdminCreateUrl()
    {
        return CrugeUtil::uiaction('fieldsadmincreate');
    }

    public function getFieldsAdminUpdateUrl()
    {
        return CrugeUtil::uiaction('fieldsadminupdate');
    }

    public function getRbacListRolesUrl()
    {
        return CrugeUtil::uiaction('rbaclistroles');
    }

    public function getRbacListTasksUrl()
    {
        return CrugeUtil::uiaction('rbaclisttasks');
    }

    public function getRbacListOpsUrl()
    {
        return CrugeUtil::uiaction('rbaclistops');
    }

    // argumento extra es usado en CrugeAuthManager para crear
    // tareas enlazadas a otras en forma de submenu, extra==authitem_parent.
    public function getRbacAuthItemCreateUrl($type, $extra = '')
    {
        // aqui type es uno de los valores de
        // CAuthItem::TYPE_ROLE,CAuthItem::TYPE_TASK,CAuthItem::TYPE_OPERATION
        return CrugeUtil::uiaction(
            'rbacauthitemcreate',
            array('type' => $type, 'extra' => $extra)
        );
    }

    public function getRbacAuthItemDeleteUrl($id)
    {
        return CrugeUtil::uiaction('rbacauthitemdelete', array('id' => $id));
    }

    public function getRbacAuthItemUpdateUrl($id)
    {
        return CrugeUtil::uiaction('rbacauthitemupdate', array('id' => $id));
    }

    public function getRbacAuthItemChildItemsUrl($id = null)
    {
        if ($id == null) {
            return CrugeUtil::uiaction('rbacauthitemchilditems');
        }
        return CrugeUtil::uiaction('rbacauthitemchilditems', array('id' => $id));
    }

    public function getRbacUsersAssignmentsUrl()
    {
        return CrugeUtil::uiaction('rbacusersassignments');
    }

    public function getRbacAjaxSetChildItemUrl()
    {
        return CrugeUtil::uiaction('rbacajaxsetchilditem');
    }

    public function getRbacAjaxAssignmentUrl()
    {
        return CrugeUtil::uiaction('rbacajaxassignment');
    }

    public function getRbacAjaxGetAssignmentBzUrl()
    {
        return CrugeUtil::uiaction('rbacajaxgetassignmentbizrule');
    }

    public function getRbacAjaxSetAssignmentBzUrl()
    {
        return CrugeUtil::uiaction('rbacajaxsetassignmentbizrule');
    }

    public function getAjaxGenerateNewPasswordUrl()
    {
        return CrugeUtil::uiaction('ajaxgeneratenewpassword');
    }

    public function getAjaxResendRegistrationEmailUrl($id)
    {
        return CrugeUtil::uiaction('ajaxresendregistrationemail', array('id' => $id));
    }

    public function getSessionAdminUrl()
    {
        return CrugeUtil::uiaction('sessionadmin');
    }

    public function getSystemUpdateUrl()
    {
        return CrugeUtil::uiaction('systemupdate');
    }

    public function getLoginLink($label = null)
    {
        if ($label === null) {
            $label = 'Login';
        }
        return CHtml::link(CrugeTranslator::t('logon', $label), self::getLoginUrl());
    }

    public function getLogoutLink($label = null)
    {
        if ($label === null) {
            $label = 'Logout';
        }
        return CHtml::link(CrugeTranslator::t('logon', $label), self::getLogoutUrl());
    }

    public function getPasswordRecoveryLink($label = null)
    {
        if ($label === null) {
            $label = 'Lost Password?';
        }
        return CHtml::link(CrugeTranslator::t('logon', $label), self::getPasswordRecoveryUrl());
    }

    public function getEditProfileLink($label = null)
    {
        if ($label === null) {
            $label = 'Update Profile';
        }
        return CHtml::link(CrugeTranslator::t('admin', $label), self::getEditProfileUrl());
    }

    public function getUserManagementAdminLink($label = null)
    {
        if ($label === null) {
            $label = 'Control Panel';
        }
        return CHtml::link(CrugeTranslator::t('admin', $label), self::getUserManagementAdminUrl());
    }

    public function getUserManagementCreateLink($label = null)
    {
        if ($label === null) {
            $label = 'Create User';
        }
        return CHtml::link(CrugeTranslator::t('admin', $label), self::getUserManagementCreateUrl());
    }

    public function getRegistrationLink($label = null)
    {
        if ($label === null) {
            $label = 'Register';
        }
        return CHtml::link(CrugeTranslator::t('logon', $label), self::getRegistrationUrl());
    }


    public function getFieldAdminListLink($label = null)
    {
        if ($label === null) {
            $label = 'List Fields';
        }
        return CHtml::link(CrugeTranslator::t('admin', $label), self::getFieldsAdminListUrl());
    }

    public function getFieldAdminCreateLink($label = null)
    {
        if ($label === null) {
            $label = 'Create Field';
        }
        return CHtml::link(CrugeTranslator::t('admin', $label), self::getFieldsAdminCreateUrl());
    }


    /*
        entrega los items para un CPortlet o cualquier CMenu compatible, incluso Bootstrap.

        actualmente este metodo se invoca desde /cruge/layouts/ui.php
    */
    public function getAdminItems()
    {
        return array(
            array('label' => CrugeTranslator::t('admin', 'User Manager')),
            array(
                'label' => CrugeTranslator::t('admin', 'Update Profile')
            ,
                'url' => $this->getEditProfileUrl()
            ),
            array(
                'label' => CrugeTranslator::t('admin', 'Create User')
            ,
                'url' => $this->getUserManagementCreateUrl()
            ),
            array(
                'label' => CrugeTranslator::t('admin', 'Manage Users')
            ,
                'url' => $this->getUserManagementAdminUrl()
            ),
            array('label' => CrugeTranslator::t('admin', 'Custom Fields')),
            array(
                'label' => CrugeTranslator::t('admin', 'List Profile Fields')
            ,
                'url' => $this->getFieldsAdminListUrl()
            ),
            array(
                'label' => CrugeTranslator::t('admin', 'Create Profile Field')
            ,
                'url' => $this->getFieldsAdminCreateUrl()
            ),
            array('label' => CrugeTranslator::t('admin', 'Roles and Assignments')),
            array(
                'label' => CrugeTranslator::t('admin', 'Roles')
            ,
                'url' => $this->getRbacListRolesUrl()
            ),
            array(
                'label' => CrugeTranslator::t('admin', 'Tasks')
            ,
                'url' => $this->getRbacListTasksUrl()
            ),
            array(
                'label' => CrugeTranslator::t('admin', 'Operations')
            ,
                'url' => $this->getRbacListOpsUrl()
            ),
            array(
                'label' => CrugeTranslator::t('admin', 'Assign Roles to Users')
            ,
                'url' => $this->getRbacUsersAssignmentsUrl()
            ),
            array('label' => CrugeTranslator::t('admin', 'System')),
            array(
                'label' => CrugeTranslator::t('admin', 'Sessions')
            ,
                'url' => $this->getSessionAdminUrl()
            ),
            array(
                'label' => CrugeTranslator::t('admin', 'System Variables')
            ,
                'url' => $this->getSystemUpdateUrl()
            ),
        );
    }

    /*	acumula los errores obtenidos durante el request para su posterior visualizacion con displayErrorConsole

        este metodo es llamado desde CrugeWebUser::checkAccess cuando el flag
        CrugeModule::rbacSetupEnabled esta habilitado, el objetivo es informar que el $itemName
        indicado ha fallado para el usuario activo.

        con este metodo se pretende ayudar al operador a que programe los permisos requeridos
        para el rol o los roles asignados a un usuario en particular.

        @see displayErrorConsole
    */
    public function addError($itemName, $tipoItemName = '', $description = "")
    {
        if (CrugeUtil::config()->rbacSetupEnabled == true) {
            CrugeUtil::config()->globalErrors[] =
                array('itemName' => $itemName, 'type' => $tipoItemName, 'descr' => $description);
        }
        // siempre reporta el error de acceso en logger:
        //
        Yii::log(
            CrugeTranslator::t('logger', 'PERMISSION IS REQUIRED') . ":\n" . Yii::app(
            )->user->name . "\niduser=" . Yii::app()->user->id
                . "\ntipo:{$tipoItemName}\nitemName:{$itemName}\n" . $description
            ,
            "rbac"
        );
    }

    /**
    este metodo deberia ser invocado al final del layout principal del cliente para que le muestre los errores obtenidos y recopilados por addError

    @see addError
     */
    public function displayErrorConsole()
    {
        $outputText = "";
        if (CrugeUtil::config()->rbacSetupEnabled == true) {
            $n = 0;
            foreach (CrugeUtil::config()->globalErrors as $gerr) {
                if ($n == 0) {

                    $user = "ID=" . Yii::app()->user->id . ", NAME=" . Yii::app()->user->name;

                    $title = CrugeTranslator::t(
                        'logger',
                        'This page displays the roles, tasks and operations that are required by the current user but unassigned. This message is displayed because CrugeModule::rbacSetupEnabled = true'
                    );
                    $icon = "";
                    $outputText = "<div title='$title' class='rbac-global-error-list'>";
                    $outputText .= "<h6>" . $icon . ucwords(
                        CrugeTranslator::t('logger', 'Assignments required by the user')
                    ) . ":" . $user . "</h6>";
                    $outputText .= "<ul>";
                }
                $tipo = "";
                if ($gerr['type'] != '') {
                    $tipo = " ({$gerr['type']})";
                }
                $desc = "";
                if ($gerr['descr'] != '') {
                    $desc = " ({$gerr['descr']})";
                }
                $outputText .= "<li><b>" . $gerr['itemName'] . "</b>" . $tipo . $desc . "</li>";
                $n++;
            }
            if ($n > 0) {
                $outputText .= "</ul></div>";
            }
        }
        return $outputText;
    }

    public function superAdminNote()
    {
        return "<div class='is-superadmin-note'>" .
            CrugeTranslator::t('admin', '*** You are working as Super Administrator ***') .
            "</div>";
    }

    public function setupAlert($message)
    {

        $estilo = "text-align: center;background-color: rgb(255,140,140);margin: 3px;padding: 3px;border-radius: 5px;color: black; font-weight: bold;box-shadow: 3px 3px 3px #333;overflow: auto; position: absolute; top:0px; left: 10%;";
        $alertReason = CrugeTranslator::t(
            'admin',
            'This message is displayed because you have "debug" parameter enabled in the config file.'
        );
        return "<div style='$estilo'>$message<br/><span style=\"color: white; font-size: small;\">{$alertReason}</span></div>";
    }

    public function getCGridViewClass()
    {
        return CrugeUtil::config()->useCGridViewClass;
    }

    public function bbutton($texto, $name = 'volver')
    {
        $this->_button($texto, $name);
    }

    public function tbutton($texto)
    {
        $this->_button($texto);
    }

    private function _button($label, $name = null)
    {
        $ar = array();
        $_type = 'submit'; // siempre son submit
        $_icon = 'remove white';
        if ($name == null) {
            $_icon = 'ok white';
            $name = 'submit';
        }
        $ar = array('name' => $name);

        $label = ucwords($label);

        $estiloBoton = CrugeUtil::config()->buttonStyle;

        if ($estiloBoton == 'jui') {
            Yii::app()->getController()->widget(
                'zii.widgets.jui.CJuiButton',
                array(
                    'name' => $name,
                    'caption' => $label,
                )
            );
            return;
        }

        if ($estiloBoton == 'bootstrap') {
            Yii::app()->getController()->widget(
                'bootstrap.widgets.TbButton'
                ,
                array(
                    'buttonType' => $_type,
                    'type' => 'primary',
                    'htmlOptions' => $ar,
                    'icon' => $_icon,
                    'label' => $label,
                    'size' => CrugeUtil::config()->buttonConf, // '', 'large', 'small' or 'mini'
                )
            );
            return;
        }

        echo CHtml::submitButton($label, $ar);
    }

}
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.