/[drupal]/contributions/modules/contento/contento.module
ViewVC logotype

Contents of /contributions/modules/contento/contento.module

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.1 - (show annotations) (download) (as text)
Wed Jun 7 16:19:21 2006 UTC (3 years, 5 months ago) by jareyero
Branch: MAIN
CVS Tags: HEAD
Branch point for: DRUPAL-4-7
File MIME type: text/x-php
New module for simple content types
1 <?php
2 // $Id$
3
4 /**
5 * @file
6 * Allows administrators to define new content types
7 *
8 * Jose A. Reyero, http://www.reyero.net
9 */
10
11 /**
12 * Implementation of hook_help().
13 */
14 function contento_help($section) {
15 switch ($section) {
16 case 'admin/modules#description':
17 return t('Allows administrators to define new simple, story like, content types.');
18 }
19
20 if (strpos($section, 'node/add#') === 0 && $type = contento_get_type(substr($section, 9))) {
21 return t($type->description);
22 } elseif(strpos($section, 'node/add/') === 0 && $type = contento_get_type(substr($section, 9))) {
23 return t($type->help);
24 }
25 }
26
27 /**
28 * Implementation of hook_perm().
29 */
30 function contento_perm() {
31 $perms = array('administer content types');
32 foreach (contento_type_list() as $name => $type) {
33 $perms[] = "create $name content";
34 $perms[] = "edit own $name content";
35 $perms[] = "administer $name content";
36 }
37 return $perms;
38 }
39
40 /**
41 * Implementation of hook_menu().
42 */
43 function contento_menu($may_cache) {
44 $items = array();
45 $access = user_access('administer content types');
46
47 if ($may_cache) {
48 /*
49 $items[] = array('path' => 'admin/settings/content-types', 'title' => t('content types'),
50 'callback' => 'contento_admin_types_overview', 'access' => $access);
51 */
52 $items[] = array('path' => 'admin/settings/content-types/node', 'title' => t('content types'),
53 'type' => MENU_DEFAULT_LOCAL_TASK,
54 'weight' => 0,
55 'access' => $access);
56
57 $items[] = array('path' => 'admin/settings/content-types/add', 'title' => t('add content type'),
58 'type' => MENU_LOCAL_TASK,
59 'weight' => 10,
60 'callback' => 'contento_admin_type_add', 'access' => $access);
61
62 $items[] = array('path' => 'admin/settings/content-types/delete', 'title' => t('delete content type'),
63 'type' => MENU_CALLBACK,
64 'weight' => 10,
65 'callback' => 'contento_admin_type_delete', 'access' => $access);
66
67 foreach (contento_type_list() as $name => $type) {
68 $items[] = array('path' => "node/add/$name", 'title' => t($type['label']),
69 'access' => user_access("create $name content"));
70 }
71 }
72 return $items;
73 }
74
75 /**
76 * Return a list of all content types.
77 */
78 function contento_type_list() {
79 static $content_type;
80
81 if (!isset($content_type)) {
82 $content_type = array();
83 $result = db_query('SELECT * FROM {content_type} nt ORDER BY nt.name ASC');
84 while ($type= db_fetch_array($result)) {
85 $content_type[$type['name']] = $type;
86 }
87 }
88
89 return $content_type;
90 }
91
92 /**
93 * Returns content type data as object
94 */
95 function contento_get_type($type){
96 $content_type = contento_type_list();
97 return isset($content_type[$type]) ? (object)$content_type[$type] : NULL;
98 }
99
100 /**
101 * Hooks for node system
102 */
103
104 /**
105 * Implementation of hook_access().
106 */
107 function contento_access($op, $node) {
108 global $user;
109 $type = is_string($node) ? $node : (is_array($node) ? $node['type'] : $node->type);
110
111 if(user_access("administer $type content")){
112 return TRUE;
113 } elseif ($op == 'create') {
114 return user_access("create $type content");
115 } elseif ($op == 'update' || $op == 'delete') {
116 if (user_access("edit own $type content") && ($user->uid == $node->uid)) {
117 return TRUE;
118 }
119 }
120 }
121 /**
122 * Implementation of hook_node_info().
123 */
124 function contento_node_info() {
125 return contento_type_list();
126 }
127 /**
128 * Implementation of hook_form().
129 */
130 function contento_form(&$node) {
131 $type = contento_get_type($node->type);
132 $form['title'] = array('#type' => 'textfield', '#title' => t($type->title_label), '#required' => TRUE, '#default_value' => $node->title, '#weight' => -5);
133 $form['body_filter']['body'] = array('#type' => 'textarea', '#title' => t($type->body_label), '#default_value' => $node->body, '#rows' => 20, '#required' => TRUE);
134 $form['body_filter']['format'] = filter_form($node->format);
135 return $form;
136 }
137
138 /**
139 * Content type administration
140 */
141
142 /**
143 * Menu callback: Add new content type
144 */
145 function contento_admin_type_add(){
146 $form = contento_type_form();
147
148 return drupal_get_form('contento_type_add', $form);
149 }
150
151 /**
152 * Forms API callback
153 */
154 function contento_type_add_submit($form_id, $form_values){
155 $values = $form_values['content'];
156 db_query("INSERT INTO {content_type}(name, base, label, description, help, title_label, body_label) VALUES('%s', '%s', '%s', '%s', '%s', '%s', '%s')", $values['name'], $values['base'], $values['label'], $values['description'], $values['help'], $values['title_label'], $values['body_label']);
157 // node_type_cache_clear();
158 drupal_set_message(t('Added new content type %type.', array('%type' => theme('placeholder', $values['name']))));
159 // Rebuild the menu to reflect new content type
160 menu_rebuild();
161 return 'admin/settings/content-types';
162 }
163
164 /**
165 * Add content type settings
166 */
167 function contento_form_alter($form_id, &$form){
168 // Content type settings
169 if (isset($form['type']) && $form['type']['#value'] .'_node_settings' == $form_id && $type = contento_get_type($form['type']['#value']) ) {
170 $form += contento_type_form($type);
171 $form['#submit']['contento_type_configure_submit'] = NULL;
172 }
173 }
174
175 function contento_type_configure_submit($form_id, $form_values) {
176 $values = $form_values['content'];
177 db_query("UPDATE {content_type} SET label = '%s', description = '%s', help = '%s', title_label = '%s', body_label = '%s' WHERE name = '%s'", $values['label'], $values['description'], $values['help'], $values['title_label'], $values['body_label'], $values['name']);
178 //node_type_cache_clear();
179 unset($form_values['content']);
180 // system_settings_form_submit($form_id, $form_values);
181 return 'admin/settings/content-types';
182 }
183
184 /**
185 * Delete content types
186 */
187
188 function contento_admin_type_delete($type) {
189 $edit = isset($_POST['edit']) ? $_POST['edit'] : array();
190
191 if ($edit['confirm']) {
192 contento_admin_type_delete_action($type, $edit['delete-content']);
193 drupal_set_message(t('Deletion complete.'));
194 drupal_goto('admin/settings/content-types');
195 }
196 else {
197 $type = contento_get_type($type);
198 $options = array(t('Delete this content type but preserve data of this content type in the database'), t('Delete this content type and all data of this content type'));
199 $form['delete-content'] = array(
200 '#type' => 'radios',
201 '#options' => $options,
202 '#title' => t('Delete all content of type %content-type', array('%content-type' => theme('placeholder', $type->label)) ),
203 '#default_value' => 0
204 );
205 return confirm_form(
206 'content_type_delete',
207 $form,
208 t('Are you sure you want to delete the content type %content-type?', array('%content-type' => theme('placeholder', $type->label))),
209 'admin/node/content-types',
210 t('This action cannot be undone.'),
211 t('Delete'),
212 t('Cancel')
213 );
214 }
215 }
216
217 /**
218 * Delete content type and/or all data of that type.
219 *
220 * TO-DO: If there are too many nodes this could take too long
221 */
222 function contento_admin_type_delete_action($type, $delete_content = FALSE) {
223 db_query("DELETE FROM {content_type} WHERE name = '%s'", $type);
224 if ($delete_content) {
225 $result = db_query("SELECT nid FROM {node} WHERE type = '%s'", $type);
226 while ($data = db_fetch_object($result)) {
227 node_delete($data->nid);
228 }
229 }
230 menu_rebuild();
231 }
232
233 function contento_type_form($type = NULL) {
234 if (isset($type)) {
235 // Configuring an existing content type
236 $form['content'] = array(
237 '#type' => 'fieldset', '#tree' => TRUE,
238 '#title' => t('Configurable content type settings'),
239 '#collapsible' => TRUE, '#collapsed' => TRUE,
240 '#weight' => -10
241 );
242 $form['content']['name'] = array(
243 '#type' => 'value',
244 '#value' => $type->name,
245 );
246 //$form['content']['delete'] = array('#type' => 'submit', '#value' => t('Remove this content type'), '#weight' => 10 );
247 $form['content']['delete'] =
248 array(
249 '#value' => l(t('Delete this content type'), 'admin/settings/content-types/delete/'.$type->name),
250 '#weight' => 10 );
251
252 } else {
253 // Creating a new content type
254 $type = new stdClass();
255 $type->title_label = t('Title');
256 $type->body_label = t('Body');
257 $form['content'] = array('#type' => 'fieldset', '#tree' => TRUE);
258 $form['content']['name'] = array(
259 '#title' => t('System Name'),
260 '#type' => 'textfield',
261 '#description' => t('The low level name of this content type. Only alphabetic -English- characters, hyphen and underscore are allowed. No spaces.'),
262 '#required' => TRUE,
263 '#weight' => -10
264 );
265 $form['content']['base'] = array(
266 '#type' => 'value',
267 '#value' => 'contento',
268 );
269 $form['buttons']['submit'] = array('#type' => 'submit', '#value' => t('Add content type') );
270 }
271 $form['content']['label'] = array(
272 '#title' => t('Label'),
273 '#type' => 'textfield',
274 '#default_value' => $type->label,
275 '#description' => t('The human-readable name of this content type.'),
276 '#required' => TRUE,
277 );
278
279 $form['content']['title_label'] = array(
280 '#title' => t('Title field label'),
281 '#type' => 'textfield',
282 '#default_value' => $type->title_label,
283 '#description' => t('The label for the title field.'),
284 );
285
286 $form['content']['body_label'] = array(
287 '#title' => t('Body field label'),
288 '#type' => 'textfield',
289 '#default_value' => $type->body_label,
290 '#description' => t('The label for the body field.'),
291 );
292
293 $form['content']['description'] = array(
294 '#title' => t('Description'),
295 '#type' => 'textarea',
296 '#default_value' => $type->description,
297 '#rows' => 10,
298 '#description' => t('A brief description of the content type.'),
299 );
300 $form['content']['help'] = array(
301 '#title' => t('Help text'),
302 '#type' => 'textarea',
303 '#default_value' => $type->help,
304 '#rows' => 10,
305 '#description' => t('Instructions to present to the user when adding new content of this type.'),
306 );
307
308
309 return $form;
310 }
311
312 ?>

  ViewVC Help
Powered by ViewVC 1.1.2