PageRenderTime 14ms CodeModel.GetById 2ms app.highlight 9ms RepoModel.GetById 1ms app.codeStats 0ms

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

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