public function post_comment($sid = 0, $uid = 0, $com = "")
 {
     // set return result
     $result = array("status" => false, "message" => "", "data" => array());
     $user = $this->is_valid_user($uid);
     if ($user) {
         if ($this->is_valid_eco_system_id($sid)) {
             if (!empty($com)) {
                 $data = array('eco_sys_id' => $sid, 'posted_by' => $uid, 'comment' => $com, 'added_on' => c_now());
                 if ($this->db->insert('eco_discussion_comments', $data)) {
                     $since = c_get_time_elapsed(strtotime(c_now()));
                     $user_image = get_profile_pic($user['id'], $user['profile_pic']);
                     $result['status'] = true;
                     $result['message'] = "Comment posted successfully.";
                     $result['data']['user']['name'] = $user['name'];
                     $result['data']['user']['profile_pic'] = $user_image;
                     $result['data']['comment_data'] = array();
                     $result['data']['comment_data']['comment'] = $com;
                     $result['data']['comment_data']['added_on'] = c_now();
                     $result['data']['comment_data']['since'] = is_null($since) ? '2 seconds ago' : $since;
                 } else {
                     $result['message'] = 'System error.';
                 }
             } else {
                 $result['message'] = "Empty comment.";
             }
         } else {
             $result['message'] = "Invalid eco system id.";
         }
     } else {
         $result['message'] = "Invalid user.";
     }
     return $result;
 }
 public function mark_fuf($sid = 0, $uid = 0)
 {
     $sql = "SELECT * FROM eco_members WHERE eco_sys_id=? AND user_id=?";
     $rsl = $this->db->query($sql, array($sid, $uid));
     if ($rsl->num_rows() > 0) {
         $sql = "DELETE FROM eco_members WHERE eco_sys_id=? AND user_id=?";
         $this->db->query($sql, array($sid, $uid));
         return true;
     } else {
         $data = array("eco_sys_id" => $sid, "user_id" => $uid, "joined_on" => c_now());
         $this->db->insert('eco_members', $data);
         return true;
     }
     return false;
 }
 public function purge_voice($voice = array())
 {
     $data = array("original_voice_id" => $voice["id"], "user_id" => $voice["user_id"], "voice_cat_id" => $voice["voice_cat_id"], "voice_tag_ids" => $voice["voice_tag_ids"], "question_text" => $voice["question_text"], "voice_details" => $voice["voice_details"], "voice_pic" => $voice["voice_pic"], "added_on" => $voice["added_on"], "dumped_on" => c_now());
     // dump voice data
     $insert = $this->db->insert("voices_dump", $data);
     if ($insert) {
         // select voice votes by voice id
         $sql = "SELECT * FROM voices_votes WHERE voice_id=?";
         $rsl = $this->db->query($sql, array($voice["id"]));
         foreach ($rsl->result_array() as $vote) {
             $data = array("id" => $vote["id"], "voice_id" => $vote["voice_id"], "user_id" => $vote["user_id"], "vote_value" => $vote["vote_value"], "voted_on" => $vote["voted_on"]);
             // insert votes dump data
             $this->db->insert("voices_votes_dump", $data);
             // delete vote from voices votes
             $sql = "DELETE FROM voices_votes WHERE id=?";
             $this->db->query($sql, array($vote["id"]));
         }
         // delete voice
         $sql = "DELETE FROM user_voices WHERE id=?";
         $this->db->query($sql, array($voice["id"]));
     }
 }
 public function create_river()
 {
     // get var in post
     $title = rtrim(ltrim($this->input->post('voc_title', true)));
     $desc = $this->input->post('voc_desc', true);
     $tags = $this->input->post('voc_tags', true);
     $cat_id = $this->input->post('voc_cat', true);
     $rc_key = $this->input->post('rc_key', true);
     $user_id = $this->get_logged_uid();
     if ($this->is_valid_river_key($user_id, $rc_key)) {
         // check title is not empty
         if (!empty($title)) {
             // check voice is valid
             if ($this->is_valid_voice_cat($cat_id)) {
                 // decode to array
                 $tags = @json_decode($tags);
                 // check tags is array
                 if (is_array($tags)) {
                     $filter_tags_data = $this->filter_tags($tags);
                     if ($filter_tags_data['status'] == true) {
                         $tags_id = json_encode($filter_tags_data['data']);
                         // upload file and return data where error or success
                         $upload_image = $this->_upload_eco_system_image();
                         if ($upload_image["status"] == true) {
                             // check valid user
                             if ($user_id) {
                                 $image_data = $upload_image["upload_data"];
                                 // set inserting data
                                 $river_data = array("level" => 2, "moderator_id" => $user_id, "voice_cat_id" => $cat_id, "voice_tag_ids" => $tags_id, "title" => $title, "description" => $desc, "eco_pic" => $image_data->file_name, "created_on" => c_now());
                                 // insert voice to voice table
                                 if ($this->db->insert('eco_system', $river_data)) {
                                     $last_river_data = $this->get_last_river($user_id);
                                     $get_merged_data = $this->get_merged_request($user_id, $rc_key);
                                     $data = array("parent_id" => $last_river_data["id"]);
                                     // update caller eco system
                                     $this->db->where('id', $get_merged_data["caller_eco_sys_id"]);
                                     $this->db->update('eco_system', $data);
                                     // update receiver eco system
                                     $this->db->where('id', $get_merged_data["receiver_eco_sys_id"]);
                                     $this->db->update('eco_system', $data);
                                     // delete caller  merge request
                                     $this->db->where("(caller_eco_sys_id='{$get_merged_data[caller_eco_sys_id]}' OR caller_eco_sys_id='{$get_merged_data[receiver_eco_sys_id]}')");
                                     $this->db->delete('eco_merge_requests');
                                     // delete receiver merge request
                                     $this->db->where("(receiver_eco_sys_id='{$get_merged_data[caller_eco_sys_id]}' OR receiver_eco_sys_id='{$get_merged_data[receiver_eco_sys_id]}')");
                                     $this->db->delete('eco_merge_requests');
                                     // voice successfully created
                                     $this->_set_message(self::SUCCESS, "Your River successfully created.");
                                     redirect(base_url() . $this->_config["my_rivers_url"]);
                                 } else {
                                     $this->_set_message(self::ERROR, "River can't created.");
                                 }
                             } else {
                                 // invalid user login
                                 $this->_set_message(self::ERROR, "You are not valid user.");
                             }
                         } else {
                             $this->_set_message(self::ERROR, $upload_image["error"]);
                         }
                     } else {
                         $this->_set_message(self::ERROR, $filter_tags_data['message']);
                         redirect(base_url() . $this->_config["river_create_url"] . '/0');
                         exit("Unathorized user.");
                     }
                 } else {
                     $this->_set_message(self::ERROR, "Voice Tags invalid format.");
                 }
             } else {
                 $this->_set_message(self::ERROR, "Voice Category invalid.");
             }
         } else {
             $this->_set_message(self::ERROR, "Voice Title can't be blank.");
         }
     } else {
         $this->_set_message(self::ERROR, "Invalid river creation.");
     }
     redirect(base_url() . $this->_config["river_create_url"] . '/0');
 }
 public function add_request_to_merge($req_id = 0, $caller_sys_id = 0, $receiver_sys_id = 0)
 {
     $result = array("status" => false, "message" => "", "data" => array());
     if ($this->is_valid_user($req_id)) {
         if ($this->is_valid_eco_system_id($caller_sys_id) && $this->is_valid_eco_system_id($receiver_sys_id)) {
             $data = array('requested_by' => $req_id, 'caller_eco_sys_id' => $caller_sys_id, 'receiver_eco_sys_id' => $receiver_sys_id, 'added_on' => c_now());
             if ($this->db->insert('eco_merge_requests', $data)) {
                 $sql = "SELECT * FROM eco_merge_requests ORDER BY id DESC LIMIT 1";
                 $rsl = $this->db->query($sql);
                 $result['status'] = true;
                 $result['message'] = "Request added.";
                 $result['data'] = $rsl->row_array();
             } else {
                 $result['message'] = "System error.";
             }
         }
     } else {
         $result['message'] = "Invalid user.";
     }
     return $result;
 }
 public function create_voice()
 {
     // get var in post
     $title = rtrim(ltrim($this->input->post('voc_title', true)));
     $desc = $this->input->post('voc_desc', true);
     $tags = $this->input->post('voc_tags', true);
     $cat_id = $this->input->post('voc_cat', true);
     // check title is not empty
     if (!empty($title)) {
         // check voice is valid
         if ($this->is_valid_voice_cat($cat_id)) {
             // decode to array
             $tags = @json_decode($tags);
             // check tags is array
             if (is_array($tags)) {
                 $tags_id = json_encode($this->_filter_tags($tags));
                 // upload file and return data where error or success
                 $upload_image = $this->_upload_voice_image();
                 if ($upload_image["status"] == true) {
                     $user_id = $this->get_logged_uid();
                     // check valid user
                     if ($user_id) {
                         $image_data = $upload_image["upload_data"];
                         // set inserting data
                         $voice_data = array("user_id" => $user_id, "voice_cat_id" => $cat_id, "voice_tag_ids" => $tags_id, "question_text" => $title, "voice_details" => $desc, "voice_pic" => $image_data->file_name, "added_on" => c_now());
                         // insert voice to voice table
                         $this->db->insert('user_voices', $voice_data);
                         // generate patronage points
                         generate_ppoints($user_id, 'create_voice');
                         // voice successfully created
                         $this->_set_message(self::SUCCESS, "Your voice successfully created.");
                     } else {
                         // invalid user login
                         $this->_set_message(self::ERROR, "You are not valid user.");
                     }
                 } else {
                     $this->_set_message(self::ERROR, $upload_image["error"]);
                 }
             } else {
                 $this->_set_message(self::ERROR, "Voice Tags invalid format.");
             }
         } else {
             $this->_set_message(self::ERROR, "Voice Category invalid.");
         }
     } else {
         $this->_set_message(self::ERROR, "Voice Title can't be blank.");
     }
     redirect(base_url() . $this->_config["my_voices_url"]);
 }
 public function filter_tags($tags = array())
 {
     // set return result
     $result = array("status" => true, "message" => "", "data" => array());
     foreach ($tags as $tag) {
         $sql = "SELECT * FROM voice_tags WHERE tag=?";
         $query = $this->db->query($sql, array(strtolower($tag)));
         // if tag is already found simple get this id
         if ($query->num_rows() > 0) {
             $rsl = (object) $query->row_array();
             // push tag id to return tags id array
             array_push($result['data'], $rsl->id);
         } else {
             $user_id = $this->get_logged_uid();
             if ($user_id) {
                 $data = array('user_id' => $user_id, 'tag' => $tag, 'added_on' => c_now());
                 $this->db->insert('voice_tags', $data);
                 $sql = "SELECT * FROM voice_tags ORDER BY id DESC LIMIT 1";
                 $query = $this->db->query($sql);
                 if ($query->num_rows() > 0) {
                     $row = (object) $query->row_array();
                     // push tag id to return tags id array
                     array_push($result['data'], $row->id);
                 }
             } else {
                 // invalid user login
                 $result['status'] = false;
                 $result['message'] = 'You are not valid user.';
             }
         }
     }
     // return tags id
     return $result;
 }
 public function vote_cast($uid = 0, $vid = 0, $vval = "")
 {
     $result = array("status" => false, "message" => "", "data" => array());
     // check valid user
     $user = $this->is_valid_user($uid);
     if ($user) {
         // check valid voice
         if ($this->is_valid_voice($vid)) {
             // check vote value
             if ($this->is_valid_vote_value($vval)) {
                 $sql = "SELECT * FROM voices_votes WHERE user_id=? AND voice_id=?";
                 $rsl = $this->db->query($sql, array($uid, $vid));
                 // check already vote casted
                 if ($rsl->num_rows() < 1) {
                     $data = array("voice_id" => $vid, "user_id" => $uid, "vote_value" => $vval, "voted_on" => c_now());
                     if ($this->db->insert("voices_votes", $data)) {
                         $voice = $this->get_single_voice($vid);
                         $result["message"] = "Vote successfully cast.";
                         $result["status"] = true;
                         $result["data"]["vote"] = $this->get_last_vote();
                         $result["data"]["voice"] = $voice["data"];
                         // generate patronage points
                         if ($vval == 'i_see') {
                             generate_ppoints($uid, 'vote_yes');
                         } else {
                             if ($vval == 'i_dont_see') {
                                 generate_ppoints($uid, 'vote_no');
                             }
                         }
                     } else {
                         $result["message"] = "vote insertion failed.";
                     }
                 } else {
                     $result["message"] = "Already vote casted.";
                 }
             } else {
                 $result["message"] = "Invalid vote value.";
             }
         } else {
             $result["message"] = "{$vid} id can't exists in voices";
         }
     } else {
         $result["message"] = "Invalid user login.";
     }
     return $result;
 }