PageRenderTime 57ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/app/modules/users/models/user_model.php

https://bitbucket.org/nanomites_webdev/heroframework
PHP | 1656 lines | 911 code | 273 blank | 472 comment | 182 complexity | 6475090a07e999d2164255fac2a274ef MD5 | raw file
Possible License(s): LGPL-2.1, BSD-3-Clause, GPL-2.0
  1. <?php
  2. /**
  3. * User Model
  4. *
  5. * Contains all the methods used to create, update, login, logout, and delete users.
  6. *
  7. * @author Electric Function, Inc.
  8. * @copyright Electric Function, Inc.
  9. * @package Hero Framework
  10. *
  11. */
  12. class User_model extends CI_Model
  13. {
  14. public $active_user; // the logged-in use
  15. public $failed_due_to_activation; // if the login failed to the account not being activated, this == TRUE
  16. public $failed_due_to_duplicate_login; // if the login failed because someone is already logged in, this == TRUE
  17. // this will change if we are in the control panel, as we want to have independent sessions foreach
  18. // so a user can be logged in the CP but no the frontend (helps for testing - eases confusion)
  19. private $session_name = 'user_id';
  20. // should we trigger the member_register hook or create the user silently in new_user()?
  21. public $trigger_register_hook = TRUE;
  22. // are we in the control panel?
  23. private $in_admin = null;
  24. private $cache_fields;
  25. function __construct()
  26. {
  27. parent::__construct();
  28. if ($this->in_admin()) {
  29. // let's use a different session token so that we can independent sessions
  30. $this->make_admin_session();
  31. }
  32. // check for session
  33. if ($this->session->userdata($this->session_name) != '') {
  34. // load active user into cache for future ->Get() calls
  35. $this->set_active($this->session->userdata($this->session_name));
  36. // no carts in the control panel...
  37. if (!$this->in_admin() and module_installed('store')) {
  38. // handle a potential cart
  39. $CI =& get_instance();
  40. $CI->load->model('store/cart_model');
  41. $CI->cart_model->save_cart_to_db();
  42. }
  43. }
  44. else {
  45. // we don't have remember_keys for admins...
  46. if (!$this->in_admin()) {
  47. $this->load->helper('cookie');
  48. // we may have a remembered user
  49. if (get_cookie('user_remember_key',TRUE)) {
  50. // does this correspond with a remember key?
  51. $this->db->select('user_id');
  52. $this->db->where('user_remember_key', get_cookie('user_remember_key', TRUE));
  53. $result = $this->db->get('users');
  54. if ($result->num_rows() == 0) {
  55. // no correspondence, this key has expired
  56. delete_cookie('user_remember_key');
  57. $this->db->update('users',array('user_remember_key' => ''),array('user_remember_key' => get_cookie('user_remember_key', TRUE)));
  58. }
  59. else {
  60. $user = $result->row_array();
  61. $this->login_by_id($user['user_id']);
  62. }
  63. }
  64. }
  65. }
  66. }
  67. /**
  68. * CP Check
  69. *
  70. * Are we in the control panel?
  71. *
  72. * @return boolean
  73. */
  74. private function in_admin () {
  75. if ($this->in_admin != null) {
  76. return $this->in_admin;
  77. }
  78. $CI =& get_instance();
  79. $url = $CI->uri->uri_string();
  80. // it may be at 0 or 1 depending on if we retained the initial slash...
  81. if (strpos($url, 'admincp') === 0 or strpos($url, 'admincp') === 1) {
  82. $this->in_admin = TRUE;
  83. }
  84. else {
  85. $this->in_admin = FALSE;
  86. }
  87. return $this->in_admin;
  88. }
  89. /**
  90. * Make Admin Session
  91. *
  92. * Forces the model into an admin session
  93. *
  94. * @return void
  95. */
  96. function make_admin_session () {
  97. $this->session_name = 'admin_id';
  98. }
  99. /**
  100. * Make Frontend Session
  101. *
  102. * Forces the model into a user session
  103. *
  104. * @return void
  105. */
  106. function make_frontend_session () {
  107. $this->session_name = 'user_id';
  108. }
  109. /**
  110. * Login User
  111. *
  112. * Logs a user in, sets the $_SESSION, updates the user's last login, and tracks login
  113. *
  114. * @param string $username Either the username or email of the user
  115. * @param string $password Their password
  116. * @param boolean $remember Remember the user with a cookie to re-log them in at future visits (default: FALSE)
  117. *
  118. * @return boolean FALSE upon failure, TRUE upon success
  119. */
  120. public function login ($username, $password, $remember = FALSE) {
  121. $authenticated = FALSE;
  122. // stop SQL injection
  123. $username = addslashes($username);
  124. $this->db->where('(`user_username` = \'' . $username . '\' or `user_email` = \'' . $username . '\')');
  125. $this->db->where('user_suspended','0');
  126. $this->db->where('user_deleted','0');
  127. $query = $this->db->get('users');
  128. if ($query->num_rows() > 0) {
  129. $user_db = $query->row_array();
  130. $user = $this->get_user($user_db['user_id']);
  131. $hashed_password = ($user['salt'] == '') ? md5($password) : md5($password . ':' . $user['salt']);
  132. if ($hashed_password == $user_db['user_password']) {
  133. $authenticated = TRUE;
  134. }
  135. }
  136. if ($authenticated === TRUE) {
  137. if ($this->config->item('simultaneous_login_prevention') == '1') {
  138. // let's make sure someone isn't logged into the account right now
  139. $this->db->where('user_id',$user['id']);
  140. $this->db->where('user_activity_date >',date('Y-m-d H:i:s', time() - 60));
  141. $result = $this->db->get('user_activity');
  142. if ($result->num_rows() > 0) {
  143. $this->failed_due_to_duplicate_login = TRUE;
  144. return FALSE;
  145. }
  146. }
  147. // let's make sure they are activated if it's been more than 1 day
  148. if (!empty($user['validate_key']) and ((time() - strtotime($user['signup_date'])) > (60*60*24))) {
  149. $this->failed_due_to_activation = TRUE;
  150. return FALSE;
  151. }
  152. }
  153. else {
  154. return FALSE;
  155. }
  156. // track login
  157. $this->login_by_id($user['id'], $password);
  158. // remember?
  159. if ($remember == TRUE) {
  160. $remember_key = random_string('unique');
  161. $result = $this->db->select('user_id')->where('user_remember_key',$remember_key)->get('users');
  162. while ($result->num_rows() > 0) {
  163. $remember_key = random_string('unique');
  164. $result = $this->db->select('user_id')->where('user_remember_key',$remember_key)->get('users');
  165. }
  166. // create the cookie with the key
  167. $this->load->helper('cookie');
  168. $cookie = array(
  169. 'name' => 'user_remember_key',
  170. 'value' => $remember_key,
  171. 'expire' => (60*60*24*365) // 1 year
  172. );
  173. set_cookie($cookie);
  174. // put key in database
  175. $this->db->update('users',array('user_remember_key' => $remember_key),array('user_id' => $user['id']));
  176. }
  177. return TRUE;
  178. }
  179. /**
  180. * Login by ID
  181. *
  182. * @param int $user_id
  183. *
  184. * @return boolean
  185. */
  186. function login_by_id ($user_id, $password = FALSE) {
  187. $CI =& get_instance();
  188. $CI->load->model('users/login_model');
  189. $CI->login_model->new_login($user_id);
  190. $this->db->update('users',array('user_last_login' => date('Y-m-d H:i:s')),array('user_id' => $user_id));
  191. $this->session->set_userdata($this->session_name,$user_id);
  192. $this->session->set_userdata('login_time',now());
  193. $this->set_active($user_id);
  194. // track activity
  195. $this->db->insert('user_activity', array('user_id' => $user_id, 'user_activity_date' => date('Y-m-d H:i:s')));
  196. // cart functions
  197. if (module_installed('store/cart_model')) {
  198. $CI =& get_instance();
  199. $CI->load->model('store/cart_model');
  200. $CI->cart_model->user_login($this->active_user);
  201. }
  202. // salt password
  203. if (!empty($password)) {
  204. // do we have a salt?
  205. if ($this->get('salt') == '') {
  206. // salt it!
  207. $CI->load->helper('string');
  208. $salt = random_string('unique');
  209. // new password with salt
  210. $salted_password = md5($password . ':' . $salt);
  211. $this->db->update('users', array('user_salt' => $salt, 'user_password' => $salted_password), array('user_id' => $this->get('id')));
  212. }
  213. }
  214. // do we have a customer record for this user?
  215. // trigger its creation if not, else just get the active customer ID
  216. $this->active_user['customer_id'] = $this->get_customer_id($user_id);
  217. // prep hook
  218. $CI =& get_instance();
  219. // call the library here, because this may be loaded in the admin/login controller which doesn't preload
  220. // app_hooks like the other controllers
  221. $CI->load->library('app_hooks');
  222. $CI->app_hooks->data('member', $user_id);
  223. $CI->app_hooks->trigger('member_login', $user_id, $password);
  224. $CI->app_hooks->reset();
  225. return TRUE;
  226. }
  227. /**
  228. * User Logout
  229. *
  230. * @return boolean TRUE upon success
  231. */
  232. function logout () {
  233. // delete activity
  234. $this->db->delete('user_activity', array('user_id' => $this->get('id')));
  235. // unset user_id session and login_time
  236. $this->session->unset_userdata($this->session_name,'login_time');
  237. // delete cookie
  238. $CI =& get_instance();
  239. $CI->load->helper('cookie');
  240. delete_cookie('user_remember_key');
  241. // prep hook
  242. // call the library here, because this may be loaded in the admin/login controller which doesn't preload
  243. // app_hooks like the other controllers
  244. $CI->load->library('app_hooks');
  245. $CI->app_hooks->data('member', $this->get('id'));
  246. $CI->app_hooks->trigger('member_logout', $this->get('id'));
  247. $CI->app_hooks->reset();
  248. return TRUE;
  249. }
  250. /**
  251. * Set Active User
  252. *
  253. * Sets the active user by ID, loads user data into array
  254. *
  255. * @param int $user_id
  256. *
  257. * @return boolean TRUE upon success
  258. */
  259. function set_active ($user_id) {
  260. if (!$user = $this->get_user($user_id)) {
  261. return FALSE;
  262. }
  263. $this->active_user = $user;
  264. return TRUE;
  265. }
  266. /**
  267. * Is User an Admin?
  268. *
  269. * @return boolean TRUE if the current user is an administrator
  270. */
  271. function is_admin () {
  272. if (empty($this->active_user) or $this->active_user['is_admin'] == FALSE) {
  273. return FALSE;
  274. }
  275. return TRUE;
  276. }
  277. /**
  278. * Is user logged in?
  279. *
  280. * @return boolean TRUE if user is logged in
  281. */
  282. function logged_in () {
  283. if (empty($this->active_user)) {
  284. return FALSE;
  285. }
  286. else {
  287. return TRUE;
  288. }
  289. }
  290. /**
  291. * Is user in this group?
  292. *
  293. * Returns TRUE if the user is in the usergroup(s) or the privileges are open to all (i.e., array is empty or contains "0").
  294. * To check if the user is logged out, send "-1" by itself or in an array. It's return TRUE if the user is logged out.
  295. *
  296. * @param int|array $group A group ID, or array of group ID's (they must be in one of the groups)
  297. * @param int $user_id (Optional) Specify the user. (default: FALSE)
  298. *
  299. * @return boolean TRUE if in the group
  300. */
  301. function in_group ($group, $user_id = FALSE) {
  302. if (empty($group)) {
  303. return TRUE;
  304. }
  305. // sometimes, we only want to show something if the user is logged out
  306. if ($group == '-1' or (is_array($group) and in_array('-1', $group))) {
  307. if ($this->logged_in() === FALSE) {
  308. return TRUE;
  309. }
  310. else {
  311. return FALSE;
  312. }
  313. }
  314. if ($user_id) {
  315. $user_array = $this->get_user($user_id);
  316. }
  317. else {
  318. $user_array = $this->active_user;
  319. }
  320. if (is_array($group) and in_array('0', $group)) {
  321. // this is a "privileges" array and it's public so anyone can see it
  322. return TRUE;
  323. }
  324. elseif (is_array($group) and in_array('', $group)) {
  325. return TRUE;
  326. }
  327. if (!$this->logged_in()) {
  328. // we aren't even logged in
  329. return FALSE;
  330. }
  331. if (is_array($group)) {
  332. // are they in any of these groups?
  333. foreach ($group as $one_group) {
  334. if (in_array($one_group, $user_array['usergroups'])) {
  335. return TRUE;
  336. }
  337. }
  338. }
  339. else {
  340. // are they in this group?
  341. if (in_array($group, $user_array['usergroups'])) {
  342. return TRUE;
  343. }
  344. }
  345. // nope
  346. return FALSE;
  347. }
  348. /**
  349. * Is user NOT in this group?
  350. *
  351. * @param int|array A group ID, or array of group ID's (they must NOT be in any of the groups)
  352. * @param int $user_id (Optional) Specify the user. Default: Current User
  353. *
  354. * @return boolean TRUE if not in the group
  355. */
  356. function not_in_group ($group, $user_id = FALSE) {
  357. if (empty($group)) {
  358. return FALSE;
  359. }
  360. if ($user_id) {
  361. $user_array = $this->get_user($user_id);
  362. }
  363. else {
  364. $user_array = $this->active_user;
  365. }
  366. if (!$this->logged_in()) {
  367. // we aren't even logged in
  368. return TRUE;
  369. }
  370. if (is_array($group)) {
  371. // are they in any of these groups?
  372. foreach ($group as $one_group) {
  373. if (in_array($one_group, $user_array['usergroups'])) {
  374. return FALSE;
  375. }
  376. }
  377. }
  378. else {
  379. // are they in this group?
  380. if (in_array($group, $user_array['usergroups'])) {
  381. return FALSE;
  382. }
  383. }
  384. // nope, they aren't in any of them
  385. return TRUE;
  386. }
  387. /**
  388. * Get user data
  389. *
  390. * @param string $parameter The name of the piece of user data (e.g., email)
  391. *
  392. * @return string User data
  393. */
  394. function get ($parameter = FALSE) {
  395. if ($parameter) {
  396. return $this->active_user[$parameter];
  397. }
  398. else {
  399. return $this->active_user;
  400. }
  401. }
  402. /**
  403. * Get Active Subscriptions
  404. *
  405. * Gets active subscriptions for a user
  406. *
  407. * @param int $user_id The user id
  408. *
  409. * @return array Array of active subscriptions, else FALSE if none exist
  410. */
  411. function get_active_subscriptions ($user_id) {
  412. if (module_installed('billing')) {
  413. $this->load->model('billing/recurring_model');
  414. $customer_id = $this->get_customer_id($user_id);
  415. return $this->recurring_model->GetRecurrings(array('customer_id' => $customer_id));
  416. }
  417. else {
  418. return FALSE;
  419. }
  420. }
  421. /**
  422. * Get Subscriptions
  423. *
  424. * Gets active and cancelled subscriptions for a user
  425. *
  426. * @param int $user_id The user id
  427. *
  428. * @param array Array of subscriptions, else FALSE if none exist
  429. */
  430. function get_subscriptions ($user_id) {
  431. if (module_installed('billing')) {
  432. $this->load->model('billing/subscription_model');
  433. return $this->subscription_model->get_subscriptions(array('user_id' => $user_id), TRUE);
  434. }
  435. else {
  436. return FALSE;
  437. }
  438. }
  439. /**
  440. * Get Customer ID
  441. *
  442. * Sometimes, we just need this, so let's not do a full blown query.
  443. *
  444. * @param int $user_id (default: active user)
  445. *
  446. * @return int|boolean $customer_id
  447. */
  448. function get_customer_id ($user_id = FALSE) {
  449. if (!module_installed('billing')) {
  450. return FALSE;
  451. }
  452. // auto-complete $user_id?
  453. if (empty($user_id) and $this->logged_in()) {
  454. $user_id = $this->active_user['id'];
  455. }
  456. // previously, we looked for the "customer_id" in the users table
  457. // however, this didn't let us confirm that the customer record actually exists
  458. // so now we look up via the customers table
  459. $this->db->select('customer_id');
  460. $this->db->where('internal_id',$user_id);
  461. $result = $this->db->get('customers');
  462. if ($result->num_rows() == 0) {
  463. // no reason not to have a customer record
  464. // let's create one
  465. if (module_installed('billing')) {
  466. // get user data
  467. $user = (!empty($this->active_user) and $this->active_user['id'] == $user_id) ? $this->active_user : $this->get_user($user_id);
  468. if (empty($user)) {
  469. // how would this happen? probably impossible
  470. return FALSE;
  471. }
  472. // do any custom fields map to billing fields?
  473. $user_custom_fields = $this->get_custom_fields();
  474. $customer = array();
  475. if (is_array($user_custom_fields)) {
  476. foreach ($user_custom_fields as $field) {
  477. if (!empty($field['billing_equiv']) and isset($user[$field['name']])) {
  478. $customer[$field['billing_equiv']] = $user[$field['name']];
  479. }
  480. }
  481. }
  482. $CI =& get_instance();
  483. $CI->load->model('billing/customer_model');
  484. $customer['email'] = $user['email'];
  485. $customer['internal_id'] = $user['id'];
  486. $customer['first_name'] = $user['first_name'];
  487. $customer['last_name'] = $user['last_name'];
  488. $customer_id = $CI->customer_model->NewCustomer($customer);
  489. $this->db->update('users',array('customer_id' => $customer_id),array('user_id' => $user_id));
  490. return $customer_id;
  491. }
  492. }
  493. else {
  494. return $result->row()->customer_id;
  495. }
  496. }
  497. /**
  498. * Set Charge ID
  499. *
  500. * After a successful order, we put this charge ID in the user database so that when the
  501. * charge trigger is tripped, we'll process this user's cart
  502. *
  503. * @param int $user_id
  504. * @param int $charge_id
  505. *
  506. * @return boolean TRUE
  507. */
  508. function set_charge_id ($user_id, $charge_id) {
  509. $this->db->update('users', array('user_pending_charge_id' => $charge_id), array('user_id' => $user_id));
  510. return TRUE;
  511. }
  512. /**
  513. * Remove Charge ID
  514. *
  515. * @param int $user_id
  516. *
  517. * @return boolean TRUE
  518. */
  519. function remove_charge_id ($user_id) {
  520. $this->db->update('users', array('user_pending_charge_id' => '0'), array('user_id' => $user_id));
  521. return TRUE;
  522. }
  523. /**
  524. * Validation
  525. *
  526. * Validates POST data to be acceptable for creating a new user
  527. *
  528. * @param boolean $editing Set to TRUE if this is an edited user (i.e., password can be blank) (default: FALSE)
  529. * @param boolean $error_array Return errors in an array or HTML formatted string (TRUE for array) (default: TRUE)
  530. *
  531. * @return array If errors, returns an array of individual errors, else returns TRUE
  532. */
  533. function validation ($editing = FALSE, $error_array = TRUE) {
  534. $CI =& get_instance();
  535. $CI->load->library('form_validation');
  536. $CI->load->model('custom_fields_model');
  537. $CI->load->helpers(array('unique_username','unique_email','strip_whitespace'));
  538. $CI->form_validation->set_rules('first_name','First Name','trim|required');
  539. $CI->form_validation->set_rules('last_name','Last Name','trim|required');
  540. $unique_email = ($editing == FALSE) ? '|unique_email' : '';
  541. $CI->form_validation->set_rules('email','Email','trim' . $unique_email . '|valid_email|required');
  542. $username_rules = array('trim','strip_whitespace','min_length[3]');
  543. if ($this->config->item('username_allow_special_characters') == FALSE) {
  544. $username_rules[] = 'alpha_numeric';
  545. }
  546. if ($editing == FALSE) {
  547. $username_rules[] = 'unique_username';
  548. }
  549. $CI->form_validation->set_rules('username','Username',implode('|', $username_rules));
  550. if ($editing == FALSE) {
  551. $CI->form_validation->set_rules('password','Password','min_length[5]|matches[password2]');
  552. $CI->form_validation->set_rules('password2','Repeat Password','required');
  553. }
  554. if ($CI->form_validation->run() === FALSE) {
  555. if ($error_array == TRUE) {
  556. return explode('||',str_replace(array('<p>','</p>'),array('','||'),validation_errors()));
  557. }
  558. else {
  559. return validation_errors();
  560. }
  561. }
  562. // validate custom fields
  563. $custom_fields = $this->get_custom_fields(array('not_in_admin' => TRUE));
  564. $CI->load->library('custom_fields/form_builder');
  565. $CI->form_builder->build_form_from_array($custom_fields);
  566. if ($CI->form_builder->validate_post() === FALSE) {
  567. $errors = $CI->form_builder->validation_errors(($error_array == TRUE) ? TRUE : FALSE);
  568. return $errors;
  569. }
  570. return TRUE;
  571. }
  572. /**
  573. * Validate Billing Address
  574. *
  575. * @param int $user_id
  576. *
  577. * @return boolean TRUE if they have a valid billing address on file
  578. */
  579. function validate_billing_address ($user_id) {
  580. $address = $this->get_billing_address($user_id);
  581. $required = array(
  582. 'first_name',
  583. 'last_name',
  584. 'address_1',
  585. 'city',
  586. 'country',
  587. 'postal_code'
  588. );
  589. foreach ($required as $item) {
  590. if (empty($address[$item])) {
  591. return FALSE;
  592. }
  593. }
  594. return TRUE;
  595. }
  596. /**
  597. * Get Billing Address
  598. *
  599. * @param int $user_id
  600. *
  601. * @return array Billing address
  602. */
  603. function get_billing_address ($user_id) {
  604. $customer_id = $this->get_customer_id($user_id);
  605. if (empty($customer_id)) {
  606. return FALSE;
  607. }
  608. $CI =& get_instance();
  609. $CI->load->model('billing/customer_model');
  610. $customer = $this->customer_model->GetCustomer($customer_id);
  611. $address = array(
  612. 'first_name' => $customer['first_name'],
  613. 'last_name' => $customer['last_name'],
  614. 'company' => $customer['company'],
  615. 'address_1' => $customer['address_1'],
  616. 'address_2' => $customer['address_2'],
  617. 'city' => $customer['city'],
  618. 'country' => $customer['country'],
  619. 'postal_code' => $customer['postal_code'],
  620. 'state' => $customer['state'],
  621. 'phone_number' => $customer['phone']
  622. );
  623. return $address;
  624. }
  625. /**
  626. * Is email address unique?
  627. *
  628. * @param string $email The email address being tested
  629. *
  630. * @return boolean TRUE upon being OK, FALSE if not
  631. */
  632. function unique_email ($email) {
  633. $this->db->select('user_id');
  634. $this->db->where('user_email',$email);
  635. $this->db->where('user_deleted','0');
  636. $result = $this->db->get('users');
  637. if ($result->num_rows() > 0) {
  638. return FALSE;
  639. }
  640. else {
  641. return TRUE;
  642. }
  643. }
  644. /**
  645. * Is username unique?
  646. *
  647. * @param string $username The username being tested
  648. *
  649. * @return boolean TRUE upon being OK, FALSE if not
  650. */
  651. function unique_username ($username) {
  652. // protected usernames
  653. $protected = array('admin','administrator','root','','1','2','3','4','5','6','7','8','9');
  654. if (in_array($username, $protected)) {
  655. return FALSE;
  656. }
  657. $this->db->select('user_id');
  658. $this->db->where('user_username',$username);
  659. $this->db->where('user_deleted','0');
  660. $result = $this->db->get('users');
  661. if ($result->num_rows() > 0) {
  662. return FALSE;
  663. }
  664. else {
  665. return TRUE;
  666. }
  667. }
  668. /**
  669. * Add a Usergroup
  670. *
  671. * @param int $user_id
  672. * @param int $group_id
  673. *
  674. * @return array New usergroup array
  675. */
  676. function add_group ($user_id, $group_id) {
  677. $user = $this->get_user($user_id);
  678. if (is_array($user['usergroups']) and !empty($user['usergroups']) and in_array($group_id, $user['usergroups'])) {
  679. // already a member
  680. return FALSE;
  681. }
  682. if ($user['usergroups'] == FALSE) {
  683. $user['usergroups'] = array();
  684. }
  685. $user['usergroups'][] = $group_id;
  686. $usergroups = '|' . implode('|',$user['usergroups']) . '|';
  687. $this->db->update('users',array('user_groups' => $usergroups),array('user_id' => $user_id));
  688. return $usergroups;
  689. }
  690. /**
  691. * Remove a Usergroup
  692. *
  693. * @param int $user_id
  694. * @param int $group_id
  695. *
  696. * @return array New usergroup array
  697. */
  698. function remove_group ($user_id, $group_id) {
  699. $user = $this->get_user($user_id);
  700. if (is_array($user['usergroups']) and !empty($user['usergroups'])) {
  701. foreach ($user['usergroups'] as $key => $val) {
  702. if ($val == $group_id) {
  703. unset($user['usergroups'][$key]);
  704. }
  705. }
  706. $usergroups = '|' . implode('|',$user['usergroups']) . '|';
  707. $this->db->update('users',array('user_groups' => $usergroups),array('user_id' => $user_id));
  708. }
  709. return $usergroups;
  710. }
  711. /**
  712. * Resend Validation Email
  713. *
  714. * Resends the validation email, unless there's no email to be sent
  715. *
  716. * @param int $user_id
  717. *
  718. * @return TRUE
  719. */
  720. function resend_validation_email ($user_id) {
  721. $user = $this->get_user($user_id);
  722. if (empty($user)) {
  723. return FALSE;
  724. }
  725. if (!empty($user['validate_key'])) {
  726. $validation_link = site_url('users/validate/' . $user['validate_key']);
  727. $CI =& get_instance();
  728. $CI->app_hooks->data('member', $user['id']);
  729. $CI->app_hooks->data_var('validation_link', $validation_link);
  730. $CI->app_hooks->data_var('validation_code', $user['validate_key']);
  731. $CI->app_hooks->trigger('member_validate_email');
  732. return TRUE;
  733. }
  734. return FALSE;
  735. }
  736. /**
  737. * New User
  738. *
  739. * Create a new user, including custom fields
  740. *
  741. * @param string $email Email Address
  742. * @param string $password Password to use
  743. * @param string $username Username
  744. * @param string $first_name First name
  745. * @param string $last_name Last name
  746. * @param array $groups Array of group ID's to be entered into (default: FALSE)
  747. * @param int $affiliate Affiliate ID of referrer (default: FALSE)
  748. * @param boolean $is_admin Check to make an administrator (default: FALSE)
  749. * @param array $custom_fields An array of custom field data, matching in name (default: array())
  750. * @param boolean $require_validation Should we require email validation? (default: FALSE)
  751. *
  752. * @return int $user_id
  753. */
  754. function new_user($email, $password, $username, $first_name, $last_name, $groups = FALSE, $affiliate = FALSE, $is_admin = FALSE, $custom_fields = array(), $require_validation = FALSE) {
  755. if (empty($groups)) {
  756. $this->load->model('users/usergroup_model');
  757. $group = $this->usergroup_model->get_default();
  758. $groups = array($group);
  759. }
  760. if ($require_validation == TRUE) {
  761. $validate_key = random_string('unique');
  762. $result = $this->db->select('user_id')->where('user_validate_key',$validate_key)->get('users');
  763. while ($result->num_rows() > 0) {
  764. $validate_key = random_string('unique');
  765. $result = $this->db->select('user_id')->where('user_validate_key',$validate_key)->get('users');
  766. }
  767. }
  768. else {
  769. $validate_key = '';
  770. }
  771. // generate hashed password
  772. $CI =& get_instance();
  773. $CI->load->helper('string');
  774. $salt = random_string('unique');
  775. $hashed_password = md5($password . ':' . $salt);
  776. $insert_fields = array(
  777. 'user_is_admin' => ($is_admin == TRUE) ? '1' : '0',
  778. 'user_groups' => '|' . implode('|',$groups) . '|',
  779. 'user_first_name' => $first_name,
  780. 'user_last_name' => $last_name,
  781. 'user_username' => $username,
  782. 'user_email' => $email,
  783. 'user_password' => $hashed_password,
  784. 'user_salt' => $salt,
  785. 'user_referrer' => ($affiliate != FALSE) ? $affiliate : '0',
  786. 'user_signup_date' => date('Y-m-d H:i:s'),
  787. 'user_last_login' => '0000-00-00 00:00:00',
  788. 'user_suspended' => '0',
  789. 'user_deleted' => '0',
  790. 'user_remember_key' => '',
  791. 'user_validate_key' => $validate_key
  792. );
  793. if (is_array($custom_fields)) {
  794. foreach ($custom_fields as $name => $value) {
  795. $insert_fields[$name] = $value;
  796. }
  797. }
  798. $this->db->insert('users',$insert_fields);
  799. $user_id = $this->db->insert_id();
  800. // create customer record
  801. if (module_installed('billing')) {
  802. $CI->load->model('billing/customer_model');
  803. $customer = array();
  804. $customer['email'] = $email;
  805. $customer['internal_id'] = $user_id;
  806. $customer['first_name'] = $first_name;
  807. $customer['last_name'] = $last_name;
  808. // do any custom fields map to billing fields?
  809. $user_custom_fields = $this->get_custom_fields();
  810. if (is_array($user_custom_fields)) {
  811. foreach ($user_custom_fields as $field) {
  812. if (!empty($field['billing_equiv']) and isset($custom_fields[$field['name']])) {
  813. $customer[$field['billing_equiv']] = $custom_fields[$field['name']];
  814. }
  815. }
  816. }
  817. // we don't want to set a country if we don't have a state/province, because we risk
  818. // internal US/Canada province validation
  819. if (isset($customer['country']) and (!isset($customer['state']) or (isset($customer['state']) and empty($customer['state'])))) {
  820. unset($customer['country']);
  821. }
  822. $customer_id = $CI->customer_model->NewCustomer($customer);
  823. $this->db->update('users',array('customer_id' => $customer_id),array('user_id' => $user_id));
  824. }
  825. // prep hook
  826. // only run this hook if the App_hooks library is loaded
  827. // it may not be if this is the user created during the install wizard
  828. if (class_exists('App_hooks') and $this->trigger_register_hook == TRUE) {
  829. $CI =& get_instance();
  830. $CI->app_hooks->data('member', $user_id);
  831. $CI->app_hooks->data_var('password', $password);
  832. // trip the validation email?
  833. if (!empty($validate_key)) {
  834. $validation_link = site_url('users/validate/' . $validate_key);
  835. $CI->app_hooks->data_var('validation_link', $validation_link);
  836. $CI->app_hooks->data_var('validation_code', $validate_key);
  837. $CI->app_hooks->trigger('member_validate_email');
  838. }
  839. $CI->app_hooks->trigger('member_register', $user_id, $password);
  840. $CI->app_hooks->reset();
  841. }
  842. return $user_id;
  843. }
  844. /**
  845. * Update User
  846. *
  847. * Updates a user, including custom fields
  848. *
  849. * @param int $user_id The current user ID #
  850. * @param string $email Email Address
  851. * @param string $password Password to use
  852. * @param string $username Username
  853. * @param string $first_name First name
  854. * @param string $last_name Last name
  855. * @param array $groups Array of group ID's to be entered into (default: FALSE)
  856. * @param boolean $is_admin Check to make an administrator (default: FALSE)
  857. * @param array $custom_fields An array of custom field data, matching in name (default: array())
  858. *
  859. * @return int $user_id
  860. */
  861. function update_user($user_id, $email, $password, $username, $first_name, $last_name, $groups = FALSE, $is_admin = FALSE, $custom_fields = array()) {
  862. $old_user = $this->get_user($user_id);
  863. if (empty($old_user)) {
  864. return FALSE;
  865. }
  866. // can we update the username?
  867. if ($old_user['username'] != $username) {
  868. // check if username is in use by someone else
  869. $users = $this->db->select('user_id')
  870. ->from('users')
  871. ->where('user_username',$username)
  872. ->get();
  873. if ($users->num_rows() > 0) {
  874. // already in use
  875. // keep old username
  876. $username = $old_user['username'];
  877. }
  878. }
  879. // can we update the email?
  880. if ($old_user['email'] != $email) {
  881. // check if email is in use by someone else
  882. $users = $this->db->select('user_id')
  883. ->from('users')
  884. ->where('user_email',$email)
  885. ->get();
  886. if ($users->num_rows() > 0) {
  887. // already in use
  888. // keep old email
  889. $email = $old_user['email'];
  890. }
  891. }
  892. $update_fields = array(
  893. 'user_is_admin' => ($is_admin == TRUE) ? '1' : '0',
  894. 'user_groups' => @'|' . implode('|',$groups) . '|',
  895. 'user_first_name' => $first_name,
  896. 'user_last_name' => $last_name,
  897. 'user_username' => $username,
  898. 'user_email' => $email
  899. );
  900. if (!empty($password)) {
  901. $this->update_password($user_id, $password);
  902. }
  903. foreach ($custom_fields as $name => $value) {
  904. $update_fields[$name] = $value;
  905. }
  906. $this->db->update('users',$update_fields,array('user_id' => $user_id));
  907. if (module_installed('billing')) {
  908. // update email in customers table
  909. $this->db->update('customers',array('email' => $email),array('internal_id' => $user_id));
  910. // do any custom fields map to billing fields?
  911. $user_custom_fields = $this->get_custom_fields();
  912. $customer = array();
  913. if (is_array($user_custom_fields)) {
  914. foreach ($user_custom_fields as $field) {
  915. if (!empty($field['billing_equiv']) and isset($custom_fields[$field['name']])) {
  916. $customer[$field['billing_equiv']] = $custom_fields[$field['name']];
  917. }
  918. }
  919. }
  920. $customer_id = $this->get_customer_id($user_id);
  921. if (!empty($customer)) {
  922. $this->db->update('customers', $customer, array('internal_id' => $user_id));
  923. }
  924. }
  925. // hook call
  926. $CI =& get_instance();
  927. $CI->app_hooks->data('member', $user_id);
  928. $CI->app_hooks->trigger('member_update', $user_id, $old_user);
  929. $CI->app_hooks->reset();
  930. return TRUE;
  931. }
  932. /**
  933. * Update Billing Address
  934. *
  935. * @param int $user_id
  936. * @param array $address_fields New Address
  937. *
  938. * @return boolean
  939. */
  940. function update_billing_address ($user_id, $address_fields) {
  941. if (!module_installed('billing')) {
  942. return FALSE;
  943. }
  944. $CI =& get_instance();
  945. $CI->load->model('billing/customer_model');
  946. $customer_id = $this->get_customer_id($user_id);
  947. if ($customer_id != FALSE) {
  948. $CI->customer_model->UpdateCustomer($customer_id, $address_fields);
  949. }
  950. else {
  951. // user doesn't have a customer record, let's create one
  952. $address_fields['internal_id'] = $user_id;
  953. $customer_id = $CI->customer_model->NewCustomer($address_fields);
  954. // link this to customer account
  955. $this->db->update('users', array('customer_id' => $customer_id), array('user_id' => $user_id));
  956. }
  957. return TRUE;
  958. }
  959. /**
  960. * Delete User
  961. *
  962. * @param int $user_id The user ID #
  963. *
  964. * @return boolean TRUE
  965. */
  966. function delete_user ($user_id) {
  967. $this->db->update('users',array('user_deleted' => '1'),array('user_id' => $user_id));
  968. // hook call
  969. $CI =& get_instance();
  970. $CI->app_hooks->data('member', $user_id);
  971. $CI->app_hooks->trigger('member_delete', $user_id);
  972. $CI->app_hooks->reset();
  973. return TRUE;
  974. }
  975. /**
  976. * Update Password
  977. *
  978. * @param int $user_id
  979. * @param string $new_password
  980. *
  981. * @return boolean
  982. */
  983. function update_password ($user_id, $new_password) {
  984. $CI =& get_instance();
  985. $CI->load->helper('string');
  986. $salt = random_string('unique');
  987. $hashed_password = md5($new_password . ':' . $salt);
  988. $this->db->update('users',array('user_password' => $hashed_password, 'user_salt' => $salt),array('user_id' => $user_id));
  989. // prep hook
  990. $CI =& get_instance();
  991. $CI->app_hooks->data('member', $user_id);
  992. $CI->app_hooks->data_var('new_password', $new_password);
  993. $CI->app_hooks->trigger('member_change_password', $user_id, $new_password);
  994. $CI->app_hooks->reset();
  995. return TRUE;
  996. }
  997. /**
  998. * Reset Password
  999. *
  1000. * @param int $user_id
  1001. *
  1002. * @return boolean TRUE
  1003. */
  1004. function reset_password ($user_id) {
  1005. $user = $this->get_user($user_id);
  1006. if (empty($user)) {
  1007. return FALSE;
  1008. }
  1009. // reset the password
  1010. $this->load->helper('string');
  1011. $password = random_string('alnum',9);
  1012. $this->db->update('users',array('user_password' => md5($password), 'user_salt' => ''),array('user_id' => $user['id']));
  1013. // hook call
  1014. $CI =& get_instance();
  1015. $CI->app_hooks->data('member', $user['id']);
  1016. $CI->app_hooks->data_var('new_password', $password);
  1017. $CI->app_hooks->trigger('member_forgot_password');
  1018. $CI->app_hooks->reset();
  1019. return TRUE;
  1020. }
  1021. /**
  1022. * Suspend User
  1023. *
  1024. * @param int $user_id The user ID #
  1025. *
  1026. * @return boolean
  1027. */
  1028. function suspend_user ($user_id) {
  1029. $this->db->update('users',array('user_suspended' => '1'),array('user_id' => $user_id));
  1030. // hook call
  1031. $CI =& get_instance();
  1032. $CI->app_hooks->data('member', $user_id);
  1033. $CI->app_hooks->trigger('member_suspend', $user_id);
  1034. $CI->app_hooks->reset();
  1035. return TRUE;
  1036. }
  1037. /**
  1038. * Unsuspend User
  1039. *
  1040. * @param int $user_id The user ID #
  1041. *
  1042. * @return boolean TRUE
  1043. */
  1044. function unsuspend_user ($user_id) {
  1045. $this->db->update('users',array('user_suspended' => '0'),array('user_id' => $user_id));
  1046. // hook call
  1047. $CI =& get_instance();
  1048. $CI->app_hooks->data('member', $user_id);
  1049. $CI->app_hooks->trigger('member_unsuspend', $user_id);
  1050. $CI->app_hooks->reset();
  1051. return TRUE;
  1052. }
  1053. /**
  1054. * Get User
  1055. *
  1056. * @param int $user_id The user ID #
  1057. * @param boolean $any_status Should even deleted records be retrieved?
  1058. *
  1059. * @return array User fields
  1060. */
  1061. function get_user ($user_id, $any_status = FALSE) {
  1062. $filters = array('id' => $user_id);
  1063. $user = $this->get_users($filters, $any_status);
  1064. if (empty($user)) {
  1065. return FALSE;
  1066. }
  1067. else {
  1068. return $user[0];
  1069. }
  1070. }
  1071. /**
  1072. * Count Users
  1073. *
  1074. * @param array $filters In same format as get_users()
  1075. *
  1076. * @return int Number of users matching filters
  1077. */
  1078. function count_users ($filters) {
  1079. return $this->get_users($filters, FALSE, TRUE);
  1080. }
  1081. /**
  1082. * Get Users
  1083. *
  1084. * @param int $filters['id'] The user ID to select
  1085. * @param int $filters['group'] The group ID to filter by
  1086. * @param int $filters['suspended'] Set to 1 to retrieve suspended users
  1087. * @param string $filters['email'] The email address to filter by
  1088. * @param string $filters['name'] Search by first and last name
  1089. * @param string $filters['username'] Member username
  1090. * @param string $filters['first_name'] Search by first name
  1091. * @param string $filters['last_name'] Search by last name
  1092. * @param date $filters['signup_start_date'] Select after this signup date
  1093. * @param date $filters['signup_end_date'] Select before this signup date
  1094. * @param string $filters['keyword'] Search by ID, Name, Email, or Username
  1095. * @param string $filters['sort'] Field to sort by
  1096. * @param string $filters['sort_dir'] ASC or DESC
  1097. * @param int $filters['limit'] How many records to retrieve
  1098. * @param int $filters['offset'] Start records retrieval at this record
  1099. * @param boolean $any_status Set to TRUE to allow for deleted users, as well (default: FALSE)
  1100. * @param boolean $counting Should we just count the number of users that match the filters? (default: FALSE)
  1101. *
  1102. * @return array Each user in an array of users
  1103. */
  1104. function get_users ($filters = array(), $any_status = FALSE, $counting = FALSE) {
  1105. $fields = $this->get_custom_fields();
  1106. // keyword search
  1107. if (isset($filters['keyword'])) {
  1108. $this->db->where('user_deleted','0');
  1109. $this->db->where('user_id', $filters['keyword']);
  1110. $this->db->or_like('user_username', $filters['keyword']);
  1111. $this->db->or_like('user_email', $filters['keyword']);
  1112. $this->db->or_like('user_last_name', $filters['keyword']);
  1113. $this->db->or_like('user_first_name', $filters['keyword']);
  1114. }
  1115. // other filters
  1116. if (isset($filters['id'])) {
  1117. $this->db->where('user_id',$filters['id']);
  1118. }
  1119. if (isset($filters['group'])) {
  1120. $this->db->where('(user_groups LIKE \'%|' . $filters['group'] . '|%\' or user_groups = \'|' . $filters['group'] . '|\')');
  1121. }
  1122. if (isset($filters['suspended'])) {
  1123. $this->db->where('user_suspended',$filters['suspended']);
  1124. }
  1125. if (isset($filters['email'])) {
  1126. $this->db->like('user_email',$filters['email']);
  1127. }
  1128. if (isset($filters['username'])) {
  1129. $this->db->like('user_username',$filters['username']);
  1130. }
  1131. if (isset($filters['name'])) {
  1132. if (is_numeric($filters['name'])) {
  1133. // we are passed a member id
  1134. $this->db->where('users.user_id',$filters['name']);
  1135. } else {
  1136. $this->db->where('(`user_first_name` LIKE \'%' . mysql_real_escape_string($filters['name']) . '%\' OR `user_last_name` LIKE \'%' . mysql_real_escape_string($filters['name']) . '%\')');
  1137. }
  1138. }
  1139. if (isset($filters['first_name'])) {
  1140. $this->db->like('user_first_name',$filters['first_name']);
  1141. }
  1142. if (isset($filters['last_name'])) {
  1143. $this->db->like('user_last_name',$filters['last_name']);
  1144. }
  1145. if (isset($filters['is_admin'])) {
  1146. $this->db->where('users.user_is_admin',$filters['is_admin']);
  1147. }
  1148. if (isset($filters['signup_date_start'])) {
  1149. $date = date('Y-m-d H:i:s', strtotime($filters['signup_date_start']));
  1150. $this->db->where('users.user_signup_date >=', $date);
  1151. }
  1152. if (isset($filters['signup_date_end'])) {
  1153. $date = date('Y-m-d H:i:s', strtotime($filters['signup_date_end']));
  1154. $this->db->where('users.user_signup_date <=', $date);
  1155. }
  1156. // custom field params
  1157. if (is_array($fields)) {
  1158. foreach ($fields as $field) {
  1159. if (isset($filters[$field['name']])) {
  1160. $this->db->like('users.' . $field['name'],$filters[$field['name']]);
  1161. }
  1162. }
  1163. }
  1164. if ($any_status == FALSE) {
  1165. $this->db->where('user_deleted','0');
  1166. }
  1167. // standard ordering and limiting
  1168. if ($counting == FALSE) {
  1169. $order_by = (isset($filters['sort'])) ? $filters['sort'] : 'user_username';
  1170. $order_dir = (isset($filters['sort_dir'])) ? $filters['sort_dir'] : 'ASC';
  1171. $this->db->order_by($order_by, $order_dir);
  1172. if (isset($filters['limit'])) {
  1173. $offset = (isset($filters['offset'])) ? $filters['offset'] : 0;
  1174. $this->db->limit($filters['limit'], $offset);
  1175. }
  1176. }
  1177. if ($counting === TRUE) {
  1178. $this->db->select('users.user_id');
  1179. $result = $this->db->get('users');
  1180. $rows = $result->num_rows();
  1181. $result->free_result();
  1182. return $rows;
  1183. }
  1184. else {
  1185. $this->db->from('users');
  1186. $result = $this->db->get();
  1187. }
  1188. if ($result->num_rows() == 0) {
  1189. return FALSE;
  1190. }
  1191. // get custom fields
  1192. $CI =& get_instance();
  1193. $CI->load->model('custom_fields_model');
  1194. $custom_fields = $CI->custom_fields_model->get_custom_fields(array('group' => '1'));
  1195. $users = array();
  1196. foreach ($result->result_array() as $user) {
  1197. $groups = explode('|',$user['user_groups']);
  1198. $user_groups = array();
  1199. foreach ($groups as $group) {
  1200. if (!empty($group)) {
  1201. $user_groups[] = $group;
  1202. }
  1203. }
  1204. $this_user = array(
  1205. 'id' => $user['user_id'],
  1206. 'is_admin' => ($user['user_is_admin'] == '1') ? TRUE : FALSE,
  1207. 'customer_id' => $user['customer_id'],
  1208. 'salt' => $user['user_salt'],
  1209. 'usergroups' => $user_groups,
  1210. 'first_name' => $user['user_first_name'],
  1211. 'last_name' => $user['user_last_name'],
  1212. 'username' => $user['user_username'],
  1213. 'email' => $user['user_email'],
  1214. 'referrer' => $user['user_referrer'],
  1215. 'signup_date' => local_time($user['user_signup_date']),
  1216. 'last_login' => local_time($user['user_last_login']),
  1217. 'suspended' => ($user['user_suspended'] == 1) ? TRUE : FALSE,
  1218. 'admin_link' => site_url('admincp/users/profile/' . $user['user_id']),
  1219. 'remember_key' => $user['user_remember_key'],
  1220. 'validate_key' => $user['user_validate_key'],
  1221. 'cart' => (empty($user['user_cart'])) ? FALSE : unserialize($user['user_cart']),
  1222. 'pending_charge_id' => (!empty($user['user_pending_charge_id'])) ? $user['user_pending_charge_id'] : FALSE
  1223. );
  1224. foreach ($custom_fields as $field) {
  1225. $this_user[$field['name']] = $user[$field['name']];
  1226. }
  1227. reset($custom_fields);
  1228. $users[] = $this_user;
  1229. }
  1230. $result->free_result();
  1231. return $users;
  1232. }
  1233. /**
  1234. * New User Custom Field
  1235. *
  1236. * Creates the user-field-specific custom field record
  1237. *
  1238. * @param int $custom_field_id
  1239. * @param string $billing_equiv If this field represents a billing address field (e.g, "address_1"), specify here: options: address_1/2, state, country, postal_code, company (default: '')
  1240. * @param boolean $admin_only Is this an admin-only field? (default: FALSE)
  1241. * @param boolean $registration_form Should we show this in the registration form? (default: TRUE)
  1242. *
  1243. * @return int $custom_field_id
  1244. */
  1245. function new_custom_field ($custom_field_id, $billing_equiv = '', $admin_only = FALSE, $registration_form = TRUE) {
  1246. $insert_fields = array(
  1247. 'custom_field_id' => $custom_field_id,
  1248. 'subscription_plans' => '',
  1249. 'products' => '',
  1250. 'user_field_billing_equiv' => $billing_equiv,
  1251. 'user_field_admin_only' => ($admin_only == TRUE) ? '1' : '0',
  1252. 'user_field_registration_form' => ($registration_form == TRUE) ? '1' : '0'
  1253. );
  1254. $this->db->insert('user_fields',$insert_fields);
  1255. return $this->db->insert_id();
  1256. }
  1257. /**
  1258. * Update User Custom Field
  1259. *
  1260. * Updates the user_fields table with user custom field-specific information
  1261. *
  1262. * @param int $user_field_id The custom field ID to edit
  1263. * @param string $billing_equiv If this field represents a billing address field (e.g, "address_1"), specify here: options: address_1/2, state, country, postal_code, company (default: '')
  1264. * @param boolean $admin_only Is this an admin-only field? (default: FALSE)
  1265. * @param boolean $registration_form Should we show this in the registration form? (default: TRUE)
  1266. *
  1267. * @return boolean TRUE
  1268. */
  1269. function update_custom_field ($custom_field_id, $billing_equiv = '', $admin_only = FALSE, $registration_form = TRUE) {
  1270. $result = $this->db->select('user_field_id')
  1271. ->from('user_fields')
  1272. ->where('custom_field_id',$custom_field_id)
  1273. ->get();
  1274. if ($result->num_rows() == 0) {
  1275. // no record yet
  1276. $field_id = $this->new_custom_field($custom_field_id, $billing_equiv, $admin_only, $registration_form);
  1277. }
  1278. $update_fields = array(
  1279. 'subscription_plans' => '',
  1280. 'products' => '',
  1281. 'user_field_billing_equiv' => $billing_equiv,
  1282. 'user_field_admin_only' => ($admin_only == TRUE) ? '1' : '0',
  1283. 'user_field_registration_form' => ($registration_form == TRUE) ? '1' : '0'
  1284. );
  1285. $this->db->update('user_fields',$update_fields,array('custom_field_id' => $custom_field_id));
  1286. return TRUE;
  1287. }
  1288. /**
  1289. * Delete User Custom Field
  1290. *
  1291. * ge custom field record and modify database
  1292. *
  1293. * @param int $id The ID of the field
  1294. * @param string The database table to reflect the changes, else FALSE
  1295. *
  1296. * @return boolean TRUE
  1297. */
  1298. function delete_custom_field ($user_field_id) {
  1299. // get custom_field_id
  1300. $field = $this->get_custom_field($user_field_id);
  1301. $this->load->model('custom_fields_model');
  1302. $this->custom_fields_model->delete_custom_field($field['custom_field_id'], 'users');
  1303. $this->db->delete('user_fields',array('user_field_id' => $user_field_id));
  1304. return TRUE;
  1305. }
  1306. /**
  1307. * Get User Custom Field
  1308. *
  1309. * @param int $custom_field_id
  1310. *
  1311. * @return boolean $custom_field or FALSE
  1312. */
  1313. function get_custom_field ($id) {
  1314. $return = $this->get_custom_fields(array('id' => $id));
  1315. if (empty($return)) {
  1316. return FALSE;
  1317. }
  1318. return $return[0];
  1319. }
  1320. /**
  1321. * Get User Custom Fields
  1322. *
  1323. * Retrieves custom fields ordered by custom_field_order
  1324. *
  1325. * @param int $filters['id'] A custom field ID
  1326. * @param boolean $filters['registration_form'] Set to TRUE to retrieve registration form fields
  1327. * @param boolean $filters['not_in_admin'] Set to TRUE to not retrieve admin-only fields
  1328. *
  1329. * @return array $fields The custom fields
  1330. */
  1331. function get_custom_fields ($filters = array()) {
  1332. $cache_string = md5(implode(',',$filters));
  1333. if (isset($this->cache_fields[$cache_string])) {
  1334. return $this->cache_fields[$cache_string];
  1335. }
  1336. $this->load->model('custom_fields_model');
  1337. if (isset($filters['id'])) {
  1338. $this->db->where('user_field_id',$filters['id']);
  1339. }
  1340. if (isset($filters['registration_form']) and $filters['registration_form'] == TRUE) {
  1341. $this->db->where('user_field_registration_form','1');
  1342. }
  1343. if (isset($filters['not_in_admin']) and $filters['not_in_admin'] == TRUE) {
  1344. $this->db->where('user_field_admin_only','0');
  1345. }
  1346. $this->db->join('user_fields','custom_fields.custom_field_id = user_fields.custom_field_id','left');
  1347. $this->db->order_by('custom_fields.custom_field_order','ASC');
  1348. $this->db->select('user_fields.user_field_id');
  1349. $this->db->select('user_fields.user_field_billing_equiv');
  1350. $this->db->select('user_fields.user_field_admin_only');
  1351. $this->db->select('user_fields.user_field_registration_form');
  1352. $this->db->select('custom_fields.*');
  1353. $this->db->where('custom_field_group','1');
  1354. $result = $this->db->get('custom_fields');
  1355. if ($result->num_rows() == 0) {
  1356. return FALSE;
  1357. }
  1358. $billing_installed = module_installed('billing');
  1359. $fields = array();
  1360. foreach ($result->result_array() as $field) {
  1361. $fields[] = array(
  1362. 'id' => $field['user_field_id'],
  1363. 'custom_field_id' => $field['custom_field_id'],
  1364. 'friendly_name' => $field['custom_field_friendly_name'],
  1365. 'name' => $field['custom_field_name'],
  1366. 'type' => $field['custom_field_type'],
  1367. 'options' => (!empty($field['custom_field_options'])) ? unserialize($field['custom_field_options']) : array(),
  1368. 'help' => $field['custom_field_help_text'],
  1369. 'order' => $field['custom_field_order'],
  1370. 'width' => $field['custom_field_width'],
  1371. 'default' => $field['custom_field_default'],
  1372. 'required' => ($field['custom_field_required'] == 1) ? TRUE : FALSE,
  1373. 'validators' => (!empty($field['custom_field_validators'])) ? unserialize($field['custom_field_validators']) : array(),
  1374. 'data' => (!empty($field['custom_field_data'])) ? unserialize($field['custom_field_data']) : array(),
  1375. 'billing_equiv' => ($billing_installed === TRUE) ? $field['user_field_billing_equiv'] : '',
  1376. 'admin_only' => ($field['user_field_admin_only'] == '1') ? TRUE : FALSE,
  1377. 'registration_form' => ($field['user_field_registration_form'] == '1') ? TRUE : FALSE
  1378. );
  1379. }
  1380. $this->cache_fields[$cache_string] = $fields;
  1381. return $fields;
  1382. }
  1383. }