Repository navigation
Expand file tree
/
Copy pathWPCRelationCollection.php
More file actions
executable file
·160 lines (127 loc) · 4.52 KB
/
Copy pathWPCRelationCollection.php
File metadata and controls
executable file
·160 lines (127 loc) · 4.52 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
152
153
154
155
156
157
158
159
160
<?php
require_once('WPCCollection.php');
require_once('WPCRelation.php');
/**
* relations records
*/
class WPCRelationCollection extends WPCCollection {
protected $table_pk = "id";
protected $table_cols = array('id', 'post_from_id', 'post_to_id');
/**
* the relationship's name in the db
*/
protected $typeslug = '';
/**
* is set to false if the relationship is stored in the same order in the database.
*/
protected $db_is_reverse = false;
/**
* returns an instance of (a subclass of) WPCRelationCollection for both types.
* if $id is set to a valid id of type $type, prefilter to get only connected relations.
*/
static function relations_by_id($typeslug, $reverse, $id=null) {
global $wpc_relationships;
if (! isset($wpc_relationships[$typeslug])) {
// XXX: there should be an _error here!
ButterLog::warn("The relationship with id $typeslug does not exist in the database.");
return null;
}
$db_reverse = $reverse ? "true" : "false";
$table = $wpc_relationships[$typeslug]->table;
$classname = ($reverse ? "Reverse" : "").str_replace(" ", "", ucwords(str_replace("_", " ", $typeslug)))."RelationRecords";
if (! class_exists($classname)){
$classdef = "class $classname extends ".__CLASS__." {
protected \$typeslug = '$typeslug';
protected \$db_is_reverse = $db_reverse;
protected \$table = '$table';
}";
eval ($classdef);
}
$relations = new $classname();
if ( !empty($id) ) {
$relations = $relations->id_is($id);
}
if ( !$reverse && $wpc_relationships[$typeslug]->ordered ) {
$relations = $relations->order_by("order");
}
return $relations;
}
function __construct() {
global $wpc_relationships;
// add metafields
$this->table_cols = array_unique(array_merge($this->table_cols,
array_keys($wpc_relationships[$this->typeslug]->fields)));
}
/**
* returns all filtered relations as array of WPCRelation objects.
*/
function results() {
return array_map(array($this, "row_to_relation"), parent::results());
}
function next () {
$next_relation = parent::next();
$ret = null;
if (!empty($next_relation)) {
if ($this->db_is_reverse) $ret = $next_relation->record_from;
else $ret = $next_relation->record_to;
if(!empty($ret)) {
if ($ret->post_status != "publish" && $ret->post_status != "without_public_page") {
return $this->next();
}
}
}
return $ret;
}
function next_relation () {
$next_relation = parent::next();
$item = null;
$ret = null;
if (!empty($next_relation)) {
if ($this->db_is_reverse) $item = $next_relation->record_from;
else $item = $next_relation->record_to;
if(!empty($item)) {
if ($item->post_status != "publish" && $item->post_status != "without_public_page") {
return $this->next_relation();
}
}
$ret = $next_relation;
}
return $ret;
}
function row_to_relation ($record) {
global $wpc_relationships;
$row = $record['t'];
return WPCRelation::new_relation(
$record['id'],
$row['post_from_id'],
$row['post_to_id'],
$this->typeslug,
$record['t']
)->write_ro($this->write_ro);
}
/**
* convenience method for filter. filters for the id.
* returns a new instance.
*/
function id_is($id) {
return $this->filter('post_from_id', $id);
}
/**
* convenience method for filter. filters for the other id.
* returns a new instance.
*/
function other_id_is($id) {
return $this->filter("post_to_id", $id);
}
/**
* this performs some magic to (re)order from and to to the expected order.
*/
function add_filter_($key, $val, $op = '=', $printf = '%s') {
if ($key == "post_from_id")
$key = $this->db_is_reverse ? "post_to_id" : "post_from_id";
else if ($key == "post_to_id")
$key = $this->db_is_reverse ? "post_from_id" : "post_to_id";
parent::add_filter_($key, $val, $op, $printf);
}
}
?>