| 1 |
<?php
|
| 2 |
// $Id: jsnippets.admin.inc,v 1.3 2009/03/02 18:26:07 karthik Exp $
|
| 3 |
|
| 4 |
/**
|
| 5 |
* Display an overview of all sections.
|
| 6 |
*/
|
| 7 |
function jsnippets_overview() {
|
| 8 |
$sections = _jsnippets_get_sections(TRUE);
|
| 9 |
$rows = array();
|
| 10 |
foreach ($sections as $section) {
|
| 11 |
// l() check_plains the name.
|
| 12 |
$rows[] = array(l($section['name'], 'admin/build/jsnippets/section/view/'. $section['section_id']), $section['jsnippets'], l(t('Edit'), 'admin/build/jsnippets/section/edit/'. $section['section_id']), l(t('Delete'), 'admin/build/jsnippets/section/delete/'. $section['section_id']));
|
| 13 |
}
|
| 14 |
$header = array(t('Section name'), t('Snippets'), array('data' => t('Operations'), 'colspan' => 2));
|
| 15 |
|
| 16 |
return theme('table', $header, $rows);
|
| 17 |
}
|
| 18 |
|
| 19 |
/**
|
| 20 |
* Add / edit a section.
|
| 21 |
*/
|
| 22 |
function jsnippets_section_edit(&$form_state, $section_id = NULL) {
|
| 23 |
$section = array('name' => '', 'form_id' => '', 'element_id' => '');
|
| 24 |
|
| 25 |
if (isset($section_id)) {
|
| 26 |
$section = _jsnippets_get_section($section_id);
|
| 27 |
$form['section_id'] = array('#type' => 'value', '#value' => $section_id);
|
| 28 |
}
|
| 29 |
|
| 30 |
$form['name'] = array(
|
| 31 |
'#type' => 'textfield',
|
| 32 |
'#title' => t('Section name'),
|
| 33 |
'#description' => t('A name to denote this section.'),
|
| 34 |
'#default_value' => $section['name'],
|
| 35 |
'#required' => TRUE
|
| 36 |
);
|
| 37 |
$form['jsnippets_form_id'] = array(
|
| 38 |
'#type' => 'textfield',
|
| 39 |
'#title' => t('Form ID'),
|
| 40 |
'#description' => t('Denotes the form that snippets are to be associated with. e.g. %example', array('%example' => 'block_admin_configure')),
|
| 41 |
'#default_value' => $section['form_id'],
|
| 42 |
'#required' => TRUE
|
| 43 |
);
|
| 44 |
$form['element_id'] = array(
|
| 45 |
'#type' => 'textfield',
|
| 46 |
'#title' => t('Element ID'),
|
| 47 |
'#description' => t('Denotes the textarea in the above form where the snippets can be inserted.'),
|
| 48 |
'#default_value' => $section['element_id'],
|
| 49 |
'#required' => TRUE
|
| 50 |
);
|
| 51 |
$form['submit'] = array('#type' => 'submit', '#value' => t('Submit'));
|
| 52 |
|
| 53 |
return $form;
|
| 54 |
}
|
| 55 |
|
| 56 |
/**
|
| 57 |
* Process a section edit form submission.
|
| 58 |
*/
|
| 59 |
function jsnippets_section_edit_submit($form, &$form_state) {
|
| 60 |
$update = array();
|
| 61 |
if (isset($form_state['values']['section_id'])) {
|
| 62 |
$update[] = 'section_id';
|
| 63 |
$form_state['redirect'] = 'admin/build/jsnippets';
|
| 64 |
}
|
| 65 |
// Fix name clash with form API.
|
| 66 |
$form_state['values']['form_id'] = $form_state['values']['jsnippets_form_id'];
|
| 67 |
|
| 68 |
$section = jsnippets_section_add($form_state['values'], $update);
|
| 69 |
|
| 70 |
if (empty($update)) {
|
| 71 |
// Redirect to new section.
|
| 72 |
$form_state['redirect'] = 'admin/build/jsnippets/section/view/'. $section['section_id'];
|
| 73 |
}
|
| 74 |
}
|
| 75 |
|
| 76 |
/**
|
| 77 |
* Display form to delete a section and its snippets.
|
| 78 |
*/
|
| 79 |
function jsnippets_section_delete($form_state, $section_id = NULL) {
|
| 80 |
if (isset($section_id) && is_numeric($section_id)) {
|
| 81 |
if ($section = _jsnippets_get_section($section_id)) {
|
| 82 |
|
| 83 |
$form['section_id'] = array('#type' => 'value', '#value' => $section_id);
|
| 84 |
$form['name'] = array('#type' => 'value', '#value' => $section['name']);
|
| 85 |
|
| 86 |
return confirm_form(
|
| 87 |
$form,
|
| 88 |
t('Are you sure you want to delete the section %name?', array('%name' => $section['name'])),
|
| 89 |
'admin/build/jsnippets',
|
| 90 |
t('Any snippets in this section will also be lost. This action cannot be undone.'),
|
| 91 |
t('Delete'),
|
| 92 |
t('Cancel')
|
| 93 |
);
|
| 94 |
}
|
| 95 |
}
|
| 96 |
|
| 97 |
drupal_not_found();
|
| 98 |
}
|
| 99 |
|
| 100 |
/**
|
| 101 |
* Process section delete form.
|
| 102 |
*/
|
| 103 |
function jsnippets_section_delete_submit($form, &$form_state) {
|
| 104 |
db_query('DELETE FROM {jsnippets_sections} WHERE section_id = %d', $form_state['values']['section_id']);
|
| 105 |
db_query('DELETE FROM {jsnippets} WHERE section_id = %d', $form_state['values']['section_id']);
|
| 106 |
drupal_set_message(t('Section %name and its snippets have been deleted.', array('%name' => $form_state['values']['name'])));
|
| 107 |
watchdog('JSnippets', 'Section %name and its snippets have been deleted.', array('%name' => $form_state['values']['name']));
|
| 108 |
|
| 109 |
$form_state['redirect'] = 'admin/build/jsnippets';
|
| 110 |
}
|
| 111 |
|
| 112 |
/**
|
| 113 |
* Display all jsnippets belonging to a section.
|
| 114 |
*/
|
| 115 |
function jsnippets_section_view($section_id) {
|
| 116 |
if ($section = _jsnippets_get_section($section_id)) {
|
| 117 |
drupal_set_title(check_plain($section['name']));
|
| 118 |
// Set breadcrumb to allow navigation back to section.
|
| 119 |
drupal_set_breadcrumb(_jsnippets_get_breadcrumb());
|
| 120 |
|
| 121 |
$jsnippets = _jsnippets_get_jsnippets($section_id);
|
| 122 |
$rows = array();
|
| 123 |
foreach ($jsnippets as $jsnippet) {
|
| 124 |
$rows[] = array(
|
| 125 |
check_plain($jsnippet['name']), l(t('Edit'), 'admin/build/jsnippets/edit/'. $jsnippet['snippet_id']),
|
| 126 |
l(t('Delete'), 'admin/build/jsnippets/delete/'. $jsnippet['snippet_id'])
|
| 127 |
);
|
| 128 |
}
|
| 129 |
$rows[] = array(
|
| 130 |
'data' => array(
|
| 131 |
array(
|
| 132 |
'data' => l(t('Add new snippet'), 'admin/build/jsnippets/add/'. $section_id),
|
| 133 |
'colspan' => 3
|
| 134 |
)
|
| 135 |
),
|
| 136 |
'class' => 'jsnippets-add'
|
| 137 |
);
|
| 138 |
|
| 139 |
$header = array(t('Snippet name'), array('data' => 'Operations', 'colspan' => 2));
|
| 140 |
|
| 141 |
return theme('table', $header, $rows);
|
| 142 |
}
|
| 143 |
|
| 144 |
return drupal_not_found();
|
| 145 |
}
|
| 146 |
|
| 147 |
/**
|
| 148 |
* Add/edit a jsnippet.
|
| 149 |
*/
|
| 150 |
function jsnippets_jsnippet_edit($form_state, $id = NULL) {
|
| 151 |
$jsnippet = array('name' => '', 'value' => '');
|
| 152 |
|
| 153 |
if (arg(3) == 'edit' && isset($id) && is_numeric($id)) {
|
| 154 |
if ($jsnippet = _jsnippets_get_jsnippet($id)) {
|
| 155 |
$form['snippet_id'] = array('#type' => 'value', '#value' => $id);
|
| 156 |
$section_id = $jsnippet['section_id'];
|
| 157 |
}
|
| 158 |
else {
|
| 159 |
drupal_not_found();
|
| 160 |
return;
|
| 161 |
}
|
| 162 |
}
|
| 163 |
// Set variables based on whether this is an add or edit form.
|
| 164 |
$title = isset($section_id) ? t('Edit snippet') : t('Add snippet');
|
| 165 |
$section_id = isset($section_id) ? $section_id : $id;
|
| 166 |
$section = _jsnippets_get_section($section_id);
|
| 167 |
|
| 168 |
// Set breadcrumb to allow navigation back to section.
|
| 169 |
drupal_set_breadcrumb(_jsnippets_get_breadcrumb(array(l($section['name'], 'admin/build/jsnippets/section/view/'. $section_id))));
|
| 170 |
|
| 171 |
$form['name'] = array(
|
| 172 |
'#type' => 'textfield',
|
| 173 |
'#title' => t('Snippet name'),
|
| 174 |
'#description' => t('A name to denote this snippet.'),
|
| 175 |
'#default_value' => $jsnippet['name'],
|
| 176 |
'#required' => TRUE
|
| 177 |
);
|
| 178 |
$form['section_id'] = array(
|
| 179 |
'#type' => 'select',
|
| 180 |
'#title' => t('Section'),
|
| 181 |
'#description' => t('The section that this snippet belongs to.'),
|
| 182 |
'#options' => _jsnippets_get_sections(),
|
| 183 |
'#default_value' => $section_id,
|
| 184 |
'#required' => TRUE
|
| 185 |
);
|
| 186 |
$form['value'] = array(
|
| 187 |
'#type' => 'textarea',
|
| 188 |
'#title' => t('Snippet value'),
|
| 189 |
'#description' => t('The data associated with this snippet.'),
|
| 190 |
'#default_value' => $jsnippet['value'],
|
| 191 |
'#rows' => 20,
|
| 192 |
'#required' => TRUE
|
| 193 |
);
|
| 194 |
$form['submit'] = array('#type' => 'submit', '#value' => t('Submit'));
|
| 195 |
|
| 196 |
return $form;
|
| 197 |
}
|
| 198 |
|
| 199 |
/**
|
| 200 |
* Process a jsnippet edit form submission.
|
| 201 |
*/
|
| 202 |
function jsnippets_jsnippet_edit_submit($form, &$form_state) {
|
| 203 |
$update = array();
|
| 204 |
if (isset($form_state['values']['snippet_id'])) {
|
| 205 |
$update[] = 'snippet_id';
|
| 206 |
$form_state['redirect'] = 'admin/build/jsnippets/section/view/'. $form_state['values']['section_id'];
|
| 207 |
}
|
| 208 |
|
| 209 |
jsnippets_snippet_add($form_state['values'], $update);
|
| 210 |
}
|
| 211 |
|
| 212 |
/**
|
| 213 |
* Display form to delete a snippet.
|
| 214 |
*/
|
| 215 |
function jsnippets_jsnippet_delete($form_state, $jsnippet_id = NULL) {
|
| 216 |
if ($jsnippet = _jsnippets_get_jsnippet($jsnippet_id)) {
|
| 217 |
$form['jsnippet_id'] = array('#type' => 'value', '#value' => $jsnippet_id);
|
| 218 |
$form['section_id'] = array('#type' => 'value', '#value' => $jsnippet['section_id']);
|
| 219 |
$form['name'] = array('#type' => 'value', '#value' => $jsnippet['name']);
|
| 220 |
|
| 221 |
return confirm_form(
|
| 222 |
$form,
|
| 223 |
t('Are you sure you want to delete the snippet %name?', array('%name' => $jsnippet['name'])),
|
| 224 |
'admin/build/jsnippets/section/view/'. $jsnippet['section_id'],
|
| 225 |
t('This action cannot be undone.'),
|
| 226 |
t('Delete'),
|
| 227 |
t('Cancel')
|
| 228 |
);
|
| 229 |
}
|
| 230 |
|
| 231 |
drupal_not_found();
|
| 232 |
}
|
| 233 |
|
| 234 |
/**
|
| 235 |
* Process snippet deletion form.
|
| 236 |
*/
|
| 237 |
function jsnippets_jsnippet_delete_submit($form, &$form_state) {
|
| 238 |
db_query('DELETE FROM {jsnippets} WHERE snippet_id = %d', $form_state['values']['jsnippet_id']);
|
| 239 |
drupal_set_message(t('Snippet %name has been deleted.', array('%name' => $form_state['values']['name'])));
|
| 240 |
watchdog('JSnippets', 'Snippet %name has been deleted.', array('%name' => $form_state['values']['name']));
|
| 241 |
|
| 242 |
$form_state['redirect'] = 'admin/build/jsnippets/section/view/'. $form_state['values']['section_id'];
|
| 243 |
}
|
| 244 |
|
| 245 |
/**
|
| 246 |
* Helper function to set a custom breadcrumb.
|
| 247 |
*
|
| 248 |
* @param Array $crumbs
|
| 249 |
* Custom breadcrumb to add to the JSnippet base.
|
| 250 |
* @return Array
|
| 251 |
* Merged custom breadcrumb.
|
| 252 |
*/
|
| 253 |
function _jsnippets_get_breadcrumb($crumbs = array()) {
|
| 254 |
$base = array(
|
| 255 |
l(t('Home'), NULL),
|
| 256 |
l(t('Administer'), 'admin'),
|
| 257 |
l(t('Site building'), 'admin/build'),
|
| 258 |
l(t('JSnippets'), 'admin/build/jsnippets'),
|
| 259 |
);
|
| 260 |
|
| 261 |
return array_merge($base, $crumbs);
|
| 262 |
}
|