PageRenderTime 76ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

/cp/expressionengine/models/member_model.php

https://bitbucket.org/sbeuken/artelux
PHP | 1624 lines | 844 code | 251 blank | 529 comment | 111 complexity | fbfd8708593f8aaf5df8d96e93606bdd MD5 | raw file
  1. <?php if (!defined('BASEPATH')) exit('No direct script access allowed');
  2. /**
  3. * ExpressionEngine - by EllisLab
  4. *
  5. * @package ExpressionEngine
  6. * @author EllisLab Dev Team
  7. * @copyright Copyright (c) 2003 - 2012, EllisLab, Inc.
  8. * @license http://ellislab.com/expressionengine/user-guide/license.html
  9. * @link http://ellislab.com
  10. * @since Version 2.0
  11. * @filesource
  12. */
  13. // ------------------------------------------------------------------------
  14. /**
  15. * ExpressionEngine Member Model
  16. *
  17. * @package ExpressionEngine
  18. * @subpackage Core
  19. * @category Model
  20. * @author EllisLab Dev Team
  21. * @link http://ellislab.com
  22. */
  23. class Member_model extends CI_Model {
  24. /**
  25. * Get Username
  26. *
  27. * Get a username from a member id
  28. *
  29. * @access public
  30. * @param int
  31. * @param string
  32. */
  33. function get_username($id = '', $field = 'screen_name')
  34. {
  35. if ($id == '')
  36. {
  37. // no id, return false
  38. return FALSE;
  39. }
  40. $this->db->select('username, screen_name');
  41. $this->db->where('member_id', $id);
  42. $member_info = $this->db->get('members');
  43. if ($member_info->num_rows() != 1)
  44. {
  45. // no match, return false
  46. return FALSE;
  47. }
  48. else
  49. {
  50. $member_name = $member_info->row();
  51. if ($field == 'username')
  52. {
  53. return $member_name->username;
  54. }
  55. else
  56. {
  57. return $member_name->screen_name;
  58. }
  59. }
  60. }
  61. // --------------------------------------------------------------------
  62. /**
  63. * Get Upload Groups
  64. *
  65. * @access public
  66. * @return mixed
  67. */
  68. function get_upload_groups()
  69. {
  70. $this->db->select('group_id, group_title');
  71. $this->db->from('member_groups');
  72. $this->db->where("group_id != '1' AND group_id != '2' AND group_id != '3' AND group_id != '4'");
  73. $this->db->where('site_id', $this->config->item('site_id'));
  74. $this->db->order_by('group_title');
  75. return $this->db->get();
  76. }
  77. // --------------------------------------------------------------------
  78. /**
  79. * Get Memmbers
  80. *
  81. * Get a collection of members
  82. *
  83. * @access public
  84. * @param int
  85. * @param int
  86. * @param int
  87. * @param string
  88. * @return mixed
  89. */
  90. function get_members($group_id = '', $limit = '', $offset = '', $search_value = '', $order = array(), $column = 'all')
  91. {
  92. $this->db->select("members.username, members.member_id, members.screen_name, members.email, members.join_date, members.last_visit, members.group_id, members.member_id, members.in_authorlist");
  93. $this->_prep_search_query($group_id, $search_value, $column);
  94. if ($limit != '')
  95. {
  96. $this->db->limit($limit);
  97. }
  98. if ($offset != '')
  99. {
  100. $this->db->offset($offset);
  101. }
  102. if (is_array($order) && count($order) > 0)
  103. {
  104. foreach ($order as $key => $val)
  105. {
  106. $this->db->order_by($key, $val);
  107. }
  108. }
  109. else
  110. {
  111. $this->db->order_by('join_date');
  112. }
  113. $members = $this->db->get('members');
  114. if ($members->num_rows() == 0)
  115. {
  116. return FALSE;
  117. }
  118. else
  119. {
  120. return $members;
  121. }
  122. }
  123. // --------------------------------------------------------------------
  124. /**
  125. * Count Members
  126. *
  127. * @access public
  128. * @return int
  129. */
  130. function get_member_count($group_id = FALSE)
  131. {
  132. $member_ids = array();
  133. if ($group_id != '')
  134. {
  135. $this->db->select('member_id');
  136. $this->db->where('group_id', $group_id);
  137. $query = $this->db->get('members');
  138. foreach($query->result() as $member)
  139. {
  140. $member_ids[] = $member->member_id;
  141. }
  142. // no member_ids in that group? Might as well return now
  143. if (count($member_ids) < 1)
  144. {
  145. return FALSE;
  146. }
  147. }
  148. // now run the query for the actual results
  149. if ($group_id)
  150. {
  151. $this->db->where_in("members.member_id", $member_ids);
  152. }
  153. $this->db->select("COUNT(*) as count");
  154. $this->db->from("member_groups");
  155. $this->db->from("members");
  156. $this->db->where("members.group_id = " .$this->db->dbprefix("member_groups.group_id"));
  157. $this->db->where("member_groups.site_id", $this->config->item('site_id'));
  158. $members = $this->db->get();
  159. return ($members->num_rows() == 0) ? FALSE : $members->row('count');
  160. }
  161. // --------------------------------------------------------------------
  162. /**
  163. * Get All Member Fields
  164. *
  165. * @access public
  166. * @param array // associative array of where
  167. * @param bool // restricts to public fields for non-superadmins
  168. * @return object
  169. */
  170. function get_all_member_fields($additional_where = array(), $restricted = TRUE)
  171. {
  172. // Extended profile fields
  173. $this->db->from('member_fields');
  174. if ($restricted == TRUE && $this->session->userdata('group_id') != 1)
  175. {
  176. $this->db->where('m_field_public', 'y');
  177. }
  178. foreach ($additional_where as $where)
  179. {
  180. foreach ($where as $field => $value)
  181. {
  182. if (is_array($value))
  183. {
  184. $this->db->where_in($field, $value);
  185. }
  186. else
  187. {
  188. $this->db->where($field, $value);
  189. }
  190. }
  191. }
  192. $this->db->order_by('m_field_order');
  193. return $this->db->get();
  194. }
  195. // --------------------------------------------------------------------
  196. /**
  197. * Get Member Data
  198. *
  199. * @access public
  200. * @return object
  201. */
  202. function get_all_member_data($id)
  203. {
  204. $this->db->from('member_data');
  205. $this->db->where('member_id', $id);
  206. return $this->db->get();
  207. }
  208. // --------------------------------------------------------------------
  209. /**
  210. * Get Member Data
  211. *
  212. * This function retuns author data for a single member
  213. *
  214. * @access public
  215. * @param integer Member Id
  216. * @param array Optional fields to return
  217. * @return mixed
  218. */
  219. function get_member_data($member_id = FALSE, $fields = array())
  220. {
  221. if (count($fields) >= 1)
  222. {
  223. $this->db->select($fields);
  224. }
  225. $this->db->where('member_id', (int) $member_id);
  226. return $this->db->get('members');
  227. }
  228. // --------------------------------------------------------------------
  229. /**
  230. * Get Member Ignore List
  231. *
  232. * This function retuns author data for a single member
  233. *
  234. * @access public
  235. * @param integer Member Id
  236. * @return object
  237. */
  238. function get_member_ignore_list($member_id = FALSE)
  239. {
  240. $query = $this->get_member_data($this->id, array('ignore_list'));
  241. $ignored = ($query->row('ignore_list') == '') ? array('') : explode('|', $query->row('ignore_list'));
  242. $this->db->select('screen_name, member_id');
  243. $this->db->where_in('member_id', $ignored);
  244. $this->db->order_by('screen_name');
  245. return $this->db->get('members');
  246. }
  247. // --------------------------------------------------------------------
  248. /**
  249. * Get Member Quicklinks
  250. *
  251. * This function retuns an array of the users quick links
  252. *
  253. * @access public
  254. * @param integer Member Id
  255. * @return array
  256. */
  257. function get_member_quicklinks($member_id = FALSE)
  258. {
  259. $query = $this->get_member_data($member_id, array('quick_links'));
  260. $i = 1;
  261. $quicklinks = array();
  262. if (count($query->row('quick_links')) != 0 AND $query->row('quick_links') != '')
  263. {
  264. foreach (explode("\n", $query->row('quick_links') ) as $row)
  265. {
  266. $x = explode('|', $row);
  267. $quicklinks[$i]['title'] = (isset($x['0'])) ? $x['0'] : '';
  268. $quicklinks[$i]['link'] = (isset($x['1'])) ? $x['1'] : '';
  269. $quicklinks[$i]['order'] = (isset($x['2'])) ? $x['2'] : '';
  270. $i++;
  271. }
  272. }
  273. return $quicklinks;
  274. }
  275. // --------------------------------------------------------------------
  276. /**
  277. * Get Member Emails
  278. *
  279. * By default fetches member_id, email, and screen_name. Additional fields and
  280. * WHERE clause can be specified by using the array arguments
  281. *
  282. * @access public
  283. * @param array
  284. * @param array array of associative field => value arrays
  285. * @return object
  286. */
  287. function get_member_emails($additional_fields = array(), $additional_where = array())
  288. {
  289. if ( ! is_array($additional_fields))
  290. {
  291. $additional_fields = array($additional_fields);
  292. }
  293. if ( ! isset($additional_where[0]))
  294. {
  295. $additional_where = array($additional_where);
  296. }
  297. if (count($additional_fields) > 0)
  298. {
  299. $this->db->select(implode(',', $additional_fields));
  300. }
  301. $this->db->select("m.member_id, m.screen_name, m.email");
  302. $this->db->from("members AS m");
  303. $this->db->join('member_groups AS mg', 'mg.group_id = m.group_id');
  304. $this->db->where('mg.site_id', $this->config->item('site_id'));
  305. foreach ($additional_where as $where)
  306. {
  307. foreach ($where as $field => $value)
  308. {
  309. if (is_array($value))
  310. {
  311. $this->db->where_in($field, $value);
  312. }
  313. else
  314. {
  315. $this->db->where($field, $value);
  316. }
  317. }
  318. }
  319. $this->db->order_by('member_id');
  320. return $this->db->get();
  321. }
  322. // --------------------------------------------------------------------
  323. /**
  324. * Create member
  325. *
  326. * This function creates a new member
  327. *
  328. * @access public
  329. * @param array
  330. * @param mixed // custom member data optional
  331. * @return int member id
  332. */
  333. function create_member($data = array(), $cdata = FALSE)
  334. {
  335. // Insert into the main table
  336. $this->db->insert('members', $data);
  337. // grab insert id
  338. $member_id = $this->db->insert_id();
  339. // Create a record in the custom field table
  340. if ($cdata)
  341. {
  342. $this->db->insert('member_data', array_merge(array('member_id' => $member_id), $cdata));
  343. }
  344. else
  345. {
  346. $this->db->insert('member_data', array('member_id' => $member_id));
  347. }
  348. // Create a record in the member homepage table
  349. $this->db->insert('member_homepage', array('member_id' => $member_id));
  350. return $member_id;
  351. }
  352. // --------------------------------------------------------------------
  353. /**
  354. * Update member
  355. *
  356. * This function updates a member
  357. *
  358. * @access public
  359. * @param int
  360. * @param array
  361. * @return void
  362. */
  363. function update_member($member_id = '', $data = array(), $additional_where = array())
  364. {
  365. $default_null = array('bday_y', 'bday_m', 'bday_d');
  366. foreach($default_null as $val)
  367. {
  368. if (isset($data[$val]) && $data[$val] == '')
  369. {
  370. $data[$val] = NULL;
  371. }
  372. }
  373. if ( ! isset($additional_where[0]))
  374. {
  375. $additional_where = array($additional_where);
  376. }
  377. foreach ($additional_where as $where)
  378. {
  379. foreach ($where as $field => $value)
  380. {
  381. if (is_array($value))
  382. {
  383. $this->db->where_in($field, $value);
  384. }
  385. else
  386. {
  387. $this->db->where($field, $value);
  388. }
  389. }
  390. }
  391. $this->db->where('member_id', $member_id);
  392. $this->db->update('members', $data);
  393. }
  394. // --------------------------------------------------------------------
  395. /**
  396. * Update Member Group
  397. *
  398. * This function updates a member group
  399. *
  400. * @access public
  401. * @param int
  402. * @param array
  403. * @return void
  404. */
  405. function update_member_group($member_group_id = '')
  406. {
  407. // for later use
  408. }
  409. // --------------------------------------------------------------------
  410. /**
  411. * Update member data
  412. *
  413. * This function updates a member's data
  414. *
  415. * @access public
  416. * @param int
  417. * @param array
  418. * @return void
  419. */
  420. function update_member_data($member_id = '', $data = array(), $additional_where = array())
  421. {
  422. if ( ! isset($additional_where[0]))
  423. {
  424. $additional_where = array($additional_where);
  425. }
  426. foreach ($additional_where as $where)
  427. {
  428. foreach ($where as $field => $value)
  429. {
  430. if (is_array($value))
  431. {
  432. $this->db->where_in($field, $value);
  433. }
  434. else
  435. {
  436. $this->db->where($field, $value);
  437. }
  438. }
  439. }
  440. $this->db->where('member_id', $member_id);
  441. $this->db->update('member_data', $data);
  442. }
  443. // --------------------------------------------------------------------
  444. /**
  445. * Delete member
  446. *
  447. * This function deletes all member data, and all communications from said member
  448. * stored on the system, and returns the id for further use
  449. *
  450. * @access public
  451. * @param mixed Single member ID as int, or array of member IDs to delete
  452. * @param int Member ID to take over ownership of deleted members' entries
  453. * @return void
  454. */
  455. function delete_member($member_ids = array(), $heir_id = NULL)
  456. {
  457. // Make sure $member_ids is an array
  458. if ( ! is_array($member_ids))
  459. {
  460. $member_ids = array((int) $member_ids);
  461. }
  462. // ---------------------------------------------------------------
  463. // 'member_delete' hook.
  464. // - Provides an opportunity for extra code to be executed upon
  465. // member deletion, and also gives the opportunity to skip
  466. // deletion for some members all together by altering the array of
  467. // member IDs we pass to the hook.
  468. //
  469. if ($this->extensions->active_hook('member_delete'))
  470. {
  471. $member_ids = $this->extensions->call('member_delete', $member_ids);
  472. }
  473. //
  474. // ---------------------------------------------------------------
  475. // No member IDs? Bail out
  476. if ($member_ids == NULL OR ! count($member_ids))
  477. {
  478. return FALSE;
  479. }
  480. // ---------------------------------------------------------------
  481. // Remove traces of member from base member tables
  482. // ---------------------------------------------------------------
  483. $tables_fields = array(
  484. 'members' => 'member_id',
  485. 'member_data' => 'member_id',
  486. 'member_homepage' => 'member_id',
  487. 'message_data' => 'sender_id',
  488. 'message_folders' => 'member_id',
  489. 'message_listed' => 'member_id',
  490. 'message_listed' => 'listed_member',
  491. 'message_copies' => 'recipient_id',
  492. 'remember_me' => 'member_id',
  493. 'sessions' => 'member_id'
  494. );
  495. // If comment module is installed
  496. if ($this->db->table_exists('comment_subscriptions'))
  497. {
  498. $tables_fields['comment_subscriptions'] = 'member_id';
  499. }
  500. // Loop through tables array and clear out based on member ID
  501. foreach ($tables_fields as $table => $field)
  502. {
  503. $this->db->where_in($field, $member_ids)->delete($table);
  504. }
  505. // ---------------------------------------------------------------
  506. // Delete private messages and update members' unread count
  507. // ---------------------------------------------------------------
  508. // First, we need to get a list of recipient IDs who will be affected
  509. // by deleting the members we are deleting so that we can update the
  510. // unread PM count for those users only
  511. $this->db->distinct('recipient_id');
  512. $this->db->where('message_read', 'n');
  513. $this->db->where_in('sender_id', $member_ids);
  514. $messages = $this->db->get('message_copies');
  515. // Now that we know which recipients are affected, we can delete the
  516. // member-to-be-deleted's messages...
  517. $this->db->where_in('sender_id', $member_ids)->delete('message_copies');
  518. if ($messages->num_rows())
  519. {
  520. // Build recipient IDs array
  521. foreach ($messages->result_array() as $message)
  522. {
  523. $recipient_ids[] = $message['recipient_id'];
  524. }
  525. // ...and get the new unread count for the affected users
  526. $this->db->select('count(*) as count, recipient_id');
  527. $this->db->where('message_read', 'n');
  528. $this->db->where_in('recipient_id', $recipient_ids);
  529. $this->db->group_by('recipient_id');
  530. $unread_messages = $this->db->get('message_copies');
  531. // Set everyone's unread message count to zero first, because if a user
  532. // has zero messages now, they won't have shown up in the above query
  533. $this->db->where_in('member_id', $recipient_ids);
  534. $this->db->update('members', array('private_messages' => 0));
  535. // For each user, update their private messages unread count with
  536. // what we gathered above
  537. foreach ($unread_messages->result_array() as $message)
  538. {
  539. $this->db->where('member_id', $message['recipient_id']);
  540. $this->db->update('members', array('private_messages' => $message['count']));
  541. }
  542. }
  543. // ---------------------------------------------------------------
  544. // Get member's channel entries, reassign them to the entries heir
  545. // or delete them all together if heir isn't specified
  546. // ---------------------------------------------------------------
  547. // Get member's entries
  548. $this->db->select('entry_id, channel_id');
  549. $this->db->where_in('author_id', $member_ids);
  550. $entries = $this->db->get('channel_titles');
  551. $channel_ids = array();
  552. if ($entries->num_rows())
  553. {
  554. // Reassign entries if heir ID is present
  555. if ( ! empty($heir_id) && is_numeric($heir_id))
  556. {
  557. $this->db->where_in('author_id', $member_ids);
  558. $this->db->update('channel_titles', array('author_id' => $heir_id));
  559. $this->update_member_entry_stats($heir_id);
  560. }
  561. // Otherwise, delete them, likely happens when member deletes own account
  562. else
  563. {
  564. foreach ($entries->result_array() as $entry)
  565. {
  566. // Entries to delete
  567. $entry_ids[] = $entry['entry_id'];
  568. // Gather channel IDs to update stats later
  569. $channel_ids[] = $entry['channel_id'];
  570. }
  571. $this->db->where_in('author_id', $member_ids)->delete('channel_titles');
  572. $this->db->where_in('entry_id', $entry_ids)->delete('channel_data');
  573. if ($this->db->table_exists('comments'))
  574. {
  575. $this->db->where_in('entry_id', $entry_ids)->delete('comments');
  576. }
  577. }
  578. }
  579. // ---------------------------------------------------------------
  580. // Find affected entries for members's comments and update totals
  581. // ---------------------------------------------------------------
  582. if ($this->db->table_exists('comments'))
  583. {
  584. $this->db->select('DISTINCT(entry_id), channel_id');
  585. $this->db->where_in('author_id', $member_ids);
  586. $entries = $this->db->get('comments');
  587. $entry_ids = array();
  588. foreach ($entries->result_array() as $row)
  589. {
  590. // Entries to update
  591. $entry_ids[] = $row['entry_id'];
  592. // Gather channel IDs to update stats later
  593. $channel_ids[] = $row['channel_id'];
  594. }
  595. // Delete comments
  596. $this->db->where_in('author_id', $member_ids)->delete('comments');
  597. // Update individual entry comment counts
  598. $this->load->model('comment_model');
  599. $this->comment_model->recount_entry_comments($entry_ids);
  600. }
  601. // Update channel and comment stats
  602. $channel_ids = array_unique($channel_ids);
  603. foreach ($channel_ids as $channel_id)
  604. {
  605. $this->stats->update_channel_stats($channel_id);
  606. $this->stats->update_comment_stats($channel_id);
  607. }
  608. // ---------------------------------------------------------------
  609. // Forum Clean-Up
  610. // ---------------------------------------------------------------
  611. if ($this->config->item('forum_is_installed') == "y")
  612. {
  613. // Forum tables to clean up
  614. $forum_tables_fields = array(
  615. 'forum_subscriptions' => 'member_id',
  616. 'forum_pollvotes' => 'member_id',
  617. 'forum_topics' => 'author_id',
  618. 'forum_administrators' => 'admin_member_id',
  619. 'forum_moderators' => 'mod_member_id',
  620. 'forum_polls' => 'author_id'
  621. );
  622. // Clean out mentions of member in forum tables
  623. foreach ($forum_tables_fields as $table => $field)
  624. {
  625. $this->db->where_in($field, $member_ids)->delete($table);
  626. }
  627. // Load forum class
  628. if ( ! class_exists('Forum'))
  629. {
  630. require PATH_MOD.'forum/mod.forum.php';
  631. require PATH_MOD.'forum/mod.forum_core.php';
  632. }
  633. $forum_core = new Forum_Core;
  634. // -----------------------------------------------------------
  635. // Grab affected topic IDs before deleting the member so we can
  636. // update stats
  637. $this->db->select('topic_id');
  638. $this->db->distinct();
  639. $this->db->where_in('author_id', $member_ids);
  640. $topics = $this->db->get('forum_posts');
  641. // Now delete those posts
  642. $this->db->where_in('author_id', $member_ids)->delete('forum_posts');
  643. // Update topic stats
  644. foreach ($topics->result_array() as $row)
  645. {
  646. $forum_core->_update_topic_stats($row['topic_id']);
  647. }
  648. // -----------------------------------------------------------
  649. // Update forum stats
  650. $this->db->select('forum_id');
  651. $this->db->where('forum_is_cat', 'n');
  652. $forums = $this->db->get('exp_forums');
  653. foreach ($forums->result_array() as $row)
  654. {
  655. $forum_core->_update_post_stats($row['forum_id']);
  656. }
  657. $forum_core->_update_global_stats();
  658. // -----------------------------------------------------------
  659. // Delete from Online Users
  660. $this->db->where_in('member_id', $member_ids)->delete('online_users');
  661. // -----------------------------------------------------------
  662. // Remove attachments
  663. $this->db->select('attachment_id, board_id');
  664. $this->db->where_in('member_id', $member_ids);
  665. $attachments = $this->db->get('forum_attachments');
  666. foreach ($attachments->result_array() as $attachment)
  667. {
  668. $forum_core->_remove_attachment($attachment['attachment_id'], $attachment['board_id'], TRUE);
  669. }
  670. }
  671. $this->stats->update_member_stats();
  672. }
  673. // --------------------------------------------------------------------
  674. /**
  675. * Update entry stats for members, specifically total_entries and last_entry_date
  676. *
  677. * @param array Array of member IDs to update stats for
  678. * @return void
  679. */
  680. public function update_member_entry_stats($member_ids = array())
  681. {
  682. // Make $member_ids an array if we need to
  683. if ( ! is_array($member_ids))
  684. {
  685. $member_ids = array($member_ids);
  686. }
  687. foreach ($member_ids as $member_id)
  688. {
  689. // Get the number of entries and latest entry date for the member
  690. $this->db->select('count(entry_id) AS count, MAX(entry_date) as entry_date');
  691. $this->db->where('author_id', $member_id);
  692. $new_stats = $this->db->get('channel_titles')->row_array();
  693. // Update member stats
  694. $this->db->where('member_id', $member_id);
  695. $this->db->update('members', array(
  696. 'total_entries' => $new_stats['count'],
  697. 'last_entry_date' => $new_stats['entry_date']
  698. ));
  699. }
  700. }
  701. // --------------------------------------------------------------------
  702. /**
  703. * Remove From Author List
  704. *
  705. * Turns on the preference to make a member part of the authorlist
  706. *
  707. * @access public
  708. * @param integer
  709. * @return void
  710. */
  711. function delete_from_authorlist($member_ids = array())
  712. {
  713. if ( ! is_array($member_ids))
  714. {
  715. $member_ids = array($member_ids);
  716. }
  717. $this->db->where_in('member_id', $member_ids);
  718. $this->db->set('in_authorlist', 'n');
  719. $this->db->update('members');
  720. }
  721. // --------------------------------------------------------------------
  722. /**
  723. * Update Author List
  724. *
  725. * Turns on the preference to make a member part of the authorlist
  726. *
  727. * @access public
  728. * @param array
  729. * @return void
  730. */
  731. function update_authorlist($member_ids = array())
  732. {
  733. if ( ! is_array($member_ids))
  734. {
  735. $member_ids = array($member_ids);
  736. }
  737. $this->db->where_in('member_id', $member_ids);
  738. $this->db->set('in_authorlist', 'y');
  739. $this->db->update('members');
  740. }
  741. // --------------------------------------------------------------------
  742. /**
  743. * Get Author Groups
  744. *
  745. * This function retuns an array if group ids for member groups
  746. * who are listed as authors for a channel
  747. *
  748. * @access public
  749. * @param integer
  750. * @return array
  751. */
  752. function get_author_groups($channel_id = '')
  753. {
  754. $this->db->select('member_groups.group_id');
  755. $this->db->join("channel_member_groups", "member_groups.group_id = channel_member_groups.group_id", 'left');
  756. $this->db->where('member_groups.include_in_authorlist', 'y');
  757. $this->db->where("channel_member_groups.channel_id", $channel_id);
  758. $this->db->or_where("member_groups.group_id", 1);
  759. $results = $this->db->get('member_groups');
  760. $group_ids = array();
  761. foreach ($results->result() as $result)
  762. {
  763. $group_ids[] = $result->group_id;
  764. }
  765. return $group_ids;
  766. }
  767. // --------------------------------------------------------------------
  768. /**
  769. * Get Authors
  770. *
  771. * This function returns a set of members who are authors in a set channel
  772. *
  773. * @access public
  774. * @param integer
  775. * @return mixed
  776. */
  777. function get_authors($author_id = FALSE, $limit = FALSE, $offset = FALSE)
  778. {
  779. $this->db->select('members.member_id, members.group_id,
  780. members.username, members.screen_name, members.in_authorlist');
  781. $this->db->from('members');
  782. $this->db->join('member_groups', 'member_groups.group_id = members.group_id');
  783. if ($author_id)
  784. {
  785. $this->db->where('members.member_id !=', $author_id);
  786. }
  787. $this->db->where('('.$this->db->dbprefix('members').'.in_authorlist = "y" OR
  788. '.$this->db->dbprefix('member_groups').'.include_in_authorlist = "y")');
  789. $this->db->where('members.group_id = '.$this->db->dbprefix('member_groups').'.group_id');
  790. $this->db->where('member_groups.site_id', $this->config->item('site_id'));
  791. $this->db->order_by('members.screen_name', 'ASC');
  792. $this->db->order_by('members.username', 'ASC');
  793. if ($limit)
  794. {
  795. $this->db->limit($limit, $offset);
  796. }
  797. return $this->db->get();
  798. }
  799. // --------------------------------------------------------------------
  800. /**
  801. * Get Authors Simple
  802. *
  803. * This function returns a set of members who are authors in a set channel- member group data is omitted
  804. *
  805. * @deprecated 2.4, Use member_model->get_authors instead
  806. * @access public
  807. * @param integer
  808. * @return mixed
  809. */
  810. function get_authors_simple($author_id = FALSE, $limit = FALSE, $offset = FALSE)
  811. {
  812. $this->load->library('logger');
  813. $this->logger->deprecated('2.4', 'Member_model::get_authors()');
  814. return $this->get_authors($author_id, $limit, $offset);
  815. }
  816. // --------------------------------------------------------------------
  817. /**
  818. * Get Member Groups
  819. *
  820. * Returns only the title and id by default, but additional fields can be passed
  821. * and automatically added to the query either as a string, or as an array.
  822. * This allows the same function to be used for "lean" and for larger queries.
  823. *
  824. * @access public
  825. * @param array
  826. * @param array array of associative field => value arrays
  827. * @return mixed
  828. */
  829. function get_member_groups($additional_fields = array(), $additional_where = array(), $limit = '', $offset = '')
  830. {
  831. if ( ! is_array($additional_fields))
  832. {
  833. $additional_fields = array($additional_fields);
  834. }
  835. if ( ! isset($additional_where[0]))
  836. {
  837. $additional_where = array($additional_where);
  838. }
  839. if (count($additional_fields) > 0)
  840. {
  841. $this->db->select(implode(',', $additional_fields));
  842. }
  843. $this->db->select("group_id, group_title");
  844. $this->db->from("member_groups");
  845. $this->db->where("site_id", $this->config->item('site_id'));
  846. if ($limit != '')
  847. {
  848. $this->db->limit($limit);
  849. }
  850. if ($offset !='')
  851. {
  852. $this->db->offset($offset);
  853. }
  854. foreach ($additional_where as $where)
  855. {
  856. foreach ($where as $field => $value)
  857. {
  858. if (is_array($value))
  859. {
  860. $this->db->where_in($field, $value);
  861. }
  862. else
  863. {
  864. $this->db->where($field, $value);
  865. }
  866. }
  867. }
  868. $this->db->order_by('group_id, group_title');
  869. return $this->db->get();
  870. }
  871. // --------------------------------------------------------------------
  872. /**
  873. * Delete Member Group
  874. *
  875. * Deletes a member group, and optionally reassigns its members to another group
  876. *
  877. * @access public
  878. * @param int The group to be deleted
  879. * @param int The group to reassign members to
  880. * @return void
  881. */
  882. function delete_member_group($group_id = '', $reassign_group = FALSE)
  883. {
  884. if ($reassign_group !== FALSE)
  885. {
  886. // reassign current members to new group
  887. $this->db->set(array('group_id'=>$reassign_group));
  888. $this->db->where('group_id', $group_id);
  889. $this->db->update('members');
  890. }
  891. // remove the group
  892. $this->db->delete('member_groups', array('group_id' => $group_id));
  893. // remove them from uploads table
  894. $this->db->delete('upload_no_access', array('member_group' => $group_id));
  895. }
  896. // --------------------------------------------------------------------
  897. /**
  898. * Count Members
  899. *
  900. * @access public
  901. * @param int
  902. * @return int
  903. */
  904. function count_members($group_id = '', $search_value = '', $search_field = '')
  905. {
  906. $this->_prep_search_query($group_id, $search_value, $search_field);
  907. return $this->db->count_all_results('members');
  908. }
  909. // --------------------------------------------------------------------
  910. /**
  911. * Count Recrods
  912. *
  913. * @access public
  914. * @param table
  915. * @return int
  916. */
  917. function count_records($table = '')
  918. {
  919. return $this->db->count_all($table);
  920. }
  921. // --------------------------------------------------------------------
  922. /**
  923. * Count Member Entries
  924. *
  925. * @access public
  926. * @param array
  927. * @return int
  928. */
  929. function count_member_entries($member_ids = array())
  930. {
  931. if ( ! is_array($member_ids))
  932. {
  933. $member_ids = array($member_ids);
  934. }
  935. $this->db->select('entry_id');
  936. $this->db->from('channel_titles');
  937. $this->db->where_in('author_id', $member_ids);
  938. return $this->db->count_all_results();
  939. }
  940. // --------------------------------------------------------------------
  941. /**
  942. * Get Members Group Ids
  943. *
  944. * Provided a string or an array of member ids, returns an array
  945. * of unique group ids that they belong to
  946. *
  947. * @access public
  948. * @param array
  949. * @return mixed
  950. */
  951. function get_members_group_ids($member_ids = array())
  952. {
  953. if ( ! is_array($member_ids))
  954. {
  955. $member_ids = array($member_ids);
  956. }
  957. $this->db->select("group_id");
  958. $this->db->from("members");
  959. $this->db->where_in("member_id", $member_ids);
  960. $groups = $this->db->get();
  961. // superadmins are always viable
  962. $group_ids[] = 1;
  963. if ($groups->num_rows() > 0)
  964. {
  965. foreach($groups->result() as $group)
  966. {
  967. $group_ids[] = $group->group_id;
  968. }
  969. }
  970. $group_ids = array_unique($group_ids);
  971. return $group_ids;
  972. }
  973. // --------------------------------------------------------------------
  974. /**
  975. * Get Custom Member Fields
  976. *
  977. * This function retuns all custom member fields
  978. *
  979. * @access public
  980. * @param an optional member id to restrict the search on
  981. * @return object
  982. */
  983. function get_custom_member_fields($member_id = '')
  984. {
  985. if ($member_id != '')
  986. {
  987. $this->db->where('m_field_id', $member_id);
  988. }
  989. $this->db->select('m_field_id, m_field_order, m_field_label, m_field_name');
  990. $this->db->from('member_fields');
  991. $this->db->order_by('m_field_order');
  992. return $this->db->get();
  993. }
  994. // --------------------------------------------------------------------
  995. /**
  996. * Get Member By Screen Name
  997. *
  998. * @access public
  999. * @param string
  1000. * @return mixed
  1001. */
  1002. function get_member_by_screen_name($screen_name = '')
  1003. {
  1004. $this->db->select('member_id');
  1005. $this->db->from('members');
  1006. $this->db->where('screen_name', $screen_name);
  1007. return $this->db->get();
  1008. }
  1009. // --------------------------------------------------------------------
  1010. /*
  1011. * Get IP Members
  1012. *
  1013. * Used in search of ip addresses within members table
  1014. *
  1015. * @access public
  1016. * @param string
  1017. * @return mixed
  1018. */
  1019. function get_ip_members($ip_address = '', $limit = 10, $offset = 0)
  1020. {
  1021. $this->db->select('member_id, username, screen_name, ip_address, email, join_date');
  1022. $this->db->like('ip_address', $ip_address, 'both');
  1023. $this->db->from('members');
  1024. $this->db->order_by('screen_name');
  1025. $this->db->limit($limit);
  1026. $this->db->offset($offset);
  1027. return $this->db->get();
  1028. }
  1029. // --------------------------------------------------------------------
  1030. /**
  1031. * Get Group Members
  1032. *
  1033. * Returns members of a group
  1034. *
  1035. * @access public
  1036. * @param string
  1037. * @return mixed
  1038. */
  1039. function get_group_members($group_id, $order_by = 'join_date')
  1040. {
  1041. $this->db->select('member_id, username, screen_name, email, join_date');
  1042. $this->db->where('group_id', $group_id);
  1043. $this->db->from('members');
  1044. $this->db->order_by($order_by, 'desc');
  1045. return $this->db->get();
  1046. }
  1047. // --------------------------------------------------------------------
  1048. /**
  1049. * Check Duplicate
  1050. *
  1051. * Checks for duplicated member fields
  1052. *
  1053. * @access public
  1054. * @param string
  1055. * @return mixed
  1056. */
  1057. function check_duplicate($value = '', $field = 'username')
  1058. {
  1059. $this->db->like($field, $value);
  1060. $this->db->from('members');
  1061. if ($this->db->count_all_results() == 0)
  1062. {
  1063. // no duplicates
  1064. return FALSE;
  1065. }
  1066. else
  1067. {
  1068. // duplicates found
  1069. return TRUE;
  1070. }
  1071. }
  1072. // --------------------------------------------------------------------
  1073. /**
  1074. * Get Theme List
  1075. *
  1076. * Show file listing as a pull-down
  1077. *
  1078. * @access public
  1079. * @param string
  1080. * @param string
  1081. * @param string
  1082. * @return string
  1083. */
  1084. function get_theme_list($path = '')
  1085. {
  1086. if ($path == '')
  1087. {
  1088. return;
  1089. }
  1090. $themes = array();
  1091. if ($fp = @opendir($path))
  1092. {
  1093. while (false !== ($file = readdir($fp)))
  1094. {
  1095. if (@is_dir($path.$file) && strpos($file, '.') === FALSE)
  1096. {
  1097. $themes[$file] = ucwords(str_replace("_", " ", $file));
  1098. }
  1099. }
  1100. closedir($fp);
  1101. }
  1102. return $themes;
  1103. }
  1104. // --------------------------------------------------------------------
  1105. /**
  1106. * Get Profile Templates
  1107. *
  1108. * Returns an array of profile themes with the name as key, and the humanized
  1109. * name as the value
  1110. *
  1111. * @access public
  1112. * @param string The path to the themes
  1113. * @return array
  1114. */
  1115. function get_profile_templates($path = PATH_MBR_THEMES)
  1116. {
  1117. $themes = array();
  1118. $this->load->helper('directory');
  1119. foreach (directory_map($path, TRUE) as $file)
  1120. {
  1121. if (is_dir($path.$file) AND strncmp('.', $file, 1) != 0)
  1122. {
  1123. $themes[$file] = ucfirst(str_replace("_", " ", $file));
  1124. }
  1125. }
  1126. return $themes;
  1127. }
  1128. // --------------------------------------------------------------------
  1129. /**
  1130. * Insert Group Layout
  1131. *
  1132. * Inserts layout information for member groups for the publish page, saved as
  1133. * a serialized array.
  1134. *
  1135. * @access public
  1136. * @param mixed Member group
  1137. * @param int Field group
  1138. * @param array The layout of the fields
  1139. * @return bool
  1140. */
  1141. function insert_group_layout($member_groups = array(), $channel_id = '', $layout_info = array())
  1142. {
  1143. if ( ! is_array($member_groups))
  1144. {
  1145. $member_groups = array($member_groups);
  1146. }
  1147. $error_count = 0; // assume no errors so far
  1148. foreach ($member_groups as $member_group)
  1149. {
  1150. // remove all data already in there
  1151. $this->delete_group_layout($member_group, $channel_id);
  1152. // Remove layout function on the CP works by passing an empty array
  1153. if (count($layout_info) > 0)
  1154. {
  1155. $this->db->set("site_id", $this->config->item('site_id'));
  1156. $this->db->set("channel_id", $channel_id);
  1157. $this->db->set("field_layout", serialize($layout_info));
  1158. $this->db->set("member_group", $member_group);
  1159. if ( ! $this->db->insert('layout_publish'))
  1160. {
  1161. $error_count++;
  1162. }
  1163. }
  1164. }
  1165. if ($error_count > 0)
  1166. {
  1167. return FALSE;
  1168. }
  1169. return TRUE;
  1170. }
  1171. /**
  1172. * Delete Group Layout
  1173. *
  1174. * Removes layout information for member groups for the publish page.
  1175. *
  1176. * @access public
  1177. * @param mixed Member group
  1178. * @param int Field group
  1179. * @return void
  1180. */
  1181. function delete_group_layout($member_group = '', $channel_id = '')
  1182. {
  1183. $this->db->where("site_id", $this->config->item('site_id'));
  1184. $this->db->where("channel_id", $channel_id);
  1185. if ($member_group != '')
  1186. {
  1187. $this->db->where("member_group", $member_group);
  1188. }
  1189. $this->db->delete('layout_publish');
  1190. }
  1191. // --------------------------------------------------------------------
  1192. /**
  1193. * Get Group Layout
  1194. *
  1195. * Gets layout information for member groups for the publish page
  1196. *
  1197. * @access public
  1198. * @param int Member group
  1199. * @param int Field group
  1200. * @return array
  1201. */
  1202. function get_group_layout($member_group = '', $channel_id = '')
  1203. {
  1204. $this->load->model('layout_model');
  1205. return $this->layout_model->get_layout_settings(array(
  1206. 'site_id' => $this->config->item('site_id'),
  1207. 'channel_id' => $channel_id,
  1208. 'member_group' => $member_group
  1209. ));
  1210. }
  1211. // --------------------------------------------------------------------
  1212. /**
  1213. * Get All Group Layouts
  1214. *
  1215. * Gets layout information for member groups for the publish page
  1216. *
  1217. * @access public
  1218. * @param int Member group
  1219. * @param int Field group
  1220. * @return array
  1221. */
  1222. function get_all_group_layouts($channel_id = array())
  1223. {
  1224. if ( ! is_array($channel_id))
  1225. {
  1226. $channel_id = array($channel_id);
  1227. }
  1228. if ( ! empty($channel_id))
  1229. {
  1230. $this->db->where_in("channel_id", $channel_id);
  1231. }
  1232. $layout_data = $this->db->get('layout_publish');
  1233. if ($layout_data->num_rows() > 0)
  1234. {
  1235. $returned_data = $layout_data->result_array();
  1236. }
  1237. else
  1238. {
  1239. $returned_data = array();
  1240. }
  1241. return $returned_data;
  1242. }
  1243. // --------------------------------------------------------------------
  1244. /**
  1245. * Localization Default
  1246. *
  1247. * This function retuns author data for a single member
  1248. *
  1249. * @access public
  1250. * @return array
  1251. */
  1252. function get_localization_default($get_id = FALSE)
  1253. {
  1254. $this->db->select('member_id, timezone, daylight_savings, time_format');
  1255. $this->db->where('localization_is_site_default', 'y');
  1256. $query = $this->db->get('members');
  1257. if ($query->num_rows() == 1)
  1258. {
  1259. $config = array(
  1260. 'default_site_timezone' => $query->row('timezone'),
  1261. 'default_site_dst' => $query->row('daylight_savings')
  1262. );
  1263. if ($get_id)
  1264. {
  1265. $config['member_id'] = $query->row('member_id');
  1266. }
  1267. }
  1268. else
  1269. {
  1270. $config = array(
  1271. 'default_site_timezone' => '',
  1272. 'default_site_dst' => ''
  1273. );
  1274. if ($get_id)
  1275. {
  1276. $config['member_id'] = '';
  1277. }
  1278. }
  1279. return $config;
  1280. }
  1281. // --------------------------------------------------------------------
  1282. /**
  1283. * Get Notepad Content
  1284. *
  1285. * Returns the contents of a user's notepad
  1286. *
  1287. * @access public
  1288. * @return array
  1289. */
  1290. function get_notepad_content($id = '')
  1291. {
  1292. $id = $id ? $id : $this->session->userdata('member_id');
  1293. $this->db->select('notepad');
  1294. $this->db->from('members');
  1295. $this->db->where('member_id', (int) $id);
  1296. $notepad_query = $this->db->get();
  1297. if ($notepad_query->num_rows() > 0)
  1298. {
  1299. $notepad_result = $notepad_query->row();
  1300. return $notepad_result->notepad;
  1301. }
  1302. return '';
  1303. }
  1304. // --------------------------------------------------------------------
  1305. /**
  1306. * Can Access Module
  1307. *
  1308. * @access public
  1309. * @return boolean
  1310. */
  1311. function can_access_module($module, $group_id = '')
  1312. {
  1313. // Superadmin sees all
  1314. if ($this->session->userdata('group_id') == 1)
  1315. {
  1316. return TRUE;
  1317. }
  1318. if ( ! $group_id)
  1319. {
  1320. $group_id = $this->session->userdata('group_id');
  1321. }
  1322. $this->db->select('modules.module_id, module_member_groups.group_id');
  1323. $this->db->where('LOWER('.$this->db->dbprefix.'modules.module_name)', strtolower($module));
  1324. $this->db->join('module_member_groups', 'module_member_groups.module_id = modules.module_id');
  1325. $this->db->where('module_member_groups.group_id', $group_id);
  1326. $query = $this->db->get('modules');
  1327. return ($query->num_rows() === 0) ? FALSE : TRUE;
  1328. }
  1329. // --------------------------------------------------------------------
  1330. /**
  1331. * Set up the search query which is used by get_members and
  1332. * count_members. Be sure to *run* the query after calling this.
  1333. *
  1334. * @access private
  1335. * @param int
  1336. * @return int
  1337. */
  1338. private function _prep_search_query($group_id = '', $search_value = '', $search_in = '')
  1339. {
  1340. $no_search = array('password', 'salt', 'crypt_key');
  1341. if ($group_id !== '')
  1342. {
  1343. $this->db->where("members.group_id", $group_id);
  1344. }
  1345. if (is_array($search_value))
  1346. {
  1347. foreach ($search_value as $token_name => $token_value)
  1348. {
  1349. // Check to see if the token is ID
  1350. $token_name = ($token_name === 'id') ? 'member_id' : $token_name;
  1351. $this->db->like('members.'.$token_name, $token_value);
  1352. }
  1353. }
  1354. elseif ($search_value != '')
  1355. {
  1356. $search_field = 'all';
  1357. if ( ! in_array($search_in, $no_search))
  1358. {
  1359. $search_in = $search_field;
  1360. }
  1361. if ($search_in == 'all')
  1362. {
  1363. $this->db->where("(`exp_members`.`screen_name` LIKE '%".$this->db->escape_like_str($search_value)."%' OR `exp_members`.`username` LIKE '%".$this->db->escape_like_str($search_value)."%' OR `exp_members`.`email` LIKE '%".$this->db->escape_like_str($search_value)."%')", NULL, TRUE);
  1364. }
  1365. else
  1366. {
  1367. $this->db->like('members.'.$search_in, $search_value);
  1368. }
  1369. }
  1370. }
  1371. }
  1372. /* End of file member_model.php */
  1373. /* Location: ./system/expressionengine/models/member_model.php */