-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfpStash.php
151 lines (135 loc) · 4.75 KB
/
fpStash.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
<?php
/**
* Adds a ::stash() method to fActiveRecord that will store the record in the
* session and redirect to a URL. fpStash::unstash() retrieves the record from
* the session.
*
* @copyright Copyright (c) 2010 Will Bond
* @author Will Bond [wb] <[email protected]>
* @license http://flourishlib.com/license
*
* @requires Flourish r912
* @requires Moor 1.0.0b7
*
* @version 1.0.0
* @changes 1.0.0 The initial implementation [wb, 2010-09-22]
*/
class fpStash
{
/**
* Adds the fActiveRecord::stash() method
*
* @param mixed $class The class name or fActiveRecord object
* @return void
*/
static public function extend()
{
fORM::registerActiveRecordMethod(
'*',
'stash',
'fpStash::stash'
);
fORM::registerReflectCallback(
'*',
'fpStash::reflect'
);
}
/**
* Returns the user to the page where the record was stashed before arriving
* at the current page
*
* @return void
*/
static public function getOrigin()
{
return fSession::delete('fpStash::return_url::' . Moor::getActiveCallback());
}
/**
* Saves the current record and redirect to a Moor callback
*
* @internal
*
* @param fActiveRecord $object The fActiveRecord instance
* @param array &$values The current values
* @param array &$old_values The old values
* @param array &$related_records Any records related to this record
* @param array &$cache The cache array for the record
* @param string $method_name The method that was called
* @param array $parameters The parameters passed to the method
* @return string The URL requested
*/
static public function stash($object, &$values, &$old_values, &$related_records, &$cache, $method_name, $parameters)
{
$callback = $parameters[0];
$expanded_callback = $callback;
if (strpos($callback, '*::') === 0) {
$expanded_callback = Moor::getActiveClass() . substr($callback, 1);
}
if (strpos($callback, '*\\') === 0 || preg_match('/^\*_[A-Z][A-Za-z0-9]*::/', $callback)) {
$expanded_callback = Moor::getActiveNamespace() . substr($callback, 1);
}
fSession::set('fpStash::record::' . Moor::getActiveCallback(), $object);
fSession::set('fpStash::return_url::' . $expanded_callback, fURL::getWithQueryString());
fURL::redirect(Moor::linkTo($callback));
}
/**
* Adjusts the fActiveRecord::reflect() signatures of columns that have been added by this class
*
* @internal
*
* @param string $class The class to reflect
* @param array &$signatures The associative array of `{method name} => {signature}`
* @param boolean $include_doc_comments If doc comments should be included with the signature
* @return void
*/
static public function reflect($class, &$signatures, $include_doc_comments)
{
$signature = '';
if ($include_doc_comments) {
$signature .= "/**\n";
$signature .= " * Saves the record in the session and redirects a user to a Moor callback\n";
$signature .= " * \n";
$signature .= " * @param string \$callback_string A callback string of a Moor route to redirect\n";
$signature .= " * @return void\n";
$signature .= " */\n";
}
$signature .= 'public function stash($callback_string)';
$signatures['stash'] = $signature;
}
/**
* Returns a stashed record if present
*
* @return fActiveRecord|NULL The stashed record
*/
static public function unstash()
{
return fSession::delete('fpStash::record::' . Moor::getActiveCallback());
}
/**
* Forces use as a static class
*
* @return fpStash
*/
private function __construct() { }
}
/**
* Copyright (c) 2010 Will Bond <[email protected]>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/