Exemplo n.º 1
0
 public function update($id)
 {
     if ($this->input->is_ajax_request()) {
         /*
         | --------------------------------------
         | # Validation
         | --------------------------------------
         */
         if (null != $this->input->post('updating_group')) {
             $contact = $this->Contact->find($id);
             $contact_group = [];
             if (!empty($contact->contacts_group)) {
                 $contact_group = explode(",", $contact->contacts_group);
             }
             # If we're Adding a Group
             if ("add" == $this->input->post('action')) {
                 # Check if the value is already in the resource,
                 # add to array if not.
                 if (!in_array($this->input->post('value'), $contact_group)) {
                     $contact_group[] = $this->input->post('value');
                 } else {
                     $data = array('message' => 'Contact is already in this group', 'type' => 'danger');
                     echo json_encode($data);
                     exit;
                 }
             }
             # If we're Removing a Group
             if ("remove" == $this->input->post('action')) {
                 # Remove the value if in the resource
                 if (in_array($this->input->post('value'), $contact_group)) {
                     $index = array_search($this->input->post('value'), $contact_group);
                     unset($contact_group[$index]);
                 } else {
                     $data = array('message' => 'Contact is already not in this group', 'type' => 'danger');
                     echo json_encode($data);
                     exit;
                 }
             }
             # Prepare data
             $contact_group = arraytoimplode($contact_group);
             // stringify the array E.g. `array("1", "2")` will be `"1,2"`
             $contact_data = array($this->input->post('updating_group') => $contact_group);
             # Update
             $this->Contact->update($id, $contact_data);
             # Response
             $data = array('message' => 'Contact was successfully updated', 'type' => 'success');
             echo json_encode($data);
             exit;
         }
         if (null != $this->input->post('updating_level')) {
             $contact = $this->Contact->find($id);
             $contact_level = [];
             if (!empty($contact->contacts_level)) {
                 $contact_level = explode(",", $contact->contacts_level);
             }
             # If we're Adding a Group
             if ("add" == $this->input->post('action')) {
                 # Check if the value is already in the resource,
                 # add to array if not.
                 if (!in_array($this->input->post('value'), $contact_level)) {
                     $contact_level[] = $this->input->post('value');
                 } else {
                     $data = array('message' => 'Contact is already in this level', 'type' => 'danger');
                     echo json_encode($data);
                     exit;
                 }
             }
             # If we're Removing a Group
             if ("remove" == $this->input->post('action')) {
                 # Remove the value if in the resource
                 if (in_array($this->input->post('value'), $contact_level)) {
                     $index = array_search($this->input->post('value'), $contact_level);
                     unset($contact_level[$index]);
                 } else {
                     $data = array('message' => 'Contact is already not in this level', 'type' => 'danger');
                     echo json_encode($data);
                     exit;
                 }
             }
             # Prepare data
             $contact_level = implode(",", $contact_level);
             // stringify the array E.g. `array("1", "2")` will be `"1,2"`
             $contact_data = array($this->input->post('updating_level') => $contact_level);
             # Update
             $this->Contact->update($id, $contact_data);
             # Response
             $data = array('message' => 'Contact was successfully updated', 'type' => 'success');
             echo json_encode($data);
             exit;
         }
         if (null != $this->input->post('updating_type')) {
             $contact = $this->Contact->find($id);
             $contact_type = [];
             if (!empty($contact->contacts_type)) {
                 $contact_type = explode(",", $contact->contacts_type);
             }
             # If we're Adding a Type
             if ("add" == $this->input->post('action')) {
                 # Check if the value is already in the resource,
                 # add to array if not.
                 if (!in_array($this->input->post('value'), $contact_type)) {
                     $contact_type[] = $this->input->post('value');
                 } else {
                     $data = array('message' => 'Contact is already in this type', 'type' => 'danger', 'debug' => $contact_type);
                     echo json_encode($data);
                     exit;
                 }
             }
             # If we're Removing a Type
             if ("remove" == $this->input->post('action')) {
                 # Remove the value if in the resource
                 if (in_array($this->input->post('value'), $contact_type)) {
                     $index = array_search($this->input->post('value'), $contact_type);
                     unset($contact_type[$index]);
                 } else {
                     $data = array('message' => 'Contact is already not in this type', 'type' => 'danger');
                     echo json_encode($data);
                     exit;
                 }
             }
             # Prepare data
             $contact_type = implode(",", $contact_type);
             // stringify the array E.g. `array("1", "2")` will be `"1,2"`
             $contact_data = array($this->input->post('updating_type') => $contact_type);
             # Update
             $this->Contact->update($id, $contact_data);
             # Response
             $data = array('message' => 'Contact was successfully updated', 'type' => 'success');
             echo json_encode($data);
             exit;
         }
         if ($this->Contact->validate(false, $id, $this->input->post('contacts_email'))) {
             /*
             | --------------------------------------
             | # Update
             | --------------------------------------
             */
             $contact = array('contacts_firstname' => $this->input->post('contacts_firstname'), 'contacts_middlename' => $this->input->post('contacts_middlename'), 'contacts_lastname' => $this->input->post('contacts_lastname'), 'contacts_level' => arraytoimplode($this->input->post('contacts_level')), 'contacts_type' => arraytoimplode($this->input->post('contacts_type')), 'contacts_blockno' => $this->input->post('contacts_blockno'), 'contacts_street' => $this->input->post('contacts_street'), 'contacts_brgy' => $this->input->post('contacts_brgy'), 'contacts_city' => $this->input->post('contacts_city'), 'contacts_zip' => $this->input->post('contacts_zip'), 'contacts_telephone' => $this->input->post('contacts_telephone'), 'contacts_mobile' => $this->input->post('contacts_mobile'), 'contacts_email' => $this->input->post('contacts_email'), 'contacts_group' => arraytoimplode($this->input->post('contacts_group')));
             $this->Contact->update($id, $contact);
             $data = array('message' => 'Contact was successfully updated', 'type' => 'success', 'debug' => $contact);
             echo json_encode($data);
             exit;
         } else {
             echo json_encode(['message' => $this->form_validation->toArray(), 'type' => 'error']);
             exit;
         }
     }
 }
 /**
  * Updates the resource
  *
  * @param  INT $id
  * @return JSON or Redirect
  */
 public function update($id)
 {
     if ($this->PrivilegesLevel->validate(false, $id, $this->input->post('code'))) {
         # Update
         $privilegeLevel = array('name' => $this->input->post('name'), 'code' => $this->input->post('code'), 'description' => $this->input->post('description'), 'modules' => arraytoimplode($this->input->post('modules')), 'updated_by' => $this->user_id);
         $this->PrivilegesLevel->update($id, $privilegeLevel);
         # Response
         $data = array('message' => 'Privileges Level was successfully updated', 'type' => 'success');
     } else {
         $data = array('message' => $this->form_validation->toArray(), 'type' => 'error');
     }
     if ($this->input->is_ajax_request()) {
         echo json_encode($data);
         exit;
     } else {
         $this->session->set_flashdata('message', $data);
     }
 }
