16e607d711d27f8317db3239fb1c4f96d6880ff9
[acontent.git] / docs / user / user_create_edit.php
1 <?php
2 /************************************************************************/
3 /* AContent                                                             */
4 /************************************************************************/
5 /* Copyright (c) 2010                                                   */
6 /* Inclusive Design Institute                                           */
7 /*                                                                      */
8 /* This program is free software. You can redistribute it and/or        */
9 /* modify it under the terms of the GNU General Public License          */
10 /* as published by the Free Software Foundation.                        */
11 /************************************************************************/
12
13 define('TR_INCLUDE_PATH', '../include/');
14 include_once(TR_INCLUDE_PATH.'vitals.inc.php');
15 include_once(TR_INCLUDE_PATH.'classes/DAO/UsersDAO.class.php');
16 include_once(TR_INCLUDE_PATH.'classes/DAO/UserGroupsDAO.class.php');
17
18 // handle submit
19 $_GET['id'] = intval($_GET['id']);
20 if (isset($_POST['cancel'])) {
21         header('Location: index.php');
22         exit;
23 } else if (isset($_POST['submit'])) {
24         require_once(TR_INCLUDE_PATH. 'classes/DAO/UsersDAO.class.php');
25         $usersDAO = new UsersDAO();
26         
27         /* password check: password is verified front end by javascript. here is to handle the errors from javascript */
28         if ($_POST['password_error'] <> "")
29         {
30                 $pwd_errors = explode(",", $_POST['password_error']);
31
32                 foreach ($pwd_errors as $pwd_error)
33                 {
34                         if ($pwd_error == "missing_password")
35                                 $missing_fields[] = _AT('password');
36                         else
37                                 $msg->addError($pwd_error);
38                 }
39         }
40         else
41         {
42                 if (isset($_POST['is_author'])) $is_author = 1;
43                 else $is_author = 0;
44                 
45                 if (!isset($_GET['id']))  // create new user
46                 {
47                         $user_id = $usersDAO->Create($_POST['user_group_id'],
48                               $_POST['login'],
49                                       $_POST['form_password_hidden'],
50                                       $_POST['email'],
51                                       $_POST['first_name'],
52                                       $_POST['last_name'],
53                                   $is_author,
54                                   $_POST['organization'],
55                                   $_POST['phone'],
56                                   $_POST['address'],
57                                   $_POST['city'],
58                                   $_POST['province'],
59                                   $_POST['country'],
60                                   $_POST['postal_code'],
61                                       $_POST['status']);
62                         
63                         if (is_int($user_id) && $user_id > 0)
64                         {
65                                 if (defined('TR_EMAIL_CONFIRMATION') && TR_EMAIL_CONFIRMATION) {
66                                         $msg->addFeedback('REG_THANKS_CONFIRM');
67                 
68                                         $code = substr(md5($_POST['email'] . $now . $user_id), 0, 10);
69                                         
70                                         $confirmation_link = $_base_href . 'confirm.php?id='.$user_id.SEP.'m='.$code;
71                 
72                                         /* send the email confirmation message: */
73                                         require(TR_INCLUDE_PATH . 'classes/phpmailer/transformablemailer.class.php');
74                                         $mail = new TransformableMailer();
75                 
76                                         $mail->From     = $_config['contact_email'];
77                                         $mail->AddAddress($_POST['email']);
78                                         $mail->Subject = SITE_NAME . ' - ' . _AT('email_confirmation_subject');
79                                         $mail->Body    = _AT('email_confirmation_message', SITE_NAME, $confirmation_link)."\n\n";
80                 
81                                         $mail->Send();
82                                 } 
83                                 else 
84                                 {
85                                         $msg->addFeedback('ACTION_COMPLETED_SUCCESSFULLY');
86                                         header('Location: index.php');
87                                         exit;
88                                 }
89                         }
90                 }
91                 else  // edit existing user
92                 {
93                         if ($usersDAO->Update($_GET['id'], 
94                                           $_POST['user_group_id'],
95                                   $_POST['login'],
96                                           $_POST['email'],
97                                           $_POST['first_name'],
98                                           $_POST['last_name'],
99                                       $is_author,
100                                       $_POST['organization'],
101                                       $_POST['phone'],
102                                       $_POST['address'],
103                                       $_POST['city'],
104                                       $_POST['province'],
105                                       $_POST['country'],
106                                       $_POST['postal_code'],
107                                           $_POST['status']))
108                         
109                         {
110                                 $msg->addFeedback('ACTION_COMPLETED_SUCCESSFULLY');
111                                 header('Location: index.php');
112                                 exit;
113                         }
114                 }
115         }
116 }
117 // end of handle submit
118
119 // initialize page 
120 $userGroupsDAO = new UserGroupsDAO();
121
122 if (isset($_GET['id'])) // edit existing user
123 {
124         $usersDAO = new UsersDAO();
125         $savant->assign('user_row', $usersDAO->getUserByID($_GET['id']));
126         $savant->assign('show_password', false);
127         
128 }
129 else  // create new user
130 {
131         $savant->assign('show_password', true);
132         
133 }
134 /*****************************/
135 /* template starts down here */
136
137 global $onload;
138 $onload = 'document.form.login.focus();';
139
140 $savant->assign('show_user_group', true);
141 $savant->assign('show_status', true);
142 $savant->assign('all_user_groups', $userGroupsDAO->getAll());
143 $savant->assign('title', _AT('create_edit_user'));
144 $savant->assign('submit_button_text', _AT('save'));
145
146 $savant->display('register.tmpl.php');
147
148 ?>