示例#1
0
文件: dao.php 项目: hasshy/sahana-tw
 /**
  * Either creates volunteer information in the database if a volunteer is being added, or updates
  * volunteer information if a volunteer already exists.
  *
  * @param $v - a Volunteer object. If it's p_uuid is not set, new volunteer information is
  * 				inserted into the database. Otherwise, the information of the volunteer
  * 				whose p_uuid is $v->p_uuid is simply updated.
  * @param $shn_user	- (optional, default false) set to true if registering a current Sahana user as a volunteer
  * @return void
  */
 function saveVol(&$v, $shn_user = false)
 {
     if (isset($v->p_uuid) && !$shn_user) {
         // this Volunteer already has a p_uuid, so simply update its information
         //update full name
         $this->execute("UPDATE person_uuid SET full_name='{$v->info['full_name']}' WHERE p_uuid = '{$v->p_uuid}'");
         //delete old ID information and insert the new
         $this->execute("DELETE FROM identity_to_person WHERE p_uuid = '{$v->p_uuid}'");
         foreach ($v->info['ids'] as $id_type => $serial) {
             if (trim($serial) != '') {
                 $this->execute("INSERT INTO identity_to_person (opt_id_type,serial, p_uuid) values ('{$id_type}','{$serial}'  ,'{$v->p_uuid}')");
             } else {
                 unset($v->info['ids'][$id_type]);
             }
         }
         //update phonetic sound matching
         $this->execute("DELETE FROM phonetic_word WHERE pgl_uuid='{$v->p_uuid}'");
         $names = preg_split("/\\s+/", $v->info['full_name']);
         foreach ($names as $single_name) {
             $this->execute("INSERT INTO phonetic_word VALUES('" . soundex($single_name) . "', '" . metaphone($single_name) . "', '{$v->p_uuid}')");
         }
         //update gender, date of birth, and occupation information
         $result = $this->execute("Select p_uuid from person_details where  p_uuid ='{$v->p_uuid}'");
         if ($result->EOF) {
             $this->execute("INSERT INTO person_details (p_uuid,opt_gender,birth_date,occupation) values ('" . $v->p_uuid . "','" . $v->info['gender'] . "','" . $v->info['dob'] . "','" . $v->info['occupation'] . "')");
         } else {
             $this->execute("UPDATE person_details SET opt_gender = '{$v->info['gender']}', birth_date = '{$v->info['dob']}', occupation = '{$v->info['occupation']}' WHERE p_uuid ='{$v->p_uuid}'");
         }
         //update the location information
         $specific_loc = $v->info['locations'][0];
         if ($specific_loc == -1 || $specific_loc == null || $specific_loc === '0') {
             $this->execute("DELETE FROM location_details WHERE poc_uuid='{$v->p_uuid}'");
         } else {
             $result = $this->execute("SELECT location_id FROM location_details WHERE poc_uuid='{$v->p_uuid}'");
             if ($result->EOF) {
                 $this->execute("INSERT INTO location_details (poc_uuid, location_id) VALUES ('{$v->p_uuid}', '{$specific_loc}')");
             } else {
                 $this->execute("UPDATE location_details SET location_id='{$specific_loc}' WHERE poc_uuid='{$v->p_uuid}'");
             }
         }
         //Update a volunteers availability and organization affiliation along with hours of availability and special needs
         $this->execute("update vm_vol_details SET date_avail_start= '{$v->info['date_start']}',date_avail_end='{$v->info['date_end']}' ,hrs_avail_start= '{$v->info['hour_start']}',hrs_avail_end= '{$v->info['hour_end']}', org_id='{$v->info['affiliation']}', special_needs='{$v->info['special_needs']}' WHERE p_uuid='" . $v->p_uuid . "'");
         //delete the old contacts and replace with new ones if they are not blank
         $this->execute("DELETE FROM contact WHERE pgoc_uuid = '{$v->p_uuid}'");
         foreach ($v->info['contact'] as $key => $value) {
             if (trim($value != '')) {
                 $this->execute("insert into contact (pgoc_uuid, opt_contact_type, contact_value) values ('{$v->p_uuid}', '{$key}', '{$value}')");
             }
         }
         $this->execute("insert into contact (pgoc_uuid, opt_contact_type, contact_value) values ('{$v->p_uuid}', 'prefer', '{$v->info['contact_prefer']}')");
         //get rid of any pre-existing skills and replace them with the new
         $this->execute("DELETE FROM vm_vol_skills WHERE p_uuid = '{$v->p_uuid}'");
         foreach ($v->info['skills'] as $skill) {
             $this->execute("INSERT INTO vm_vol_skills (p_uuid, opt_skill_code) VALUES('{$v->p_uuid}', '{$skill}')");
         }
     } else {
         //create a new p_uuid for the volunteer and insert its new information into the database
         //generate a new p_uuid only if $shn_user is false
         global $global;
         if (!$shn_user) {
             require_once $global['approot'] . "/inc/lib_uuid.inc";
             $v->p_uuid = shn_create_uuid();
         }
         //create a Sahana account if necessary
         if (isset($v->info['account_info'])) {
             include_once $global['approot'] . 'inc/lib_security/lib_auth.inc';
             include_once $global['approot'] . 'inc/lib_security/constants.inc';
             $acct = $v->info['account_info'];
             //create an account and give the user 'Anonymous User' privileges
             shn_auth_add_user($acct['account_name'], $acct['user_name'], $acct['pass'], ANONYMOUS, $v->p_uuid);
         }
         //add phonetic sound matching
         $names = preg_split("/\\s+/", $v->info['full_name']);
         foreach ($names as $single_name) {
             $this->execute("INSERT INTO phonetic_word VALUES('" . soundex($single_name) . "', '" . metaphone($single_name) . "', '{$v->p_uuid}')");
         }
         //insert the volunteer's full name
         $result = $this->db->execute("insert into person_uuid (p_uuid, full_name) values ('" . $v->p_uuid . "', '" . $v->info['full_name'] . "')");
         //insert the volunteer's availibility and organization affiliation along with hours of availability and special needs
         $this->execute("insert into vm_vol_details (p_uuid,date_avail_start,date_avail_end,hrs_avail_start,hrs_avail_end, org_id, special_needs) values ('{$v->p_uuid}', '{$v->info['date_start']}', '{$v->info['date_end']}', '{$v->info['hour_start']}', '{$v->info['hour_end']}', '{$v->info['affiliation']}', '{$v->info['special_needs']}')");
         //insert new ID information
         if (!empty($v->info['ids'])) {
             $id_type = array_pop(array_keys($v->info["ids"]));
             $serial = $v->info['ids'][$id_type];
             if ($serial != '') {
                 $this->execute("INSERT INTO identity_to_person (opt_id_type,serial, p_uuid) values ('{$id_type}','{$serial}'  ,'{$v->p_uuid}')");
             } else {
                 unset($v->info['ids'][$id_type]);
             }
         }
         //insert gender, birth date, and occupation information
         $this->execute("INSERT INTO person_details (p_uuid,opt_gender,birth_date,occupation) values ('" . $v->p_uuid . "','" . $v->info['gender'] . "','" . $v->info['dob'] . "','" . $v->info['occupation'] . "')");
         //insert contact types
         if (!empty($v->info['contact'])) {
             foreach ($v->info['contact'] as $key => $value) {
                 if (trim($value != '')) {
                     $this->execute("insert into contact (pgoc_uuid, opt_contact_type, contact_value) values ('{$v->p_uuid}', '{$key}', '{$value}')");
                 }
             }
         }
         //insert skill information
         if (!empty($v->info['skills'])) {
             foreach ($v->info['skills'] as $skill) {
                 $this->execute("INSERT INTO vm_vol_skills (p_uuid, opt_skill_code) VALUES('{$v->p_uuid}', '{$skill}')");
             }
         }
         //insert the location information
         $specific_loc = $v->info['locations'][0];
         if ($specific_loc != null && $specific_loc != -1) {
             $this->execute("INSERT INTO location_details (poc_uuid, location_id) VALUES ('{$v->p_uuid}', '{$specific_loc}')");
         }
     }
 }
 public function act_add_user()
 {
     // var_dump($_POST);
     include_once APPROOT . 'inc/lib_form.inc';
     include_once APPROOT . 'inc/lib_form_util.inc';
     include_once APPROOT . 'inc/lib_validate.inc';
     include_once APPROOT . 'inc//security/lib_auth.inc';
     include_once 'lib_user.inc';
     $this->user_form = user_get_form();
     if (isset($_POST['save'])) {
         $valide = true;
         $username = $_POST['username'];
         $password1 = $_POST['password1'];
         $password2 = $_POST['password2'];
         $firstName = $_POST['first_name'];
         $lastName = $_POST['last_name'];
         $organization = $_POST['organization'];
         $designation = $_POST['designation'];
         $email = $_POST['email'];
         $address = $_POST['address'];
         $role = $_POST['role'];
         $status = $_POST['status'];
         $locale = $_POST['locale'];
         $user_form = $this->user_form;
         if (trim($username) == '') {
             $user_form['username']['extra_opts'] = array();
             $user_form['username']['extra_opts']['error'] = array();
             $user_form['username']['extra_opts']['error'][] = _t("USERNAME_CANNOT_BE_EMPTY");
             $user_form['username']['extra_opts']['required'][] = true;
             $valide = false;
         }
         if (UserHelper::isUser($username)) {
             $user_form['username']['extra_opts'] = array();
             $user_form['username']['extra_opts']['error'] = array();
             $user_form['username']['extra_opts']['error'][] = _t("USERNAME_ALREADY_EXISTS__USE_A_DIFFERENT_USERNAME");
             $user_form['username']['extra_opts']['required'][] = true;
             $valide = false;
         }
         if (trim($password1) == '') {
             $user_form['password1']['extra_opts'] = array();
             $user_form['password1']['extra_opts']['error'] = array();
             $user_form['password1']['extra_opts']['error'][] = _t("PASSWORD_REQUIRED");
             $user_form['password1']['extra_opts']['required'][] = true;
             $valide = false;
         }
         if (trim($password2) == '') {
             $user_form['password2']['extra_opts'] = array();
             $user_form['password2']['extra_opts']['error'] = array();
             $user_form['password2']['extra_opts']['error'][] = _t("PASSWORD_REQUIRED");
             $user_form['password2']['extra_opts']['required'][] = true;
             $valide = false;
         }
         if ($password1 != $password2) {
             $user_form['password2']['extra_opts'] = array();
             $user_form['password2']['extra_opts']['error'] = array();
             $user_form['password2']['extra_opts']['error'][] = _t("PASSWORD_MISMATCH");
             $user_form['password2']['extra_opts']['required'][] = true;
             $valide = false;
         }
         if (true) {
             //password match policy
         }
         if ($email != '' && !shn_valid_email($email)) {
             //email not valide
             $user_form['email']['extra_opts'] = array();
             $user_form['email']['extra_opts']['error'] = array();
             $user_form['email']['extra_opts']['error'][] = _t("INVALID_EMAIL_ADDRESS");
             $valide = false;
         }
         $status = $status == 'active' || $status == 'disable' ? $status : 'disable';
         $this->user_form = $user_form;
         if ($valide == true) {
             $userProfile = new UserProfile();
             $userProfile->username = $username;
             $userProfile->first_name = $firstName;
             $userProfile->last_name = $lastName;
             $userProfile->organization = $organization;
             $userProfile->designation = $designation;
             $userProfile->email = $email;
             $userProfile->address = $address;
             //$userProfile->Save();
             $userConfig = array();
             $userConfig['locale'] = $locale;
             shn_auth_add_user($username, $password1, $role, $userProfile, $status, $userConfig);
             set_redirect_header('admin', 'user_management');
         }
     }
 }