/
jmd_form_toggle.php
199 lines (179 loc) · 4.19 KB
/
jmd_form_toggle.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
<?php
$plugin = array(
'description' => 'Toggleable admin-forms.',
'version' => '0.3',
'type' => 1,
);
if (!defined('txpinterface')) include '../zem_tpl.php';
if (0) {
?>
# --- BEGIN PLUGIN HELP ---
h1. jmd_form_toggle
"Forum thread":http://forum.textpattern.com/viewtopic.php?id=27725, "hg repo":http://www.bitbucket.org/jmdeldin/jmd_form_toggle/
h2. Credits
Thanks to "Steve Dickinson":http://txp-plugins.netcarving.com/ for making this work.
# --- END PLUGIN HELP ---
<?php
}
# --- BEGIN PLUGIN CODE ---
if (txpinterface === 'admin')
{
if (gps('event') === 'form')
{
ob_start('jmd_form_toggle_head');
ob_start('jmd_form_toggle');
}
if (safe_field('css', 'txp_css', 'name="jmd_form_toggle"') === FALSE)
{
$css = <<<EOD
/*Collapse certain types by default
#type_article,
#type_comment,
#type_file,
#type_link,
#type_misc
{
display: none;
}
*/
/*"Create new form" link*/
#jmd_form_toggle .action
{
display: block;
margin: 0 0 0.5em;
}
#jmd_form_toggle h3
{
cursor: pointer;
font: 14px Georgia, serif;
margin: 0.5em 0 0;
}
#jmd_form_toggle table
{
border-collapse: collapse;
width: 100%;
}
#jmd_form_toggle td
{
border-bottom: 1px solid #ddd;
vertical-align: middle;
}
#jmd_form_toggle .single td
{
padding: 0.3em 0;
}
#jmd_form_toggle .checkbox
{
text-align: right;
}
#jmd_form_toggle tr
{}
#jmd_form_toggle .current
{
background: #ffffcc;
}
EOD;
$css = base64_encode($css);
safe_insert("txp_css", "name='jmd_form_toggle', css='$css'");
}
}
/**
* Adds headers for each form type.
*
* @param string $buffer
*/
function jmd_form_toggle($buffer)
{
global $DB, $essential_forms, $step;
if (empty($DB))
{
$DB = new DB();
}
$curForm = (gps('name') ? gps('name') : 'default');
$out = sLink('form', 'form_create', gTxt('create_new_form'), 'action');
$rs = safe_rows('name, type', 'txp_form',
'name !="" order by type, name asc');
foreach ($rs as $form)
{
$forms[$form['type']][] = $form['name'];
}
$types = array_keys($forms);
foreach ($types as $type)
{
$out .= <<<EOD
<h3 class="plain" onclick="jmd_form_toggle('type_{$type}');">{$type}</h3>
<table id="type_{$type}">
EOD;
$count = count($forms[$type]);
for ($i = 0; $i < $count; $i++)
{
$formName = $forms[$type][$i];
$formLink = eLink('form', 'form_edit', 'name', $formName, $formName);
$checkbox = '<input type="checkbox" name="selected_forms[]"
value="' . $formName . '"/>';
$class = '';
if (in_array($formName, $essential_forms))
{
$checkbox = '';
$class .= ' single';
}
if ($curForm === $formName)
{
$class .= ' current';
$formLink = tag($formName, 'strong');
}
$out .= <<<EOD
<tr class="{$class}">
<td>
{$formLink}
</td>
<td class="checkbox">
{$checkbox}
</td>
</tr>
EOD;
}
$out .= '</table>';
}
$out .= '<input type="hidden" name="event" value="form" />';
$pattern = '/<table cellpadding="0" cellspacing="0" border="0" id="list" align="center">(.*)<input type="hidden" name="event" value="form" \/>/s';
return preg_replace($pattern, tag($out, 'div', ' id="jmd_form_toggle"'),
$buffer);
}
/**
* Inserts CSS and JS into the head.
*
* @param string $buffer
*/
function jmd_form_toggle_head($buffer)
{
$out = <<<EOD
<script type="text/javascript">
/**
* Toggle an element's display.
*
* @param string id
*/
function jmd_form_toggle(id)
{
var el = document.getElementById(id);
if (el)
{
if (document.defaultView.getComputedStyle(el, '').getPropertyValue('display') == 'none')
{
el.style.display = 'table';
}
else
{
el.style.display = 'none';
}
}
};
</script>
<link href="./css.php?n=jmd_form_toggle" rel="stylesheet" type="text/css"/>
EOD;
$find = '</head>';
return str_replace($find, $out . $find, $buffer);
}
# --- END PLUGIN CODE ---
?>