-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathTokenHelper.php
More file actions
108 lines (94 loc) · 2.92 KB
/
TokenHelper.php
File metadata and controls
108 lines (94 loc) · 2.92 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
<?php
/**
* TokenHelper
*
* @copyright Copyright 2014, NetCommons Project
* @author Kohei Teraguchi <kteraguchi@commonsnet.org>
* @link http://www.netcommons.org NetCommons Project
* @license http://www.netcommons.org/license.txt NetCommons License
*/
App::uses('FormHelper', 'View/Helper');
App::uses('Xml', 'Utility');
/**
* TokenHelper
*
*/
class TokenHelper extends FormHelper {
/**
* Return _Token array.
*
* @param mixed $model The model name for which the form is being defined. Should
* include the plugin name for plugin models. e.g. `ContactManager.Contact`.
* If an array is passed and $options argument is empty, the array will be used as options.
* If `false` no model is used.
* @param array|string $actionUrl Action url.
* @param array $tokenFields An array of fields to generate inputs for, or null.
* @param array $hiddenFields An array of fields that exist in $tokenFields to generate inputs hidden for, or null.
* @param array $blacklist A array of fields to not create inputs for.
* @return array _Token
*/
public function getToken($model = null, $actionUrl = null, $tokenFields = null,
$hiddenFields = array(), $blacklist = null) {
if (! isset($blacklist)) {
$blacklist = array();
$blacklist += array(
'created',
'modified',
'created_user',
'modified_user'
);
}
$options = array(
'legend' => false,
'fieldset' => false
);
$fields = $tokenFields;
if (is_array($tokenFields)) {
$fields = $this->_getInputsFields($tokenFields, $hiddenFields);
}
$formHtml = $this->create($model, array('url' => $actionUrl));
$this->inputs($fields, $blacklist, $options);
$formHtml .= $this->end();
$tokens = $this->_extractToken($formHtml);
return $tokens;
}
/**
* Return input fields array added hidden option.
*
* @param array $tokenFields An array of fields to generate inputs for, or null.
* @param array $hiddenFields An array of fields that exist in $tokenFields to generate inputs hidden for, or null.
* @return array input fields
*/
protected function _getInputsFields($tokenFields, $hiddenFields) {
$fieldNames = array_keys($tokenFields);
$fields = array();
$options = array('type' => 'hidden');
foreach ($fieldNames as $fieldName) {
if (in_array($fieldName, $hiddenFields)) {
$fields[$fieldName] = $options;
continue;
}
$fields[$fieldName] = array();
}
return $fields;
}
/**
* Return _Token array.
*
* @param string $formHtml Token html string
* @return array _Token
*/
protected function _extractToken($formHtml) {
$tokens = array();
$domDocument = Xml::build($formHtml, array('return' => 'domdocument'));
$inputs = $domDocument->getElementsByTagName('input');
foreach ($inputs as $input) {
$matches = array();
$name = $input->getAttribute('name');
if (preg_match('/data\[_Token\]\[(.+)\]/', $name, $matches)) {
$tokens['_Token'][$matches[1]] = $input->getAttribute('value');
}
}
return $tokens;
}
}