/
contributor.php
95 lines (88 loc) · 2.4 KB
/
contributor.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
<?php
/**
* The file contains a model for dealing with contributors
*/
/**
* This model represents a spark contributor
*/
class Contributor extends CI_Model
{
/**
* Insert a contributor record
* @param <type> $data
* @return bool
*/
public static function insert($data)
{
$CI = &get_instance();
$data['password'] = sha1($data['password']);
$data['modified'] = date('Y-m-d H:i:s');
$data['created'] = date('Y-m-d H:i:s');
return $CI->db->insert('contributors', $data);
}
/**
* Get a contributor by his username
* @param string $username
* @return Contributor
*/
public static function findByUsername($username)
{
$CI = &get_instance();
return $CI->db->get_where('contributors', array('username' => $username))->row(0, 'Contributor');
}
/**
* Get a contributor by his id
* @param int $user_id
* @return Contributor
*/
public static function findById($user_id)
{
$CI = &get_instance();
return $CI->db->get_where('contributors', array('id' => $user_id))->row(0, 'Contributor');
}
/**
*
* @param string $username
* @param string $password
* $param Contributor If the record is found, a contributor
*/
public static function getByLogin($email, $password)
{
$CI = &get_instance();
return $CI->db->get_where('contributors', array('email' => $email, 'password' => sha1($password)))->row(0, 'Contributor');
}
/**
* Get contributor's sparks
* @return array[Spark]
*/
public function getContributions()
{
$CI = &get_instance();
$CI->load->model('spark');
return $CI->db->get_where('sparks', array('contributor_id' => $this->id))->result('Spark');
}
/**
* Update a contributor's data
* @param int $id
* @param array $data
* @return bool
*/
public static function update($id, $data)
{
$CI = &get_instance();
# Do some password handling
if(array_key_exists('password', $data))
{
if(empty($data['password']))
{
unset($data['password']);
}
else
{
$data['password'] = sha1($data['password']);
}
}
$CI->db->where('id', $id);
return $CI->db->update('contributors', $data);
}
}