/halogy/application/modules/users/models/users_model.php

https://bitbucket.org/haloweb/halogy-1.0/ · PHP · 223 lines · 157 code · 40 blank · 26 comment · 18 complexity · 28d8d5c4601a5bed9480f5f802a8dd9a MD5 · raw file

  1. <?php
  2. /**
  3. * Halogy
  4. *
  5. * A user friendly, modular content management system for PHP 5.0
  6. * Built on CodeIgniter - http://codeigniter.com
  7. *
  8. * @package Halogy
  9. * @author Haloweb Ltd.
  10. * @copyright Copyright (c) 2008-2011, Haloweb Ltd.
  11. * @license http://halogy.com/license
  12. * @link http://halogy.com/
  13. * @since Version 1.0
  14. * @filesource
  15. */
  16. // ------------------------------------------------------------------------
  17. class Users_model extends Model {
  18. function Users_model()
  19. {
  20. parent::Model();
  21. // get siteID, if available
  22. if (defined('SITEID'))
  23. {
  24. $this->siteID = SITEID;
  25. }
  26. }
  27. function get_users($q = '')
  28. {
  29. $this->db->where(array('siteID' => $this->siteID));
  30. // tidy query
  31. $q = $this->db->escape_like_str($q);
  32. $name = @preg_split('/ /', $q);
  33. if (count($name) > 1)
  34. {
  35. $firstName = $name[0];
  36. $lastName = $name[1];
  37. $this->db->where('(email LIKE "%'.$q.'%" OR firstName LIKE "%'.$firstName.'%" AND lastName LIKE "%'.$lastName.'%")');
  38. }
  39. else
  40. {
  41. $this->db->where('(email LIKE "%'.$q.'%" OR firstName LIKE "%'.$q.'%" OR lastName LIKE "%'.$q.'%")');
  42. }
  43. $query = $this->db->get('users', 30);
  44. if ($query->num_rows() > 0)
  45. {
  46. return $query->result_array();
  47. }
  48. else
  49. {
  50. return false;
  51. }
  52. }
  53. function get_user($userID)
  54. {
  55. // default wheres
  56. if ($this->session->userdata('groupID') >= 0)
  57. {
  58. $this->db->where('siteID', $this->siteID);
  59. }
  60. $this->db->where('userID', $userID);
  61. // grab
  62. $query = $this->db->get('users', 1);
  63. if ($query->num_rows())
  64. {
  65. return $query->row_array();
  66. }
  67. else
  68. {
  69. return false;
  70. }
  71. }
  72. function get_avatar($filename)
  73. {
  74. $pathToAvatars = '/static/uploads/avatars/';
  75. if (is_file('.'.$pathToAvatars.$filename))
  76. {
  77. $avatar = $pathToAvatars.$filename;
  78. }
  79. else
  80. {
  81. $avatar = $pathToAvatars.'noavatar.gif';
  82. }
  83. return $avatar;
  84. }
  85. function import_csv($file)
  86. {
  87. $handle = fopen($file['tmp_name'], "r");
  88. if ($handle)
  89. {
  90. $allowedExtensions = array("txt", "csv");
  91. if (!in_array(end(explode(".", $file['name'])), $allowedExtensions))
  92. {
  93. $this->form_validation->set_error('The file was not a CSV.');
  94. return FALSE;
  95. }
  96. $array = @explode("\n", fread($handle, filesize($file['tmp_name'])));
  97. $total_array = count($array);
  98. if ($total_array > 0)
  99. {
  100. $i = 0;
  101. foreach ($array as $row)
  102. {
  103. $data = explode(",", $row);
  104. if ($data[0] != '')
  105. {
  106. // lookup user
  107. $query = $this->db->get_where('users', array('email' => trim($data[0])), 1);
  108. if ($query->num_rows() > 0)
  109. {
  110. // edit user
  111. $row = $query->row_array();
  112. if ($row['firstName'] == '' && $row['lastName'] == '')
  113. {
  114. $this->db->set('firstName', trim($data[1]));
  115. $this->db->set('lastName', trim($data[2]));
  116. $this->db->where('userID', $row['userID']);
  117. $this->db->update('users');
  118. $i++;
  119. }
  120. }
  121. else
  122. {
  123. // add new user providing email is valid
  124. if (!$this->form_validation->valid_email($data[0]))
  125. {
  126. $this->form_validation->set_error('<p>There was a badly formatted email address ('.$data[0].'), so the import could not complete. Please check the CSV file and try again.</p>');
  127. return false;
  128. }
  129. $username = url_title(strtolower($data[0]));
  130. $username = str_replace('.','',$username);
  131. $username = str_replace('-','',$username);
  132. $username = str_replace('_','',$username);
  133. $this->db->set('dateCreated', date("Y-m-d H:i:s"));
  134. $this->db->set('username', substr($username,0,6).$i.rand(100,999));
  135. $this->db->set('password', md5(rand(19999,49999)));
  136. $this->db->set('email', trim($data[0]));
  137. $this->db->set('firstName', trim($data[1]));
  138. $this->db->set('lastName', trim($data[2]));
  139. $this->db->set('siteID', $this->siteID);
  140. $this->db->insert('users');
  141. $i++;
  142. }
  143. }
  144. }
  145. return $i;
  146. }
  147. else
  148. {
  149. $this->form_validation->set_error('There didn\'t seem to be any rows in the CSV file.');
  150. return FALSE;
  151. }
  152. }
  153. else
  154. {
  155. $this->form_validation->set_error('There was a problem opening the file.');
  156. return FALSE;
  157. }
  158. }
  159. function export()
  160. {
  161. // default where
  162. $this->db->where('users.siteID', $this->siteID);
  163. $this->db->where('users.subscription !=', 'P');
  164. $this->db->where('users.subscription !=', 'N');
  165. $this->db->where('users.bounced', '0');
  166. // select
  167. $this->db->select('email as Email');
  168. $this->db->select(' CONCAT(firstName, " ", lastName) as Name', FALSE);
  169. // join
  170. $this->db->join('permission_groups', 'permission_groups.groupID = users.groupID', 'left');
  171. // order
  172. $this->db->order_by('dateCreated', 'asc');
  173. $query = $this->db->get('users');
  174. if ($query->num_rows() > 0)
  175. {
  176. return $query;
  177. }
  178. else
  179. {
  180. return FALSE;
  181. }
  182. }
  183. }