Exemplo n.º 3
0
 public function add()
 {
     if ($this->input->is_ajax_request()) {
         /*
         | -------------------------------------
         | # Debug
         | -------------------------------------
         */
         // $data['message'] = $this->input->post();
         // $data['type'] = 'success';
         // echo json_encode($data); exit();
         /*
         | --------------------------------------
         | # Validation
         | --------------------------------------
         */
         if ($this->Type->validate(true)) {
             /*
             | --------------------------------------
             | # Save
             | --------------------------------------
             */
             $type = array('types_name' => $this->input->post('types_name'), 'types_description' => $this->input->post('types_description'), 'types_code' => $this->input->post('types_code'));
             $type_id = $this->Type->insert($type);
             /*
             | --------------------------------------
             | # Save the Contacts Types
             | --------------------------------------
             */
             if (null !== $this->input->post('types_contacts') && ($contacts_ids = $this->input->post('types_contacts'))) {
                 $types_contacts = explode(",", $contacts_ids);
                 foreach ($types_contacts as $contact_id) {
                     $contact = $this->Contact->find($contact_id);
                     $contact_type = explode(",", $contact->contacts_type);
                     # Check if the value is already in the resource,
                     # add to array if not.
                     if (!in_array($type_id, $contact_type)) {
                         $contact_type[] = $type_id;
                     } else {
                         $data = array('message' => 'Contact is already in this type', 'type' => 'danger');
                         echo json_encode($data);
                         exit;
                     }
                     $contact_type = arraytoimplode($contact_type);
                     $this->Contact->update($contact_id, array('contacts_type' => $contact_type));
                 }
             }
             /*
             | ----------------------------------------
             | # Response
             | ----------------------------------------
             */
             $data = array('message' => 'Type was successfully added', 'type' => 'success');
             echo json_encode($data);
             exit;
         } else {
             echo json_encode(['message' => $this->form_validation->toArray(), 'type' => 'danger']);
             exit;
         }
     } else {
         redirect(base_url('types'));
     }
 }
