source: trunk/expressoAdmin1_2/inc/class.uimanagers.inc.php @ 107

Revision 107, 16.8 KB checked in by niltonneto, 16 years ago (diff)

Inclusão de funcionalidade

  • Property svn:eol-style set to native
  • Property svn:executable set to *
Line 
1<?php
2        /************************************************************************************\
3        * Expresso Administração                                                                                                    *
4        * by Joao Alfredo Knopik Junior (joao.alfredo@gmail.com, jakjr@celepar.pr.gov.br)   *
5        * ----------------------------------------------------------------------------------*
6        *  This program is free software; you can redistribute it and/or modify it                      *
7        *  under the terms of the GNU General Public License as published by the                        *
8        *  Free Software Foundation; either version 2 of the License, or (at your                       *
9        *  option) any later version.                                                                                                           *
10        \************************************************************************************/
11
12        class uimanagers
13        {
14                var $public_functions = array
15                (
16                        'list_managers'         => True,
17                        'add_managers'          => True,
18                        'delete_managers'       => True,
19                        'edit_managers'         => True,
20                        'validate'                      => True
21                );
22
23                var $functions;
24                var $config;
25
26                function uimanagers()
27                {
28                        $this->functions = CreateObject('expressoAdmin1_2.functions');
29                        $c = CreateObject('phpgwapi.config','expressoAdmin1_2');
30                        $c->read_repository();
31                        $this->config = $c->config_data;
32                       
33                        if(!@is_object($GLOBALS['phpgw']->js))
34                        {
35                                $GLOBALS['phpgw']->js = CreateObject('phpgwapi.javascript');
36                        }
37                        $GLOBALS['phpgw']->js->validate_file('jscode','connector','expressoAdmin1_2');#diretorio, arquivo.js, aplicacao
38                        $GLOBALS['phpgw']->js->validate_file('jscode','managers','expressoAdmin1_2');
39                }
40
41                function row_action($lang,$link,$manager_lid,$context)
42                {       
43                        return '<a href="'.$GLOBALS['phpgw']->link('/index.php',Array(
44                                'menuaction' => 'expressoAdmin1_2.uimanagers.'.$link,
45                                'action'                =>      $lang,
46                                'manager_lid' => $manager_lid,
47                                'context' => $context
48                        )).'" onmouseover="window.status=\''.lang($lang).' Manager\'; return true;" onmouseout="window.status=\'\';" >'.lang($lang).' </a>';
49                }
50                               
51                function list_managers()
52                {
53                        // Caso nao seja admin, sai.
54                        if ($GLOBALS['phpgw']->acl->check('group_access',1,'admin'))
55                        {
56                                $GLOBALS['phpgw']->redirect($GLOBALS['phpgw']->link('/admin/index.php'));
57                        }
58                        // Imprime o NavBar
59                        unset($GLOBALS['phpgw_info']['flags']['nonavbar']);
60                        $GLOBALS['phpgw_info']['flags']['app_header'] = $GLOBALS['phpgw_info']['apps']['expressoAdmin1_2']['title'].' - '.lang('List Managers');
61                        $GLOBALS['phpgw']->common->phpgw_header();
62
63                        // Seta o template
64                        $p = CreateObject('phpgwapi.Template',PHPGW_APP_TPL);
65                        $p->set_file(array('managers' => 'managers.tpl'));
66                        $p->set_block('managers','body','body');
67                        $p->set_block('managers','row','row');
68                        $p->set_block('managers','row_empty','row_empty');
69                        $tpl_vars = $p->get_undefined('body');
70
71                        $var = Array(
72                                'action'                        => $GLOBALS['phpgw']->link('/index.php','menuaction=expressoAdmin1_2.uimanagers.add_managers'),
73                                'tr_color'                      => '#DDDDDD',
74                                'th_bg'                 => $GLOBALS['phpgw_info']['theme']['th_bg']
75                        );
76
77                        // Cria dinamicamente os langs
78                        foreach ($tpl_vars as $atribute)
79                        {
80                                $lang = strstr($atribute, 'lang_');
81                                if($lang !== false)
82                                {
83                                        $p->set_var($atribute, $this->make_lang($atribute));
84                                }
85                        }
86
87                        // Le BD para pegar os administradors.
88                        $query = 'SELECT manager_lid,context FROM phpgw_expressoadmin ORDER by manager_lid';
89                        $GLOBALS['phpgw']->db->query($query);
90                        while($GLOBALS['phpgw']->db->next_record())
91                        {
92                                $managers[] = $GLOBALS['phpgw']->db->row();
93                        }
94                        $ldap_conn = $GLOBALS['phpgw']->common->ldapConnect();
95                        $justthese = array("cn");
96                        // Loop para listar os administradores
97                        if (count($managers))
98                        {
99                                foreach($managers as $array_managers)
100                                {
101                                        $managers_context = "";
102                                        $a_managers_context = split("%", $array_managers['context']);
103
104                                        foreach ($a_managers_context as $context)
105                                        {
106                                                $managers_context .= "$context<br>";
107                                        }
108                                       
109                                        $filter="(&(phpgwAccountType=u)(uid=".$array_managers['manager_lid']."))";
110                                        $ldap_search = ldap_search($ldap_conn, $GLOBALS['phpgw_info']['server']['ldap_context'], $filter, $justthese);
111                                        $ldap_result = ldap_get_entries($ldap_conn, $ldap_search);
112                                        $p->set_var('manager_lid', $array_managers[manager_lid]);
113                                        $p->set_var('manager_cn', $ldap_result[0]['cn'][0] == '' ? '<font color=red>NAO ENCONTRADO NO LDAP</font>' : $ldap_result[0]['cn'][0]);
114                                        $p->set_var('context', $managers_context);
115                                        $p->set_var('link_edit',$this->row_action('edit','edit_managers',$array_managers[manager_lid],$array_managers[context]));
116                                        $p->set_var('link_delete',$this->row_action('delete','delete_managers',$array_managers[manager_lid],$array_managers[context]));
117                                        $p->set_var('link_copy',"<a href='#' onClick='javascript:copy_manager(\"".$array_managers['manager_lid']."\");'>Copiar</a>");
118                                        $p->fp('rows','row',True);
119                                }
120                        }
121                        $p->set_var($var);
122                        $p->pfp('out','body');
123                        ldap_close($ldap_conn);
124                }
125
126
127                function add_managers()
128                {
129                        // Caso nao seja admin, sai.
130                        if ($GLOBALS['phpgw']->acl->check('group_access',1,'admin'))
131                        {
132                                $GLOBALS['phpgw']->redirect($GLOBALS['phpgw']->link('/admin/index.php'));
133                        }
134                       
135                        // Seta o template
136                        $p = CreateObject('phpgwapi.Template',PHPGW_APP_TPL);
137                        $p->set_file(array('managers' => 'managers_form.tpl'));
138                        $p->set_block('managers','form','form');
139                        $tpl_vars = $p->get_undefined('form');
140
141                        // Imprime o NavBar
142                        unset($GLOBALS['phpgw_info']['flags']['nonavbar']);
143                        $GLOBALS['phpgw_info']['flags']['app_header'] = $GLOBALS['phpgw_info']['apps']['expressoAdmin1_2']['title'].' - '.lang('Add Managers');
144                        $GLOBALS['phpgw']->common->phpgw_header();
145                       
146                        // Seta variaveis javascript necessárias
147                        $webserver_url = $GLOBALS['phpgw_info']['server']['webserver_url'];
148                        $scripts_java = '<script type="text/javascript" src="'.$webserver_url.'/expressoAdmin1_2/js/jscode/expressoadmin.js"></script>';
149                       
150                        // App, create list of available apps
151                        $applications_list = $this->make_app_list('');
152                       
153                        if ($_POST['context'])
154                        {
155                                $contexts = split("%", $_POST['context']);
156                                foreach ($contexts as $manager_context)
157                                        $input_context_fields .= "<input type='text' size=60 value=$manager_context></input><br>";
158                        }
159                        else
160                        $input_context_fields = '<input type="text" size=60></input><br>';
161                       
162                        // Seta variaveis que estao no TPL
163                        $var = Array(
164                                'scripts_java'                  =>      $scripts_java, 
165                                'action'                                => $GLOBALS['phpgw']->link('/index.php','menuaction=expressoAdmin1_2.uimanagers.validate'),
166                                'display_samba_suport'  => $this->config['expressoAdmin_samba_support'] == 'true' ? '' : 'display:none',
167                                'type'                                  => "add",
168                                'color_bg1'                             => "#E8F0F0",
169                                'color_bg2'                             => "#D3DCE3",
170                                'color_font1'                   => "#DDDDDD",
171                                'color_font2'                   => "#EEEEEE",
172                                'input_context_fields'  => $input_context_fields,
173                                'error_messages'                => $_POST['error_messages'] == '' ? '' : '<script language="JavaScript">alert("'.$_POST['error_messages'].'");</script>',
174                                'manager_lid'                   => $_POST['manager_lid'],
175                                'context'                               => $_POST['context'],
176                                'app_list'                              => $applications_list
177                        );
178                        $p->set_var($var);
179                       
180                        // Cria dinamicamente os langs e seta acls
181                        foreach ($tpl_vars as $atribute)
182                        {
183                                $acl  = strstr($atribute, 'acl_');
184                                $lang = strstr($atribute, 'lang_');
185                                // Recuperar os valores das ACLS
186                                if ($acl !== false)
187                                {
188                                        $p->set_var($atribute, $_POST[$atribute] != '' ? 'checked' : '');
189                                }
190                                // Setar os langs do tpl.
191                                elseif($lang !== false)
192                                {
193                                        $p->set_var($atribute, $this->make_lang($atribute));
194                                }
195                        }
196                       
197                        echo $p->fp('out','form');
198                }
199       
200                function delete_managers()
201                {
202                        // Criar uma verificação e jogar a query para o BO.
203                        $context = $_GET['context'];
204                        $manager_lid = $_GET['manager_lid'];
205                       
206                        $query = "DELETE FROM phpgw_expressoadmin WHERE manager_lid = '".$manager_lid."' AND context = '" . $context ."'";
207                        $GLOBALS['phpgw']->db->query($query);
208                       
209                        // Remove Gerente da tabela dos apps
210                        $query = "DELETE FROM phpgw_expressoadmin_apps WHERE "
211                        . "manager_lid = '".$manager_lid."' AND "
212                        . "context = '".$context."'";
213                        $GLOBALS['phpgw']->db->query($query);           
214                       
215                        // Remove Gerente na ACL do expressoadmin
216                        $accounts = CreateObject('phpgwapi.accounts');
217                        $manager_id = $accounts->name2id($_GET['manager_lid']);
218                        $sql = "DELETE FROM phpgw_acl WHERE acl_appname = 'expressoadmin' AND acl_account = '" . $manager_id . "'";
219                        $GLOBALS['phpgw']->db->query($sql);                     
220                       
221                        ExecMethod('expressoAdmin1_2.uimanagers.list_managers');
222                }
223       
224                function edit_managers()
225                {
226                        // Caso nao seja admin, sai.
227                        if ($GLOBALS['phpgw']->acl->check('group_access',1,'admin'))
228                        {
229                                $GLOBALS['phpgw']->redirect($GLOBALS['phpgw']->link('/admin/index.php'));
230                        }
231                       
232                        // Verifica se eh a primeira entrada, ai eu tenho o get, senao pego o post.
233                        if ($_GET['manager_lid'] != '')
234                        {
235                                $first_time = true;
236                                $_POST['manager_lid']   = $_GET['manager_lid'];
237                                $_POST['context']               = $_GET['context'];
238                                $old_manager_lid                = $_GET['manager_lid'];
239                                $old_context                    = $_GET['context'];
240                        }
241                        elseif ($_POST['manager_lid'] != '')
242                        {
243                                $first_time             = false;
244                                $old_manager_lid        = $_POST['old_manager_lid'];
245                                $old_context            = $_POST['old_context'];                               
246                        }
247                       
248                        if ($first_time)
249                        {
250                                //Pego ACL do gerente
251                                $manager = $this->functions->read_acl($_GET['manager_lid']);
252                                //Cria vetor da ACL
253                                $manager_acl = $this->functions->make_array_acl($manager['acl']);
254
255                                //Pesquisa no Banco e pega os valores dos apps.
256                                $query = "SELECT * FROM phpgw_expressoadmin_apps WHERE manager_lid = '" . $_GET['manager_lid'] . "' AND context = '" . $_GET['context'] . "'";
257                                $GLOBALS['phpgw']->db->query($query);
258                                $i=0;
259                                $manager[0]['apps'] = array();
260                                while($GLOBALS['phpgw']->db->next_record())
261                                {
262                                        $tmp[$i] = $GLOBALS['phpgw']->db->row();
263                                        $_POST['applications_list'][$tmp[$i]['app']] = 1;
264                                        $manager[0]['apps'][$tmp[$i]['app']] = 1;
265                                        $i++;
266                                }
267                        }
268                       
269                        // Seta o template
270                        $p = CreateObject('phpgwapi.Template',PHPGW_APP_TPL);
271                        $p->set_file(array('managers' => 'managers_form.tpl'));
272                        $p->set_block('managers','form','form');
273                        $tpl_vars = $p->get_undefined('form');
274                       
275                        // Imprime o NavBar
276                        unset($GLOBALS['phpgw_info']['flags']['nonavbar']);
277                        $GLOBALS['phpgw_info']['flags']['app_header'] = $GLOBALS['phpgw_info']['apps']['expressoAdmin1_2']['title'].' - '.lang('Edit Managers');
278                        $GLOBALS['phpgw']->common->phpgw_header();
279
280                        // Seta variaveis javas necessárias
281                        $webserver_url = $GLOBALS['phpgw_info']['server']['webserver_url'];
282                        $scripts_java = '<script type="text/javascript" src="'.$webserver_url.'/expressoAdmin1_2/js/jscode/expressoadmin.js"></script>';
283
284                        // App, create list of available apps
285                        $applications_list = $this->make_app_list($manager[0]['apps']);
286
287                        $a_context = split("%", $_POST['context']);
288                        foreach ($a_context as $context)
289                                $input_context_fields .= '<input type="text" value="'.$context.'" size=60></input><br>';
290
291                        $var = Array(
292                                'scripts_java'  => $scripts_java,
293                                'action'                => $GLOBALS['phpgw']->link('/index.php','menuaction=expressoAdmin1_2.uimanagers.validate'),
294                                'display_samba_suport'  => $this->config['expressoAdmin_samba_support'] == 'true' ? '' : 'display:none',
295                                'color_bg1'             => "#E8F0F0",
296                                'color_bg2'             => "#D3DCE3",
297                                'color_font1'   => "#DDDDDD",
298                                'color_font2'   => "#EEEEEE",
299                                'type'                  => "edit",
300                                'error_messages'=> $_POST['error_messages'] == '' ? '' : '<script language="JavaScript1.3">alert("'.$_POST['error_messages'].'");</script>',
301                                'manager_lid'   => $_POST['manager_lid'],
302                                'context'               => $_POST['context'],
303                               
304                                'input_context_fields' => $input_context_fields,
305                               
306                                // Para o update no banco, preciso saber oq foi alterado e para que.
307                                // Talvez so o manager e o contexto.
308                                'old_manager_lid'       => $old_manager_lid,
309                                'old_context'           => $old_context,
310                                'app_list'                      => $applications_list
311                        );
312                        $p->set_var($var);
313                       
314                        // Cria dinamicamente os langs e seta acls
315                        foreach ($tpl_vars as $atribute)
316                        {
317                                $acl  = strstr($atribute, 'acl_');
318                                $lang = strstr($atribute, 'lang_');
319                                // Recuperar os valores das ACLS
320                                if ($acl !== false)
321                                {
322                                        if ($first_time)
323                                                $p->set_var($atribute, $manager_acl[$atribute] != '' ? 'checked' : '');
324                                        else
325                                                $p->set_var($atribute, $_POST[$atribute] != '' ? 'checked' : '');
326                                }
327                                // Setar os langs do tpl.
328                                elseif($lang !== false)
329                                {
330                                        $p->set_var($atribute, $this->make_lang($atribute));
331                                }
332                        }
333                       
334                        echo $p->fp('out','form');
335                }
336               
337                function validate()
338                {       
339                        $accounts = CreateObject('phpgwapi.accounts');
340                        $errors = array();
341                       
342                        // verifica se o manager existe. caso retorne 1 existe e eh uma conta de usuario.
343                        $manager_lid_exists = $accounts->exists($_POST['manager_lid']);
344                        if ($manager_lid_exists != 1)
345                        {
346                                $_POST['error_messages'] = lang("Manager LID don't exist.");
347                                if ($_POST['type'] == 'add')   
348                                        $this->add_managers();
349                                elseif ($_POST['type'] == 'edit')
350                                        $this->edit_managers();
351                                return;
352                        }
353                       
354                        // Verifica se o contexto existe.
355                        $dn                     = $GLOBALS['phpgw_info']['server']['ldap_root_dn'];
356                        $passwd         = $GLOBALS['phpgw_info']['server']['ldap_root_pw'];
357                        $ldap_conn      = ldap_connect($GLOBALS['phpgw_info']['server']['ldap_host']);
358                        ldap_set_option($ldap_conn, LDAP_OPT_PROTOCOL_VERSION, 3);
359                        ldap_set_option($ldap_conn, LDAP_OPT_REFERRALS, 0);
360                        ldap_bind($ldap_conn,$dn,$passwd);
361       
362                        $contexts = split("%", $_POST['context']);
363                       
364                        foreach ($contexts as $index=>$context)
365                        {
366                                $sr=@ldap_list($ldap_conn, $context, "cn=*");
367                                if (!$sr)
368                                {
369                                        $_POST['error_messages'] = lang("Context don't exist") . ": $context";                 
370                                        ldap_close($ldap_conn);
371                                        if ($_POST['type'] == 'add')   
372                                                $this->add_managers();
373                                        elseif ($_POST['type'] == 'edit')
374                                                $this->edit_managers();
375                                        return;
376                                }
377                        }
378                       
379                        if ($_POST['type'] == 'add')
380                        {
381                                //Verifica se ja existe o manager com aquele contexto cadastrado
382                                $query = "SELECT manager_lid FROM phpgw_expressoadmin WHERE manager_lid = '" . $_POST['manager_lid'] . "' AND context = '" . $_POST['context'] . "'";                           
383                                $GLOBALS['phpgw']->db->query($query);
384                                $num_registros = 0;
385                                while($GLOBALS['phpgw']->db->next_record())
386                                {
387                                        $tmp[] = $GLOBALS['phpgw']->db->row();
388                                        $num_registros++;
389                                }
390                                if ($num_registros != 0)
391                                {
392                                        $_POST['error_messages'] = lang('Este Gerente, neste contexto já exite !!');
393                                        $this->add_managers();
394                                        return;
395                                }
396                        }
397                       
398                        if ($_POST['type'] == 'add')
399                                ExecMethod('expressoAdmin1_2.bomanagers.add_managers');
400                        elseif ($_POST['type'] == 'edit')
401                                ExecMethod('expressoAdmin1_2.bomanagers.edit_managers');
402                       
403                        return true;
404                }
405               
406                function make_lang($ram_lang)
407                {
408                        $a_lang = split("_", $ram_lang);
409                        $a_lang_reverse  = array_reverse ( $a_lang, true );
410                        //Retira o lang do array.
411                        array_pop ( $a_lang_reverse );
412                        $a_lang  = array_reverse ( $a_lang_reverse, true );
413                        $a_new_lang = implode ( " ", $a_lang );
414                        return lang($a_new_lang);
415                }
416               
417                function make_app_list($manager_app_list)
418                {
419                        $this->nextmatchs = createobject('phpgwapi.nextmatchs');
420                        $apps = CreateObject('phpgwapi.applications',$_account_id);
421                        $db_perms = $apps->read_account_specific();
422                        $availableApps = $GLOBALS['phpgw_info']['apps'];
423                       
424                        uasort($availableApps,create_function('$a,$b','return strcasecmp($a["title"],$b["title"]);'));
425                       
426                        // Loop para criar dinamicamente uma tabela com 3 colunas, cada coluna com um aplicativo e um check box.
427                        $applications_list = '';
428                        $app_col1 = '';
429                        $app_col2 = '';
430                        $app_col3 = '';
431                        $total_apps = count($availableApps);
432                        $i = 0;
433
434                        foreach($availableApps as $app => $data)
435                        {
436                                // 1 coluna
437                                if (($i +1) % 3 == 1)
438                                {
439                                        if ($manager_app_list[$app] == 1)
440                                                $checked = 'checked';
441                                        else
442                                                $checked = '';
443                                        $app_col1 = sprintf("<td>%s</td><td width='10'><input type='checkbox' name='applications_list[%s]' value='1' %s %s></td>\n",
444                                        $data['title'],$app,$checked, $disabled);
445                                       
446                                        if ($i == ($total_apps-1))
447                                                $applications_list .= sprintf('<tr bgcolor="%s">%s</tr>',$this->nextmatchs->alternate_row_color(), $app_col1);
448                                }
449                                // 2 coluna
450                                if (($i +1) % 3 == 2)
451                                {
452                                        if ($manager_app_list[$app] == 1)
453                                                $checked = 'checked';
454                                        else
455                                                $checked = '';
456                                        $app_col2 = sprintf("<td>%s</td><td width='10'><input type='checkbox' name='applications_list[%s]' value='1' %s %s></td>\n",
457                                        $data['title'],$app,$checked, $disabled);
458                                       
459                                        if ($i == ($total_apps-1))
460                                                $applications_list .= sprintf('<tr bgcolor="%s">%s%s</tr>',$this->nextmatchs->alternate_row_color(), $app_col1,$app_col2);
461                                }
462                                // 3 coluna
463                                if (($i +1) % 3 == 0)
464                                {
465                                        if ($manager_app_list[$app] == 1)
466                                                $checked = 'checked';
467                                        else
468                                                $checked = '';
469                                        $app_col3 = sprintf("<td>%s</td><td width='10'><input type='checkbox' name='applications_list[%s]' value='1' %s %s></td>\n",
470                                        $data['title'],$app,$checked, $disabled);
471                                       
472                                        // Cria nova linha
473                                        $applications_list .= sprintf('<tr bgcolor="%s">%s%s%s</tr>',$this->nextmatchs->alternate_row_color(), $app_col1, $app_col2, $app_col3);                                       
474                                }
475                                $i++;
476                        }
477                        return $applications_list;
478                }
479        }
480?>
Note: See TracBrowser for help on using the repository browser.