| 1 |
<?php
|
| 2 |
// $Id: scaffolding_example.module,v 1.3 2008/06/03 05:39:12 eaton Exp $
|
| 3 |
|
| 4 |
/**
|
| 5 |
* @file
|
| 6 |
* Provides example scaffolding for a basic Drupal module with CRUD.
|
| 7 |
*
|
| 8 |
* This module implements all the permissions, menu callbacks, form handling,
|
| 9 |
* installation tasks, and CRUD functions needed by most Drupal modules. You'll
|
| 10 |
* need to customize it with your own table definitions and tweak the user
|
| 11 |
* interface, obviously, but this skeleton should save quite a bit of time.
|
| 12 |
*/
|
| 13 |
|
| 14 |
/**
|
| 15 |
* Implementation of hook_menu().
|
| 16 |
*
|
| 17 |
* Defines four pages: an overview page that lists all records in the module's
|
| 18 |
* database table, add and edit pages for each record, and a confirmation page
|
| 19 |
* for deleting records.
|
| 20 |
*/
|
| 21 |
function scaffolding_example_menu() {
|
| 22 |
$items = array();
|
| 23 |
|
| 24 |
$items['admin/build/scaffolding_example'] = array(
|
| 25 |
'title' => 'Scaffolding',
|
| 26 |
'description' => 'Manage records',
|
| 27 |
'page callback' => 'drupal_get_form',
|
| 28 |
'page arguments' => array('scaffolding_example_overview_form'),
|
| 29 |
'access arguments' => array('administer scaffolding data'),
|
| 30 |
'file' => 'scaffolding_example.admin.inc',
|
| 31 |
);
|
| 32 |
|
| 33 |
$items['admin/build/scaffolding_example/sorted'] = array(
|
| 34 |
'title' => 'Sorted list',
|
| 35 |
'type' => MENU_DEFAULT_LOCAL_TASK,
|
| 36 |
'weight' => -10,
|
| 37 |
);
|
| 38 |
|
| 39 |
$items['admin/build/scaffolding_example/pager'] = array(
|
| 40 |
'title' => 'Paged list',
|
| 41 |
'description' => 'Manage records',
|
| 42 |
'page callback' => 'scaffolding_example_overview_pager',
|
| 43 |
'access arguments' => array('administer scaffolding data'),
|
| 44 |
'type' => MENU_LOCAL_TASK,
|
| 45 |
'weight' => -5,
|
| 46 |
'file' => 'scaffolding_example.admin.inc',
|
| 47 |
);
|
| 48 |
|
| 49 |
$items['admin/build/scaffolding_example/add'] = array(
|
| 50 |
'title' => 'Add record',
|
| 51 |
'page callback' => 'drupal_get_form',
|
| 52 |
'page arguments' => array('scaffolding_example_form'),
|
| 53 |
'access arguments' => array('administer scaffolding data'),
|
| 54 |
'type' => MENU_LOCAL_TASK,
|
| 55 |
'file' => 'scaffolding_example.admin.inc',
|
| 56 |
);
|
| 57 |
|
| 58 |
$items['admin/build/scaffolding_example/%scaffolding_example_record/edit'] = array(
|
| 59 |
'title' => 'Edit record',
|
| 60 |
'page callback' => 'drupal_get_form',
|
| 61 |
'page arguments' => array('scaffolding_example_form', 3),
|
| 62 |
'access arguments' => array('administer scaffolding data'),
|
| 63 |
'type' => MENU_CALLBACK,
|
| 64 |
'file' => 'scaffolding_example.admin.inc',
|
| 65 |
);
|
| 66 |
|
| 67 |
$items['admin/build/scaffolding_example/%scaffolding_example_record/delete'] = array(
|
| 68 |
'title' => 'Delete record',
|
| 69 |
'page callback' => 'drupal_get_form',
|
| 70 |
'page arguments' => array('scaffolding_example_delete_confirm', 3),
|
| 71 |
'access arguments' => array('administer scaffolding data'),
|
| 72 |
'type' => MENU_CALLBACK,
|
| 73 |
'file' => 'scaffolding_example.admin.inc',
|
| 74 |
);
|
| 75 |
|
| 76 |
$items['scaffolding_example'] = array(
|
| 77 |
'title' => 'Scaffolding records',
|
| 78 |
'description' => 'Lists records saved by the Scaffolding Example module.',
|
| 79 |
'page callback' => 'scaffolding_example_page',
|
| 80 |
'access arguments' => array('view content'),
|
| 81 |
'file' => 'scaffolding_example.pages.inc',
|
| 82 |
);
|
| 83 |
return $items;
|
| 84 |
}
|
| 85 |
|
| 86 |
/**
|
| 87 |
* Implementation of hook_perm().
|
| 88 |
*
|
| 89 |
* Defines access permissions that may be assigned to roles and used to restrict
|
| 90 |
* access.
|
| 91 |
*/
|
| 92 |
function scaffolding_example_perm() {
|
| 93 |
return array('administer scaffolding data');
|
| 94 |
}
|
| 95 |
|
| 96 |
/**
|
| 97 |
* Implementation of hook_theme().
|
| 98 |
*
|
| 99 |
* Returns information about every themable function defined by the module.
|
| 100 |
*/
|
| 101 |
function scaffolding_example_theme() {
|
| 102 |
$items = array();
|
| 103 |
$items['scaffolding_example_overview_form'] = array(
|
| 104 |
'arguments' => array('form' => array()),
|
| 105 |
'file' => 'scaffolding_example.admin.inc',
|
| 106 |
);
|
| 107 |
$items['scaffolding_example_record'] = array(
|
| 108 |
'arguments' => array('record' => array()),
|
| 109 |
'file' => 'scaffolding_example.pages.inc',
|
| 110 |
);
|
| 111 |
return $items;
|
| 112 |
}
|
| 113 |
|
| 114 |
/**
|
| 115 |
* Loader function for individual records.
|
| 116 |
*
|
| 117 |
* Because we use '%scaffolding_example_record' as a wildcard in our hook_menu()
|
| 118 |
* handler, this function will also be called automatically when we go to edit
|
| 119 |
* or delete a record. Thanks, Menu API!.
|
| 120 |
*
|
| 121 |
* @param $record_id
|
| 122 |
* An int containing the ID of a record.
|
| 123 |
* @return
|
| 124 |
* A single record in array format, or FALSE if none matched the incoming ID.
|
| 125 |
*/
|
| 126 |
function scaffolding_example_record_load($record_id) {
|
| 127 |
$sql = "SELECT * FROM {scaffolding_record} WHERE record_id = %d";
|
| 128 |
$result = db_query($sql, $record_id);
|
| 129 |
if ($record = db_fetch_array($result)) {
|
| 130 |
return $record;
|
| 131 |
}
|
| 132 |
else {
|
| 133 |
return FALSE;
|
| 134 |
}
|
| 135 |
}
|
| 136 |
|
| 137 |
/**
|
| 138 |
* Public loader function for the full collection of records.
|
| 139 |
*
|
| 140 |
* In situations where the module's data rarely changes, or is being used
|
| 141 |
* frequently (for example, loaded and processed on every page load), this
|
| 142 |
* is a prime candidate for caching. See The Beginner's Guide to Caching at
|
| 143 |
* http://www.lullabot.com/articles/a_beginners_guide_to_caching_data for more
|
| 144 |
* details.
|
| 145 |
*
|
| 146 |
* This function assumes that results should be sorted by 'weight' -- if your
|
| 147 |
* module doesn't store a weight column on its records, or if you need to sort
|
| 148 |
* on some other property, this function's SQL should be updated as well.
|
| 149 |
*
|
| 150 |
* @return
|
| 151 |
* An array of all records, keyed by id.
|
| 152 |
*/
|
| 153 |
function scaffolding_example_record_load_all() {
|
| 154 |
$sql = 'SELECT * FROM {scaffolding_record} ORDER BY weight ASC';
|
| 155 |
$result = db_query($sql);
|
| 156 |
|
| 157 |
$records = array();
|
| 158 |
while ($record = db_fetch_array($result)) {
|
| 159 |
$records[$record['record_id']] = $record;
|
| 160 |
}
|
| 161 |
return $records;
|
| 162 |
}
|
| 163 |
|
| 164 |
/**
|
| 165 |
* Inserts a new record, or updates an existing one.
|
| 166 |
*
|
| 167 |
* Automatically inserts or updates, based on whether the record's unique ID has
|
| 168 |
* been set yet. Because drupal_write_record() updates the record itself (adding
|
| 169 |
* the unique ID after the database has been updated), we return the record
|
| 170 |
* after saving it.
|
| 171 |
*
|
| 172 |
* This allows any calling function to check the id of the returned record and
|
| 173 |
* act on the ID as needed (redirecting to a 'view' page after inserting, etc).
|
| 174 |
*
|
| 175 |
* @param $record
|
| 176 |
* A record to be saved. If $record['record_id'] is set, the record will be updated.
|
| 177 |
* Otherwise, a new record will be inserted into the database.
|
| 178 |
* @return
|
| 179 |
* The saved record, with its ID set.
|
| 180 |
*/
|
| 181 |
function scaffolding_example_record_save($record) {
|
| 182 |
if (isset($record['record_id'])) {
|
| 183 |
drupal_write_record('scaffolding_record', $record, 'record_id');
|
| 184 |
}
|
| 185 |
else {
|
| 186 |
drupal_write_record('scaffolding_record', $record);
|
| 187 |
}
|
| 188 |
return $record;
|
| 189 |
}
|
| 190 |
|
| 191 |
/**
|
| 192 |
* Deletes a record, given its unique ID.
|
| 193 |
*
|
| 194 |
* @param $record_id
|
| 195 |
* An int containing the ID of a record.
|
| 196 |
*/
|
| 197 |
function scaffolding_example_record_delete($record_id) {
|
| 198 |
$sql = 'DELETE FROM {scaffolding_record} WHERE record_id = %d';
|
| 199 |
db_query($sql, $record_id);
|
| 200 |
}
|