This repository has been archived by the owner on Jul 11, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlinked_select.php
74 lines (58 loc) · 1.92 KB
/
linked_select.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
<?php
/**
* Used to draw a select box and a link to add a new item
*
* @author Clifford W. Hansen ([email protected])
*/
class LinkedSelectHelper extends Helper {
var $helpers = array('Form', 'Html', 'Javascript');
function input($fieldName, $options = array(), $selected = null, $attributes = array(), $showEmpty = ''){
// Get the view object
$this->setEntity($fieldName);
// Get the fields "Human" name
if (strpos($fieldName, '.') !== false) {
$labelName = array_pop(explode('.', $fieldName));
} else {
$labelName = $fieldName;
}
if (substr($labelName, -3) == '_id') {
$labelName = substr($labelName, 0, strlen($labelName) - 3);
}
$labelName = __(Inflector::humanize(Inflector::underscore($labelName)), true);
// Get the name of the controller
$controllerName = Inflector::variable(Inflector::pluralize(preg_replace('/_id$/', '', $this->field())));
// The main url
$url = array(
'controller' => $controllerName,
'action' => 'add'
);
// Extract passed in link options
$link_options = array();
if(isset($options['link_options'])){
$link_options = $options['link_options'];
if(isset($link_options['url'])){
if(is_array($link_options['url'])){
$tmp_url = array();
foreach($link_options['url'] as $key=>$val){
$tmp_url[] = $key . '=' . $val;
}
$url[] = '?'.join('&',$tmp_url);
} else {
$url[] = '?'.$link_options['url'];
}
unset($link_options['url']);
}
unset($options['link_options']);
}
// Get any javascript that is passed in
if(isset($options['javascript'])){
$view =& ClassRegistry::getObject('view');
$this->Javascript->codeBlock($options['javascript'], array('inline'=>false));
unset($options['javascript']);
}
$return = $this->Form->input($fieldName, $options, $selected, $attributes, $showEmpty);
$return .= $this->Html->link(__('New '.$labelName,true), $url, $link_options);
return $return;
}
}
?>