/applications/pm/models/profile_model.php

https://bitbucket.org/amitholkar/zenfile-18-05 · PHP · 196 lines · 145 code · 22 blank · 29 comment · 14 complexity · 603228d910642035a616bc511d206404 MD5 · raw file

  1. <?php if (!defined('BASEPATH')) exit('No direct script access allowed');
  2. class Profile_model extends CI_Model
  3. {
  4. function __construct()
  5. {
  6. parent::__construct();
  7. }
  8. /**
  9. * Returns customer's profile data
  10. *
  11. * @access public
  12. * @author Sergey Koshkarev <koshkarev.ss@gmail.com>
  13. * @return mixed
  14. */
  15. public function get_profile_info()
  16. {
  17. $customer_id = $this->session->userdata('client_user_id');
  18. $this->db->where('id', $customer_id);
  19. $query = $this->db->get('customers');
  20. if ($query->num_rows()) {
  21. return $query->row_array();
  22. }
  23. return NULL;
  24. }
  25. /**
  26. * Updates customer's profile
  27. *
  28. * @access public
  29. * @author Sergey Koshkarev <koshkarev.ss@gmail.com>
  30. * @return mixed
  31. */
  32. public function update_profile()
  33. {
  34. $data = array(
  35. 'email' => $this->input->post('email'),
  36. 'firstname' => $this->input->post('first_name'),
  37. 'lastname' => $this->input->post('last_name'),
  38. 'company_name' => $this->input->post('company_name'),
  39. 'address' => $this->input->post('address'),
  40. 'address2' => $this->input->post('address2'),
  41. 'city' => $this->input->post('city'),
  42. 'state' => $this->input->post('state'),
  43. 'zip_code' => $this->input->post('zip_code'),
  44. 'country' => $this->input->post('country'),
  45. 'phone_number' => $this->input->post('phone_number'),
  46. 'phone_country_code' => $this->input->post('phone_country_code'),
  47. 'ext' => $this->input->post('ext'),
  48. 'fax' => $this->input->post('fax')
  49. );
  50. $customer_id = $this->session->userdata('client_user_id');
  51. $this->db->where('id', $customer_id);
  52. $this->db->update('customers', $data);
  53. $affected = $this->db->affected_rows();
  54. if ($affected > 0) {
  55. $this->session->set_userdata('client_lastname', $this->input->post('last_name'));
  56. $this->session->set_userdata('client_firstname', $this->input->post('first_name'));
  57. return TRUE;
  58. }
  59. return FALSE;
  60. }
  61. public function get_google_address_data()
  62. {
  63. $fresult = array();
  64. $search_string = $this->input->post('q');
  65. $address = urlencode($search_string);
  66. $url = 'http://maps.googleapis.com/maps/api/geocode/json?address=' . $address . '&sensor=true';
  67. $user_agent = 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.1 (KHTML, like Gecko) Chrome/14.0.835.202 Safari/535.1';
  68. $ch = curl_init($url);
  69. curl_setopt($ch, CURLOPT_URL, $url); // set url to post to
  70. curl_setopt($ch, CURLOPT_USERAGENT, $user_agent); // allow redirects
  71. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // return into a variable
  72. curl_setopt($ch, CURLOPT_HEADER, FALSE);
  73. curl_setopt($ch, CURLOPT_TIMEOUT, 10); // times out after Ns
  74. $raw_result = curl_exec($ch); // run the whole process
  75. $zip_code = '';
  76. $result = json_decode($raw_result);
  77. if ($result->status == 'OK') {
  78. // If we have just 1 result from Google Maps API
  79. if (is_array($result->results) && count($result->results) == 1) {
  80. if (is_array($address_components = $result->results[0]->address_components)) {
  81. $address = $address_components[0]->long_name . ' ' . $address_components[1]->long_name;
  82. $city = $address_components[4]->long_name; // City
  83. $country = $address_components[7]->long_name; // Country
  84. $postal_code = $address_components[8]->long_name; // Postal Code
  85. $formatted_address = $result->results[0]->formatted_address;
  86. $fresult['city'] = $city;
  87. $fresult['zip_code'] = $zip_code;
  88. $fresult['country'] = $country;
  89. $fresult['address'] = $address;
  90. $fresult['formatted_address'] = $formatted_address;
  91. }
  92. }
  93. elseif (is_array($result->results) && count($result->results) > 1)
  94. {
  95. echo '<pre>';
  96. print_r($result->results);
  97. echo '</pre>';
  98. exit();
  99. $index = 0;
  100. foreach ($result->results as $result_entry)
  101. {
  102. if (is_array($address_components = $result_entry->address_components)) {
  103. $address = $address_components[0]->long_name . ' ' . $address_components[1]->long_name;
  104. $city = $address_components[4]->long_name; // City
  105. $country = $address_components[7]->long_name; // Country
  106. $zip_code = isset($address_components[8]->long_name) ? $address_components[8]->long_name : ''; // Postal Code
  107. $formatted_address = $result_entry->formatted_address;
  108. $fresult[$index]['city'] = $city;
  109. $fresult[$index]['zip_code'] = $zip_code;
  110. $fresult[$index]['country'] = $country;
  111. $fresult[$index]['address'] = $address;
  112. $fresult[$index]['formatted_address'] = $formatted_address;
  113. $index++;
  114. }
  115. }
  116. }
  117. }
  118. if (is_ajax()) {
  119. echo json_encode($fresult);
  120. }
  121. else
  122. {
  123. return $fresult;
  124. }
  125. }
  126. public function get_bdv()
  127. {
  128. $this->db->select('managers.*');
  129. $this->db->join('customers', 'customers.bdv_id = managers.id');
  130. $this->db->where('customers.id', $this->session->userdata('client_user_id'));
  131. $query = $this->db->get('managers');
  132. if ($query->num_rows()) {
  133. return $query->row_array();
  134. }
  135. return NULL;
  136. }
  137. /*
  138. * Returns data for current PM
  139. *
  140. * @access public
  141. * @author Sergey Koshkarev <koshkarev.ss@gmail.com>
  142. * @return mixed
  143. */
  144. public function get_pm_profile()
  145. {
  146. $user_id = $this->session->userdata('manager_user_id');
  147. $this->db->where('id', $user_id);
  148. $query = $this->db->get('managers');
  149. if ($query->num_rows()) {
  150. return $query->row_array();
  151. }
  152. return NULL;
  153. }
  154. /**
  155. * Updates PM profile
  156. *
  157. * @access public
  158. * @author Sergey Koshkarev <koshkarev.ss@gmail.com>
  159. * @return void
  160. */
  161. public function update_pm_profile()
  162. {
  163. $data = array(
  164. 'firstname' => $this->input->post('firstname'),
  165. 'lastname' => $this->input->post('lastname'),
  166. 'email' => $this->input->post('email'),
  167. 'phone' => $this->input->post('phone'),
  168. );
  169. $user_id = $this->session->userdata('manager_user_id');
  170. $this->db->where('id', $user_id);
  171. $this->db->update('managers', $data);
  172. }
  173. }
  174. ?>