Exemplo n.º 4
0
 public function update($id)
 {
     if ($this->input->is_ajax_request()) {
         /*
         | --------------------------------------
         | # Validation
         | --------------------------------------
         */
         if ($this->Group->validate(false, $id, $this->input->post('groups_code'))) {
             /*
             | --------------------------------------
             | # Update
             | --------------------------------------
             */
             $group = array('groups_name' => $this->input->post('groups_name'), 'groups_description' => $this->input->post('groups_description'), 'groups_code' => $this->input->post('groups_code'), 'updated_by' => $this->user_id);
             $this->Group->update($id, $group);
             # Look up any member that shouldn't be in the group anymore
             $old_members_of_this_group = $this->GroupMember->lookup('group_id', $id)->result_array();
             foreach ($old_members_of_this_group as $group_members) {
                 $old_member = $this->Member->find($group_members['member_id']);
                 $old_member_groups = [];
                 if (!empty($old_member->groups)) {
                     $old_member_groups = explode(",", $old_member->groups);
                 }
                 if (!in_array($old_member->id, explodetoarray($this->input->post('groups_members')))) {
                     $index = array_search($group_members['group_id'], $old_member_groups);
                     unset($old_member_groups[$index]);
                 }
                 $old_member_groups = arraytoimplode($old_member_groups);
                 // implode to string
                 # Update the member
                 $this->Member->update($old_member->id, array('groups' => $old_member_groups, 'updated_by' => $this->user_id));
             }
             # Update the members.groups
             $members = $this->Member->find(explodetoarray($this->input->post('groups_members')));
             $this->GroupMember->delete($id);
             foreach ($members->result_array() as $member) {
                 $member_groups = [];
                 if (!empty($member['groups'])) {
                     $member_groups = explode(",", $member['groups']);
                 }
                 if (!in_array($id, $member_groups)) {
                     $member_groups[] = $id;
                 }
                 $member_groups = arraytoimplode($member_groups);
                 // implode to string
                 # Update the member
                 $this->Member->update($member['id'], array('groups' => $member_groups, 'updated_by' => $this->user_id));
                 # Update the group_members
                 $group_ids = explodetoarray($member_groups);
                 $this->GroupMember->delete_member($member['id']);
                 foreach ($group_ids as $group_id) {
                     $this->GroupMember->insert(array('group_id' => $group_id, 'member_id' => $member['id']));
                 }
             }
             $data = array('message' => 'Group was successfully updated', 'type' => 'success', 'debug-2' => $old_members_of_this_group);
             echo json_encode($data);
             exit;
         } else {
             echo json_encode(['message' => $this->form_validation->toArray(), 'type' => 'danger']);
             exit;
         }
     }
 }
