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

Revision 81, 16.6 KB checked in by niltonneto, 17 years ago (diff)

Atualização do CVS Interno com CVS Externo.
Modificações, vide changelog no arquivo changelog.txt ou no site do Expresso.

  • 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                        $input_context_fields = '<input type="text" size=60></input><br>';
154                       
155                        // Seta variaveis que estao no TPL
156                        $var = Array(
157                                'scripts_java'                  =>      $scripts_java, 
158                                'action'                                => $GLOBALS['phpgw']->link('/index.php','menuaction=expressoAdmin1_2.uimanagers.validate'),
159                                'display_samba_suport'  => $this->config['expressoAdmin_samba_support'] == 'true' ? '' : 'display:none',
160                                'type'                                  => "add",
161                                'color_bg1'                             => "#E8F0F0",
162                                'color_bg2'                             => "#D3DCE3",
163                                'color_font1'                   => "#DDDDDD",
164                                'color_font2'                   => "#EEEEEE",
165                                'input_context_fields'  => $input_context_fields,
166                                'error_messages'                => $_POST['error_messages'] == '' ? '' : '<script language="JavaScript">alert("'.$_POST['error_messages'].'");</script>',
167                                'manager_lid'                   => $_POST['manager_lid'],
168                                'context'                               => $_POST['context'],
169                                'app_list'                              => $applications_list
170                        );
171                        $p->set_var($var);
172                       
173                        // Cria dinamicamente os langs e seta acls
174                        foreach ($tpl_vars as $atribute)
175                        {
176                                $acl  = strstr($atribute, 'acl_');
177                                $lang = strstr($atribute, 'lang_');
178                                // Recuperar os valores das ACLS
179                                if ($acl !== false)
180                                {
181                                        $p->set_var($atribute, $_POST[$atribute] != '' ? 'checked' : '');
182                                }
183                                // Setar os langs do tpl.
184                                elseif($lang !== false)
185                                {
186                                        $p->set_var($atribute, $this->make_lang($atribute));
187                                }
188                        }
189                       
190                        echo $p->fp('out','form');
191                }
192       
193                function delete_managers()
194                {
195                        // Criar uma verificação e jogar a query para o BO.
196                        $context = $_GET['context'];
197                        $manager_lid = $_GET['manager_lid'];
198                       
199                        $query = "DELETE FROM phpgw_expressoadmin WHERE manager_lid = '".$manager_lid."' AND context = '" . $context ."'";
200                        $GLOBALS['phpgw']->db->query($query);
201                       
202                        // Remove Gerente da tabela dos apps
203                        $query = "DELETE FROM phpgw_expressoadmin_apps WHERE "
204                        . "manager_lid = '".$manager_lid."' AND "
205                        . "context = '".$context."'";
206                        $GLOBALS['phpgw']->db->query($query);           
207                       
208                        // Remove Gerente na ACL do expressoadmin
209                        $accounts = CreateObject('phpgwapi.accounts');
210                        $manager_id = $accounts->name2id($_GET['manager_lid']);
211                        $sql = "DELETE FROM phpgw_acl WHERE acl_appname = 'expressoadmin' AND acl_account = '" . $manager_id . "'";
212                        $GLOBALS['phpgw']->db->query($sql);                     
213                       
214                        ExecMethod('expressoAdmin1_2.uimanagers.list_managers');
215                }
216       
217                function edit_managers()
218                {
219                        // Caso nao seja admin, sai.
220                        if ($GLOBALS['phpgw']->acl->check('group_access',1,'admin'))
221                        {
222                                $GLOBALS['phpgw']->redirect($GLOBALS['phpgw']->link('/admin/index.php'));
223                        }
224                       
225                        // Verifica se eh a primeira entrada, ai eu tenho o get, senao pego o post.
226                        if ($_GET['manager_lid'] != '')
227                        {
228                                $first_time = true;
229                                $_POST['manager_lid']   = $_GET['manager_lid'];
230                                $_POST['context']               = $_GET['context'];
231                                $old_manager_lid                = $_GET['manager_lid'];
232                                $old_context                    = $_GET['context'];
233                        }
234                        elseif ($_POST['manager_lid'] != '')
235                        {
236                                $first_time             = false;
237                                $old_manager_lid        = $_POST['old_manager_lid'];
238                                $old_context            = $_POST['old_context'];                               
239                        }
240                       
241                        if ($first_time)
242                        {
243                                //Pego ACL do gerente
244                                $manager = $this->functions->read_acl($_GET['manager_lid']);
245                                //Cria vetor da ACL
246                                $manager_acl = $this->functions->make_array_acl($manager['acl']);
247
248                                //Pesquisa no Banco e pega os valores dos apps.
249                                $query = "SELECT * FROM phpgw_expressoadmin_apps WHERE manager_lid = '" . $_GET['manager_lid'] . "' AND context = '" . $_GET['context'] . "'";
250                                $GLOBALS['phpgw']->db->query($query);
251                                $i=0;
252                                $manager[0]['apps'] = array();
253                                while($GLOBALS['phpgw']->db->next_record())
254                                {
255                                        $tmp[$i] = $GLOBALS['phpgw']->db->row();
256                                        $_POST['applications_list'][$tmp[$i]['app']] = 1;
257                                        $manager[0]['apps'][$tmp[$i]['app']] = 1;
258                                        $i++;
259                                }
260                        }
261                       
262                        // Seta o template
263                        $p = CreateObject('phpgwapi.Template',PHPGW_APP_TPL);
264                        $p->set_file(array('managers' => 'managers_form.tpl'));
265                        $p->set_block('managers','form','form');
266                        $tpl_vars = $p->get_undefined('form');
267                       
268                        // Imprime o NavBar
269                        unset($GLOBALS['phpgw_info']['flags']['nonavbar']);
270                        $GLOBALS['phpgw_info']['flags']['app_header'] = $GLOBALS['phpgw_info']['apps']['expressoAdmin1_2']['title'].' - '.lang('Edit Managers');
271                        $GLOBALS['phpgw']->common->phpgw_header();
272
273                        // Seta variaveis javas necessárias
274                        $webserver_url = $GLOBALS['phpgw_info']['server']['webserver_url'];
275                        $scripts_java = '<script type="text/javascript" src="'.$webserver_url.'/expressoAdmin1_2/js/jscode/expressoadmin.js"></script>';
276
277                        // App, create list of available apps
278                        $applications_list = $this->make_app_list($manager[0]['apps']);
279
280                        $a_context = split("%", $_POST['context']);
281                        foreach ($a_context as $context)
282                                $input_context_fields .= '<input type="text" value="'.$context.'" size=60></input><br>';
283
284                        $var = Array(
285                                'scripts_java'  => $scripts_java,
286                                'action'                => $GLOBALS['phpgw']->link('/index.php','menuaction=expressoAdmin1_2.uimanagers.validate'),
287                                'display_samba_suport'  => $this->config['expressoAdmin_samba_support'] == 'true' ? '' : 'display:none',
288                                'color_bg1'             => "#E8F0F0",
289                                'color_bg2'             => "#D3DCE3",
290                                'color_font1'   => "#DDDDDD",
291                                'color_font2'   => "#EEEEEE",
292                                'type'                  => "edit",
293                                'error_messages'=> $_POST['error_messages'] == '' ? '' : '<script language="JavaScript1.3">alert("'.$_POST['error_messages'].'");</script>',
294                                'manager_lid'   => $_POST['manager_lid'],
295                                'context'               => $_POST['context'],
296                               
297                                'input_context_fields' => $input_context_fields,
298                               
299                                // Para o update no banco, preciso saber oq foi alterado e para que.
300                                // Talvez so o manager e o contexto.
301                                'old_manager_lid'       => $old_manager_lid,
302                                'old_context'           => $old_context,
303                                'app_list'                      => $applications_list
304                        );
305                        $p->set_var($var);
306                       
307                        // Cria dinamicamente os langs e seta acls
308                        foreach ($tpl_vars as $atribute)
309                        {
310                                $acl  = strstr($atribute, 'acl_');
311                                $lang = strstr($atribute, 'lang_');
312                                // Recuperar os valores das ACLS
313                                if ($acl !== false)
314                                {
315                                        if ($first_time)
316                                                $p->set_var($atribute, $manager_acl[$atribute] != '' ? 'checked' : '');
317                                        else
318                                                $p->set_var($atribute, $_POST[$atribute] != '' ? 'checked' : '');
319                                }
320                                // Setar os langs do tpl.
321                                elseif($lang !== false)
322                                {
323                                        $p->set_var($atribute, $this->make_lang($atribute));
324                                }
325                        }
326                       
327                        echo $p->fp('out','form');
328                }
329               
330                function validate()
331                {       
332                        $accounts = CreateObject('phpgwapi.accounts');
333                        $errors = array();
334                       
335                        // verifica se o manager existe. caso retorne 1 existe e eh uma conta de usuario.
336                        $manager_lid_exists = $accounts->exists($_POST['manager_lid']);
337                        if ($manager_lid_exists != 1)
338                        {
339                                $_POST['error_messages'] = lang("Manager LID don't exist.");
340                                if ($_POST['type'] == 'add')   
341                                        $this->add_managers();
342                                elseif ($_POST['type'] == 'edit')
343                                        $this->edit_managers();
344                                return;
345                        }
346                       
347                        // Verifica se o contexto existe.
348                        $dn                     = $GLOBALS['phpgw_info']['server']['ldap_root_dn'];
349                        $passwd         = $GLOBALS['phpgw_info']['server']['ldap_root_pw'];
350                        $ldap_conn      = ldap_connect($GLOBALS['phpgw_info']['server']['ldap_host']);
351                        ldap_set_option($ldap_conn, LDAP_OPT_PROTOCOL_VERSION, 3);
352                        ldap_set_option($ldap_conn, LDAP_OPT_REFERRALS, 0);
353                        ldap_bind($ldap_conn,$dn,$passwd);
354       
355                        $contexts = split("%", $_POST['context']);
356                       
357                        foreach ($contexts as $index=>$context)
358                        {
359                                $sr=@ldap_list($ldap_conn, $context, "cn=*");
360                                if (!$sr)
361                                {
362                                        $_POST['error_messages'] = lang("Context don't exist") . ": $context";                 
363                                        ldap_close($ldap_conn);
364                                        if ($_POST['type'] == 'add')   
365                                                $this->add_managers();
366                                        elseif ($_POST['type'] == 'edit')
367                                                $this->edit_managers();
368                                        return;
369                                }
370                        }
371                       
372                        if ($_POST['type'] == 'add')
373                        {
374                                //Verifica se ja existe o manager com aquele contexto cadastrado
375                                $query = "SELECT manager_lid FROM phpgw_expressoadmin WHERE manager_lid = '" . $_POST['manager_lid'] . "' AND context = '" . $_POST['context'] . "'";                           
376                                $GLOBALS['phpgw']->db->query($query);
377                                $num_registros = 0;
378                                while($GLOBALS['phpgw']->db->next_record())
379                                {
380                                        $tmp[] = $GLOBALS['phpgw']->db->row();
381                                        $num_registros++;
382                                }
383                                if ($num_registros != 0)
384                                {
385                                        $_POST['error_messages'] = lang('Este Gerente, neste contexto já exite !!');
386                                        $this->add_managers();
387                                        return;
388                                }
389                        }
390                       
391                        if ($_POST['type'] == 'add')
392                                ExecMethod('expressoAdmin1_2.bomanagers.add_managers');
393                        elseif ($_POST['type'] == 'edit')
394                                ExecMethod('expressoAdmin1_2.bomanagers.edit_managers');
395                       
396                        return true;
397                }
398               
399                function make_lang($ram_lang)
400                {
401                        $a_lang = split("_", $ram_lang);
402                        $a_lang_reverse  = array_reverse ( $a_lang, true );
403                        //Retira o lang do array.
404                        array_pop ( $a_lang_reverse );
405                        $a_lang  = array_reverse ( $a_lang_reverse, true );
406                        $a_new_lang = implode ( " ", $a_lang );
407                        return lang($a_new_lang);
408                }
409               
410                function make_app_list($manager_app_list)
411                {
412                        $this->nextmatchs = createobject('phpgwapi.nextmatchs');
413                        $apps = CreateObject('phpgwapi.applications',$_account_id);
414                        $db_perms = $apps->read_account_specific();
415                        $availableApps = $GLOBALS['phpgw_info']['apps'];
416                       
417                        uasort($availableApps,create_function('$a,$b','return strcasecmp($a["title"],$b["title"]);'));
418                       
419                        // Loop para criar dinamicamente uma tabela com 3 colunas, cada coluna com um aplicativo e um check box.
420                        $applications_list = '';
421                        $app_col1 = '';
422                        $app_col2 = '';
423                        $app_col3 = '';
424                        $total_apps = count($availableApps);
425                        $i = 0;
426
427                        foreach($availableApps as $app => $data)
428                        {
429                                // 1 coluna
430                                if (($i +1) % 3 == 1)
431                                {
432                                        if ($manager_app_list[$app] == 1)
433                                                $checked = 'checked';
434                                        else
435                                                $checked = '';
436                                        $app_col1 = sprintf("<td>%s</td><td width='10'><input type='checkbox' name='applications_list[%s]' value='1' %s %s></td>\n",
437                                        $data['title'],$app,$checked, $disabled);
438                                       
439                                        if ($i == ($total_apps-1))
440                                                $applications_list .= sprintf('<tr bgcolor="%s">%s</tr>',$this->nextmatchs->alternate_row_color(), $app_col1);
441                                }
442                                // 2 coluna
443                                if (($i +1) % 3 == 2)
444                                {
445                                        if ($manager_app_list[$app] == 1)
446                                                $checked = 'checked';
447                                        else
448                                                $checked = '';
449                                        $app_col2 = sprintf("<td>%s</td><td width='10'><input type='checkbox' name='applications_list[%s]' value='1' %s %s></td>\n",
450                                        $data['title'],$app,$checked, $disabled);
451                                       
452                                        if ($i == ($total_apps-1))
453                                                $applications_list .= sprintf('<tr bgcolor="%s">%s%s</tr>',$this->nextmatchs->alternate_row_color(), $app_col1,$app_col2);
454                                }
455                                // 3 coluna
456                                if (($i +1) % 3 == 0)
457                                {
458                                        if ($manager_app_list[$app] == 1)
459                                                $checked = 'checked';
460                                        else
461                                                $checked = '';
462                                        $app_col3 = sprintf("<td>%s</td><td width='10'><input type='checkbox' name='applications_list[%s]' value='1' %s %s></td>\n",
463                                        $data['title'],$app,$checked, $disabled);
464                                       
465                                        // Cria nova linha
466                                        $applications_list .= sprintf('<tr bgcolor="%s">%s%s%s</tr>',$this->nextmatchs->alternate_row_color(), $app_col1, $app_col2, $app_col3);                                       
467                                }
468                                $i++;
469                        }
470                        return $applications_list;
471                }
472        }
473?>
Note: See TracBrowser for help on using the repository browser.