-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathAction.class.php
164 lines (121 loc) · 3.73 KB
/
Action.class.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
<?php
/**
* Action object for the E project
*
* PHP version 7
*
* ------
* This project uses the EmPHyre microframework,
* and is built off the EmPHyre example files
*
* Written by Julian Haagsma.
*
* @category Classes
* @package EmPHyre
* @author Julian Haagsma <jhaagsma@gmail.com>
* @license All files are licensed under the MIT License.
* @link https://demo3.phasesensors.com
* @since Pulled apart Nov 2016
*/
namespace EmPHyre;
class Action extends \EmPHyre\CRUD
{
protected static $tableName = 'user_actions';
protected static $primaryKey = 'action_id';
private static $flags = null;
public function initialize()
{
parent::initialize();
$this->action_flag = self::getActionFlag($this->action_type);
}//end initialize()
public static function actions($user_id = null)
{
self::$db = Container::getDb();
if ($user_id != null) {
return self::$db->pquery(
"SELECT action_id FROM user_actions WHERE user_id = ? ORDER BY `time` DESC",
$user_id
)->fetchFieldSet();
}
return self::$db->pquery(
"SELECT action_id FROM user_actions ORDER BY `time` DESC"
)->fetchFieldSet();
}//end actions()
public function getUserID()
{
return $this->user_id;
}//end getUserID()
public function userDisplay()
{
$user = Container::newUser($this->user_id);
return $user->display();
}//end userDisplay()
public function timeDisplay($extra = false)
{
$display = date('Y-m-d H:i:s', $this->time);
if (!$extra) {
return $display;
}
return $display.' ('.datetime($this->time).')';
}//end timeDisplay()
public function display()
{
switch ($this->action_flag) {
default:
return $this->action_flag.': '.$this->foreign_key;
}
}//end display()
public function getActionTime()
{
return $this->time;
}//end getActionTime()
public function getActionUserTime($extra = false)
{
return $this->userDisplay().' at '.$this->timeDisplay($extra);
}//end getActionUserTime()
public static function addAction(
$user_id,
$action_flag,
$foreign_key = null,
$foreign_misc = null,
$time = 0
) {
if ($time == 0) {
$time = time();
}
self::$db = Container::getDb();
$action_type = self::getActionType($action_flag);
$action_id = self::$db->pquery(
"INSERT INTO user_actions
SET user_id=?, `time`=?, action_type=?, foreign_key=?, foreign_misc=?",
$user_id,
$time,
$action_type,
$foreign_key,
$foreign_misc
)->insertId();
return new Result("ADDED_ACTION", $action_id, true);
}//end addAction()
public static function getActionType($action_flag)
{
$action_type = self::$db->pquery(
"SELECT action_type FROM action_types WHERE action_flag = ?",
$action_flag
)->fetchField();
if (!$action_type) {
$action_type = self::$db->pquery(
"INSERT INTO action_types SET action_flag = ?",
$action_flag
)->insertId();
}
return $action_type;
}//end getActionType()
public static function getActionFlag($action_type)
{
$action_flag = self::$db->pquery(
"SELECT action_flag FROM action_types WHERE action_type = ?",
$action_type
)->fetchField();
return $action_flag;
}//end getActionFlag()
}//end class