-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrow.php
More file actions
124 lines (101 loc) · 3.01 KB
/
row.php
File metadata and controls
124 lines (101 loc) · 3.01 KB
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
<?php
class row
{
/**
* The table object who created this row.
* @var table
*/
public $_table = null;
public $_key = 0; // If this row was SELECTed, or has been INSERTed, then we should know its id.
function row($table)
{
$this->_table = $table;
}
/**
* Saves this row, then returns it for you.
* @return row
*/
public function save()
{
// Ignore class variables
$temp = get_object_vars($this);
$data = array();
$foreignTables = array();
foreach($temp as $key => $val)
{
if($key[0] != '_' && !is_array($val) ) {
$data[$this->_table->clean($key)] = $this->_table->clean($val);
}
// Check for foreign tables
if(is_array($val) && substr($key, -5) == '-list') {
$foreignTables[substr($key, 0, -5)] = $val;
}
}
if($this->_key) // Update
{
$this->id = $this->_key;
$this->_table->rowUpdate($data, $this->_key);
}
else // Insert
{
$this->_key = $this->_table->rowInsert($data);
$this->id = $this->_key;
}
// Update foreign tables
foreach($foreignTables as $table => $data) $this->attachTable($table, $data);
return $this->id;
}
public function insertData($data)
{
foreach($data as $col => $value) {
$this->$col = trim($value);
}
$this->edit_user_id = $_SESSION['user_id'];
$this->edit_date = time();
}
public function getDefault($col) {
if(right(3, $col) == '_id') $col = right(-3, ($col));
$col = str_replace(' ', '', $this->_table->fancify($col));
$variable = '_default'.$col;
$default = false;
if(isset($this->$variable)) $default = $this->$variable;
return $default;
}
public function attachTable($otherTable, $data){
$thisTable = $this->_table->table;
$joinTable = $thisTable.'_'.$otherTable;
$thisTableIDCol = $thisTable.'_id';
$otherTableIDCol = $otherTable.'_id';
$this->_table->beginTransaction();
$myID = $this->id;
// Out with the old
$query = "DELETE FROM $joinTable WHERE $thisTableIDCol = $myID";
$this->_table->update($query);
$myUserID = $_SESSION['user_id'];
$now = time();
// In with the new
foreach($data as $otherTableID) {
$otherTableID = $this->_table->clean($otherTableID);
$query = "
INSERT INTO `$joinTable`
(`$otherTableIDCol`, `$thisTableIDCol`,`create_user_id`,`create_date`,`edit_user_id`,`edit_date`)
VALUES
('$otherTableID', '$myID', '$myUserID', '$now', '$myUserID', '$now')
";
$this->_table->update($query);
$this->_table->endTransaction();
}
}
public function getAttachedTable($otherTable) {
$thisTable = $this->_table->table;
$joinTable = $thisTable.'_'.$otherTable;
$otherTableClass = $this->_table->controller->newTable($otherTable);
$thisTableIDCol = $thisTable.'_id';
$otherTableIDCol = $otherTable.'_id';
$where = "$joinTable.$thisTableIDCol=$this->id";
$join = "LEFT JOIN $joinTable ON $joinTable.$otherTableIDCol = $otherTable.id";
$result = $otherTableClass->getRows("`$otherTable`.*", $where, "$otherTable.id", $join);
if(!$result) $result = array();
return $result;
}
}