1
|
|
|
<?php |
2
|
|
|
namespace GV; |
3
|
|
|
|
4
|
|
|
/** If this file is called directly, abort. */ |
5
|
|
|
if ( ! defined( 'GRAVITYVIEW_DIR' ) ) { |
6
|
|
|
die(); |
7
|
|
|
} |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* The multi-entry Entry implementation. |
11
|
|
|
* |
12
|
|
|
* An entry that is really a join of 2+ entries. |
13
|
|
|
* Used for JOINS in the \GF_Query component. |
14
|
|
|
*/ |
15
|
|
|
class Multi_Entry extends Entry implements \ArrayAccess { |
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* The entries in this form. |
18
|
|
|
*/ |
19
|
|
|
public $entries = array(); |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var string The identifier of the backend used for this entry. |
23
|
|
|
* @api |
24
|
|
|
* @since 2.0 |
25
|
|
|
*/ |
26
|
|
|
public static $backend = 'multi'; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Initialization. |
30
|
|
|
*/ |
31
|
12 |
|
private function __construct() { |
32
|
12 |
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Construct a multientry from an array of entries. |
36
|
|
|
* |
37
|
|
|
* @param \GV\Entry[] $entries The entries. |
38
|
|
|
* |
39
|
|
|
* @return \GV\Multi_Entry A multientry object. |
40
|
|
|
*/ |
41
|
12 |
|
public static function from_entries( $entries ) { |
42
|
12 |
|
$_entry = new self(); |
43
|
12 |
|
foreach ( $entries as &$entry ) { |
44
|
12 |
|
if ( ! $entry instanceof Entry ) { |
45
|
|
|
continue; |
46
|
|
|
} |
47
|
12 |
|
$_entry->entries[ $entry['form_id'] ] = &$entry; |
48
|
|
|
} |
49
|
12 |
|
return $_entry; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Fake legacy template support. |
54
|
|
|
* |
55
|
|
|
* Take the first entry and set it as the current entry. |
56
|
|
|
* But support nesting. |
57
|
|
|
* |
58
|
|
|
* @return array See \GV\Entry::as_entry() |
59
|
|
|
*/ |
60
|
6 |
|
public function as_entry() { |
61
|
6 |
|
$_entry = array(); |
62
|
|
|
|
63
|
6 |
|
if ( $entry = reset( $this->entries ) ) { |
64
|
6 |
|
$_entry = $entry->as_entry(); |
65
|
|
|
|
66
|
6 |
|
foreach ( $this->entries as $entry ) { |
67
|
6 |
|
$entry = $entry->as_entry(); |
68
|
6 |
|
$_entry['_multi'][ $entry['form_id'] ] = $entry; |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|
72
|
6 |
|
return $_entry; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Return the link to this multi entry in the supplied context. |
77
|
|
|
* |
78
|
|
|
* @api |
79
|
|
|
* @since 2.2 |
80
|
|
|
* |
81
|
|
|
* @param \GV\View|null $view The View context. |
82
|
|
|
* @param \GV\Request $request The Request (current if null). |
83
|
|
|
* @param boolean $track_directory Keep the housing directory arguments intact (used for breadcrumbs, for example). Default: true. |
84
|
|
|
* |
85
|
|
|
* @return string The permalink to this entry. |
86
|
|
|
*/ |
87
|
1 |
|
public function get_permalink( \GV\View $view = null, \GV\Request $request = null, $track_directory = true ) { |
88
|
1 |
|
$slugs = array(); |
89
|
1 |
|
add_filter( 'gravityview/entry/slug', $callback = function( $slug ) use ( &$slugs ) { |
90
|
1 |
|
$slugs[] = $slug; |
91
|
1 |
|
return implode( ',', $slugs ); |
92
|
1 |
|
}, 10, 1 ); |
93
|
|
|
|
94
|
1 |
|
foreach ( $this->entries as $entry ) { |
95
|
1 |
|
$permalink = call_user_func_array( array( $entry, __FUNCTION__ ), func_get_args() ); |
96
|
|
|
} |
97
|
|
|
|
98
|
1 |
|
remove_filter( 'gravityview/entry/slug', $callback ); |
99
|
|
|
|
100
|
1 |
|
return $permalink; |
|
|
|
|
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* ArrayAccess compatibility layer with a Gravity Forms entry array. |
105
|
|
|
* |
106
|
|
|
* @internal |
107
|
|
|
* @deprecated |
108
|
|
|
* @since 2.0 |
109
|
|
|
* @return bool Whether the offset exists or not. |
110
|
|
|
*/ |
111
|
9 |
|
public function offsetExists( $offset ) { |
112
|
9 |
|
return isset( $this->entries[ $offset ] ); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* ArrayAccess compatibility layer with a Gravity Forms entry array. |
117
|
|
|
* |
118
|
|
|
* Maps the old keys to the new data; |
119
|
|
|
* |
120
|
|
|
* @internal |
121
|
|
|
* @deprecated |
122
|
|
|
* @since 2.0 |
123
|
|
|
* |
124
|
|
|
* @return mixed The value of the requested entry data. |
125
|
|
|
*/ |
126
|
10 |
|
public function offsetGet( $offset ) { |
127
|
10 |
|
if ( ! $this->offsetExists( $offset ) ) { |
|
|
|
|
128
|
5 |
|
return null; |
129
|
|
|
} |
130
|
10 |
|
return $this->entries[ $offset ]; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* ArrayAccess compatibility layer with a Gravity Forms entry array. |
135
|
|
|
* |
136
|
|
|
* @internal |
137
|
|
|
* @deprecated |
138
|
|
|
* @since 2.0 |
139
|
|
|
* |
140
|
|
|
* @return void |
141
|
|
|
*/ |
142
|
|
|
public function offsetSet( $offset, $value ) { |
143
|
|
|
gravityview()->log->error( 'The underlying multi entry is immutable. This is a \GV\Entry object and should not be accessed as an array.' ); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* ArrayAccess compatibility layer with a Gravity Forms entry array. |
148
|
|
|
* |
149
|
|
|
* @internal |
150
|
|
|
* @deprecated |
151
|
|
|
* @since 2.0 |
152
|
|
|
* @return void |
153
|
|
|
*/ |
154
|
|
|
public function offsetUnset( $offset ) { |
155
|
|
|
gravityview()->log->error( 'The underlying multi entry is immutable. This is a \GV\Entry object and should not be accessed as an array.' ); |
156
|
|
|
} |
157
|
|
|
} |
158
|
|
|
|