Exemplo n.º 5
0
 public function update($id)
 {
     # Validation
     if (null != $this->input->post('updating_groups')) {
         $member = $this->Member->find($id);
         $member_groups = [];
         if (!empty($member->groups)) {
             $member_groups = explode(",", $member->groups);
         }
         # If we're Adding a Group
         if ("add" == $this->input->post('action')) {
             # Check if the value is already in the resource,
             # add to array if not.
             if (!in_array($this->input->post('value'), $member_groups)) {
                 $member_groups[] = $this->input->post('value');
             } else {
                 $data = array('message' => 'Member is already in this groups', 'type' => 'danger');
                 echo json_encode($data);
                 exit;
             }
         }
         # If we're Removing a Group
         if ("remove" == $this->input->post('action')) {
             # Remove the value if in the resource
             if (in_array($this->input->post('value'), $member_groups)) {
                 $index = array_search($this->input->post('value'), $member_groups);
                 unset($member_groups[$index]);
             } else {
                 $data = array('message' => 'Member is already not in this groups', 'type' => 'danger');
                 echo json_encode($data);
                 exit;
             }
         }
         # Prepare data
         $member_groups = arraytoimplode($member_groups);
         // stringify the array E.g. `array("1", "2")` will be `"1,2"`
         $member_data = array($this->input->post('updating_groups') => $member_groups);
         # Update
         $this->Member->update($id, $member_data);
         # Update the group_members
         $group_ids = explodetoarray($member_groups);
         $members_groups = $this->GroupMember->lookup('member_id', $id)->result_array();
         $this->GroupMember->delete_member($id);
         foreach ($group_ids as $group_id) {
             $this->GroupMember->insert(array('group_id' => $group_id, 'member_id' => $id));
         }
         # Response
         $data = array('message' => 'Member was successfully updated', 'type' => 'success');
         echo json_encode($data);
         exit;
     }
     if (null != $this->input->post('updating_level')) {
         $member = $this->Member->find($id);
         $member_level = [];
         if (!empty($member->level)) {
             $member_level = explode(",", $member->level);
         }
         # If we're Adding a Group
         if ("add" == $this->input->post('action')) {
             # Check if the value is already in the resource,
             # add to array if not.
             if (!in_array($this->input->post('value'), $member_level)) {
                 $member_level[] = $this->input->post('value');
             } else {
                 $data = array('message' => 'Member is already in this level', 'type' => 'danger');
                 echo json_encode($data);
                 exit;
             }
         }
         # If we're Removing a Group
         if ("remove" == $this->input->post('action')) {
             # Remove the value if in the resource
             if (in_array($this->input->post('value'), $member_level)) {
                 $index = array_search($this->input->post('value'), $member_level);
                 unset($member_level[$index]);
             } else {
                 $data = array('message' => 'Member is already not in this level', 'type' => 'danger');
                 echo json_encode($data);
                 exit;
             }
         }
         # Prepare data
         $member_level = implode(",", $member_level);
         // stringify the array E.g. `array("1", "2")` will be `"1,2"`
         $member_data = array($this->input->post('updating_level') => $member_level);
         # Update
         $this->Member->update($id, $member_data);
         # Response
         $data = array('message' => 'Member was successfully updated', 'type' => 'success');
         echo json_encode($data);
         exit;
     }
     if (null != $this->input->post('updating_type')) {
         $member = $this->Member->find($id);
         $member_type = [];
         if (!empty($member->type)) {
             $member_type = explode(",", $member->type);
         }
         # If we're Adding a Type
         if ("add" == $this->input->post('action')) {
             # Check if the value is already in the resource,
             # add to array if not.
             if (!in_array($this->input->post('value'), $member_type)) {
                 $member_type[] = $this->input->post('value');
             } else {
                 $data = array('message' => 'Member is already in this type', 'type' => 'danger', 'debug' => $member_type);
                 echo json_encode($data);
                 exit;
             }
         }
         # If we're Removing a Type
         if ("remove" == $this->input->post('action')) {
             # Remove the value if in the resource
             if (in_array($this->input->post('value'), $member_type)) {
                 $index = array_search($this->input->post('value'), $member_type);
                 unset($member_type[$index]);
             } else {
                 $data = array('message' => 'Member is already not in this type', 'type' => 'danger');
                 echo json_encode($data);
                 exit;
             }
         }
         # Prepare data
         $member_type = implode(",", $member_type);
         // stringify the array E.g. `array("1", "2")` will be `"1,2"`
         $member_data = array($this->input->post('updating_type') => $member_type);
         # Update
         $this->Member->update($id, $member_data);
         # Response
         $data = array('message' => 'Member was successfully updated', 'type' => 'success');
         echo json_encode($data);
         exit;
     }
     if ($this->Member->validate(false, $id, $this->input->post('email'))) {
         $avatar = $this->session->members_photo ? $this->session->members_photo : "";
         if (!empty($avatar)) {
             unlink($this->Member->find($id)->avatar);
         }
         # Update
         $member = array('stud_no' => $this->input->post('stud_no'), 'firstname' => $this->input->post('firstname'), 'middlename' => $this->input->post('middlename'), 'lastname' => $this->input->post('lastname'), 'level' => arraytoimplode($this->input->post('level')), 'type' => arraytoimplode($this->input->post('type')), 'address_blockno' => $this->input->post('address_blockno'), 'address_street' => $this->input->post('address_street'), 'address_brgy' => $this->input->post('address_brgy'), 'address_city' => $this->input->post('address_city'), 'address_zip' => $this->input->post('address_zip'), 'telephone' => $this->input->post('telephone'), 'msisdn' => $this->input->post('msisdn'), 'email' => $this->input->post('email'), 'groups' => arraytoimplode($this->input->post('groups')), 'updated_by' => $this->user_id, 'avatar' => $avatar, 'schedule_id' => $this->input->post('schedule_id'));
         $this->Member->update($id, $member);
         # Update the group_members
         $group_ids = $this->input->post('groups');
         $members_groups = $this->GroupMember->lookup('member_id', $id)->result_array();
         $this->GroupMember->delete_member($id);
         if (null !== $group_ids) {
             foreach ($group_ids as $group_id) {
                 $this->GroupMember->insert(array('group_id' => $group_id, 'member_id' => $id));
             }
         } else {
             $this->GroupMember->delete_member($id);
         }
         # Response
         $data = array('message' => 'Member was successfully updated', 'type' => 'success', 'debug' => $member);
     } else {
         $data = array('message' => $this->form_validation->toArray(), 'type' => 'error');
     }
     if ($this->input->is_ajax_request()) {
         echo json_encode($data);
         exit;
     } else {
         $this->session->set_flashdata('message', $data);
     }
 }