Repository navigation
Expand file tree
/
Copy pathWPCRelation.php
More file actions
executable file
·151 lines (118 loc) · 4.69 KB
/
Copy pathWPCRelation.php
File metadata and controls
executable file
·151 lines (118 loc) · 4.69 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
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
<?php
/**
* The base class for records of a content type.
*
* Does not lazy-load data.
*/
abstract class WPCRelation extends WPCData {
protected $to_type;
protected $from_type;
protected $data_keys = array('id', 'record_from', 'record_to');
/**
* constructor for a Relation
*/
protected function __construct($id, $record_from, $record_to, $meta=null) {
global $wpc_content_types, $wpc_relationships;
$this->id = $id;
$this->to_type = $wpc_content_types[$wpc_relationships[$this->typeslug]->post_type_to_id];
$this->from_type = $wpc_content_types[$wpc_relationships[$this->typeslug]->post_type_from_id];
$this->meta_keys = array_keys($wpc_relationships[$this->typeslug]->fields);
// if only the numerical id is given, construct a new record object
if ($record_from && ! is_object($record_from))
$record_from = WPCRecord::new_record($record_from);
if ($record_to && ! is_object($record_to))
$record_to = WPCRecord::new_record($record_to);
$data = array(
'id' => $this->id,
"record_from" => $record_from,
"record_to" => $record_to,
);
parent::__construct($data, $meta);
}
function set($key, $val) {
if ($key === 'record_to')
return $this->to($val);
if ($key === 'record_from')
return $this->from($val);
return parent::set($key, $val);
}
function to($record) {
if (! is_object($record))
$record = WPCRecord::new_record($record, null, null, $this->to_type);
$this->data['record_to'] = $record;
return $this;
}
function from($record) {
if (! is_object($record))
$record = WPCRecord::new_record($record, null, null, $this->from_type);
$this->data['record_from'] = $record;
return $this;
}
function commit($write_ro = null, $write_wo_change=false) {
global $wpc_relationships;
$write_ro = $write_ro === null ? $this->write_ro : $write_ro;
$relation = $wpc_relationships[$this->typeslug];
$this->data = $this->data_to_update + $this->data;
if ($this->data['record_to']->id === "" || $this->data['record_from'] === "") {
ButterLog::error('From- or To-Record not given or not in database. You might need to commit them.');
return;
}
$arg = array(
'from_id' => $this->data['record_from']->id,
'to_id' => $this->data['record_to']->id,
'rel_id' => $this->typeslug,
'relation_metadata' => $this->meta_to_update,
'id' => $this->id
);
if ($this->id === null)
$res = $relation->add_relation($arg);
else {
$res = $relation->update_relation($arg);
$this->id = $res->id;
}
// invalidate metadata
$this->meta = null;
return $this;
}
protected function get_field_type ($field_key) {
$ret = "";
if ( !empty($wpc_relationships[$this->typeslug]->fields[$field_key]) )
$ret = $wpc_relationships[$this->typeslug]->fields[$field_key]->type;
return $ret;
}
protected function load_data() {
global $wpdb;
global $wpc_relationships;
if ($this->id === null)
return;
// this should inner join on the two tables and get the record's data
$table = $wpc_relationships[$this->typeslug]->table;
$stmt = $wpdb->prepare("SELECT r.* FROM $table AS r
WHERE r.id = %d", $this->id);
$row = $wpdb->get_row($stmt, ARRAY_A);
$this->data['record_from'] = WPCRecord::new_record($row['post_from_id'], null, null, $this->from_type);
$this->data['record_to'] = WPCRecord::new_record($row['post_to_id'], null, null, $this->to_type);
$this->meta = $row;
}
protected function load_meta() {
if ($this->data === null || ! empty($this->meta_keys))
// load_data will also load meta-data
return $this->load_data();
}
function delete() {
global $wpdb;
global $wpc_relationships;
$table = $wpc_relationships[$this->typeslug]->table;
$stmt = $wpdb->prepare("DELETE FROM $table WHERE id = %d", $this->id);
$wpdb->query($stmt);
}
/**
* returns a new object of the right type.
*/
static function new_relation($id, $record_from, $record_to, $typeslug, $meta=null) {
$classname = $typeslug."Relation";
self::make_specific_class($classname, $typeslug);
return new $classname($id, $record_from, $record_to, $meta);
}
}
?>