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

Diff of /contributions/modules/tablemanager/tablemanager.module

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

revision 1.88.2.8, Tue Apr 15 21:01:02 2008 UTC revision 1.88.2.9, Sat Aug 22 19:13:08 2009 UTC
# Line 1  Line 1 
1  <?php  <?php
2  // $Id$  // $Id: tablemanager.module,v 1.88.2.8 2008/04/15 21:01:02 pobster Exp $
3    
4  /**  /**
5   * @file   * @file
# Line 87  function tablemanager_filter_tips($delta Line 87  function tablemanager_filter_tips($delta
87  } // tablemanager_filter_tips  } // tablemanager_filter_tips
88    
89  /**  /**
90     * Implementation of hook_form().
91     */
92    function tablemanager_form(&$node, &$param) {
93      // Stupid hack as Drupal doesn't really allow much overriding of the "node/add" form
94      // Panels_node does this too but it won't be present (hopefully) on future versions
95      switch ($node->type) {
96        case 'table':
97          drupal_goto('node/add/table/new');
98          break;
99        case 'tablemanager':
100          drupal_goto('node/add/tablemanager/types');
101          break;
102      }
103    } // tablemanager_form
104    
105    /**
106   * Implementation of hook_help().   * Implementation of hook_help().
107   */   */
108  function tablemanager_help($path, $arg) {  function tablemanager_help($path, $arg) {
109    switch ($path) {    switch ($path) {
110      case 'node/add/tablemanager': // Shown when you click through 'table entry' on the create content screen      case 'node/add/tablemanager/types': // Shown when you click through 'table entry' on the create content screen
111        return t('Choose from the following available tables:');        return t('Choose from the following available tables:');
112      case 'node/add/tablemanager/%': // Shown when you add a new row      case 'node/add/tablemanager/%': // Shown when you add a new row
113        return t("<p>!desc</p>Add a new entry to table %table. Fill in your data and click submit.",        return t("<p>!desc</p>Add a new entry to table %table. Fill in your data and click submit.",
# Line 107  function tablemanager_help($path, $arg) Line 123  function tablemanager_help($path, $arg)
123   * Implementation of hook_menu().   * Implementation of hook_menu().
124   */   */
125  function tablemanager_menu() {  function tablemanager_menu() {
   $items = array();  
126    // main configuration page of the basic tablemanager module    // main configuration page of the basic tablemanager module
127    $items['admin/settings/tablemanager'] = array(    $items['admin/settings/tablemanager'] = array(
128      'title' => 'Tablemanager Settings',      'title' => 'Tablemanager Settings',
# Line 161  function tablemanager_menu() { Line 176  function tablemanager_menu() {
176      'type' => MENU_CALLBACK,      'type' => MENU_CALLBACK,
177    );    );
178    // Row delete + edit links    // Row delete + edit links
   $items['tablemanager/edit/%tablemanager_row'] = array(  
     'file' => 'includes/rows.inc',  
     'page callback' => 'drupal_get_form',  
     'page arguments' => array('tablemanager_manage_row', 'edit', 2),  
     'access callback' => '_tablemanager_manage_row_access',  
     'access arguments' => array(2),  
     'type' => MENU_CALLBACK,  
   );  
179    $items['tablemanager/%tablemanager_function'] = array(    $items['tablemanager/%tablemanager_function'] = array(
180      'file' => 'includes/rows.inc',      'file' => 'includes/rows.inc',
181      'page callback' => 'drupal_get_form',      'page callback' => 'tablemanager_row_function',
182      'page arguments' => array('tablemanager_row_delete', 1),      'page arguments' => array(1),
183      'load arguments' => array('%map', '%index'),      'load arguments' => array('%map', '%index'),
184      'access callback' => '_tablemanager_manage_row_access',      'access callback' => '_tablemanager_manage_row_access',
185      'access arguments' => array(1),      'access arguments' => array(1),
186      'type' => MENU_CALLBACK,      'type' => MENU_CALLBACK,
187    );    );
188    return $items;    // Node pickups
189  } // tablemanager_menu    $items['node/add/table/new'] = array(
190        'file' => 'includes/tables.inc',
 /**  
  * Implementation of hook_menu_alter().  
  */  
 function tablemanager_menu_alter(&$callbacks) {  
   // Overwrite the default callbacks for node/add page  
   $callbacks['node/add/table'] = array(  
191      'title' => 'Table',      'title' => 'Table',
192      'description' => 'Create a new table.',      'description' => 'Create a new table.',
     'title callback' => 'check_plain',  
193      'page callback' => 'drupal_get_form',      'page callback' => 'drupal_get_form',
194      'page arguments' => array('tablemanager_table_add'),      'page arguments' => array('tablemanager_table_add'),
195      'access callback' => '_tablemanager_table_add_access',      'access callback' => '_tablemanager_table_add_access',
196      'module' => 'tablemanager',      'type' => MENU_CALLBACK,
197      'file' => 'includes/tables.inc',    );
198      $items['node/add/tablemanager/types'] = array(
199        'title' => 'Table entry',
200        'page callback' => 'tablemanager_nodeadd',
201        'access callback' => '_tablemanager_table_add_content_access',
202        'type' => MENU_CALLBACK,
203      );
204      $items['node/add/tablemanager/%tablemanager_table'] = array(
205        'file' => 'includes/rows.inc',
206        'page callback' => 'drupal_get_form',
207        'page arguments' => array('tablemanager_manage_row', 'add', 3),
208        'access callback' => '_tablemanager_table_add_content_access',
209        'access arguments' => array(3),
210        'type' => MENU_CALLBACK,
211    );    );
212    $callbacks['node/add/tablemanager']['page callback'] = 'drupal_get_form';  
213    $callbacks['node/add/tablemanager']['page arguments'] = array('tablemanager_nodeadd_form');    return $items;
214    $callbacks['node/add/tablemanager']['access callback'] = '_tablemanager_table_add_access';  } // tablemanager_menu
215    $callbacks['node/add/tablemanager']['module'] = 'tablemanager';  
216    unset($callbacks['node/add/tablemanager']['file']);  /**
217    $callbacks['node/add/tablemanager/%tablemanager_table']['page callback'] = 'drupal_get_form';   * Menu Loader functions:
218    $callbacks['node/add/tablemanager/%tablemanager_table']['page arguments'] = array('tablemanager_manage_row', 'add', 3);   */
219    $callbacks['node/add/tablemanager/%tablemanager_table']['access callback'] = '_tablemanager_table_add_access';  
220    $callbacks['node/add/tablemanager/%tablemanager_table']['module'] = 'tablemanager';  /**
221    $callbacks['node/add/tablemanager/%tablemanager_table']['file'] = 'includes/rows.inc';   * Ensures the table-xxx parameter passed through node/add/tablemanager + admin/content/tablemanager/table_delete/xxx is valid.
222  } // tablemanager_menu_alter   */
223    function tablemanager_table_load($string) {
224      if (!(preg_match('/^table-(\d+)$/i', $string, $match))) {
225        return FALSE;
226      }
227      if (tablemanager_table_exists($match[1])) {
228        return $match[1];
229      }
230      return FALSE;
231    } // tablemanager_table_load
232    
233    /**
234     * Row functions.
235     */
236    function tablemanager_function_load($op, $rows, $index) {
237      // 'rows' can never be empty
238      if (empty($rows)) {
239        return FALSE;
240      }
241      // remove 'tablemanager/(delete|edit)'
242      $rows = array_slice($rows, $index + 1);
243      $tid = NULL;
244      switch ($op) {
245        case 'delete':
246        case 'edit':
247          foreach ($rows as $row) {
248            // check all rows are numeric
249            if (!is_numeric($row)) {
250              return FALSE;
251            }
252            // check all rows exist and have same parent table
253            $fetch = db_result(db_query('SELECT tid FROM {tablemanager_data} WHERE id = %d', $row));
254            if (!$fetch) {
255              return FALSE;
256            }
257            // this sets the parent table in $tid on the first pass-through
258            if (!$tid) {
259              $tid = $fetch;
260              continue;
261            }
262            // this is the test to ensure all row ids come from the same parent table
263            if ($tid != $fetch) {
264              return FALSE;
265            }
266          }
267          // if tests pass, return something useful
268          return array('op' => $op, 'tid' => $tid, 'tableuid' => db_result(db_query("SELECT uid FROM {tablemanager} WHERE tid = %d", $tid)), 'rows' => $rows);
269      }
270      return FALSE;
271    } // tablemanager_function_load
272    
273  /**  /**
274   * Implementation of hook_node_info().   * Implementation of hook_node_info().
275   */   */
276  function tablemanager_node_info() {  function tablemanager_node_info() {
277    $tables = tablemanager_nodeadd();    if (($tables = tablemanager_nodeadd())) {
278    if ($tables) $description .= t('<dd>Choose from the following available tables: !tables', array('!tables' => tablemanager_nodeadd()));      $description = t('<dd>Choose from the following available tables: !tables', array('!tables' => $tables));
279    return array('table' => array('name' => t('table'), 'module' => 'tablemanager', 'description' => t('Create a new table.')),    }
280      'tablemanager' => array('name' => t('table entry'), 'module' => 'tablemanager', 'description' => $description)    return array(
281        'table' => array(
282          'name' => t('table'),
283          'module' => 'tablemanager',
284          'description' => t('Create a new table.'),
285        ),
286        'tablemanager' => array(
287          'name' => t('table entry'),
288          'module' => 'tablemanager',
289          'description' => $description,
290        ),
291    );    );
292  } // tablemanager_node_info  } // tablemanager_node_info
293    
# Line 225  function tablemanager_node_info() { Line 297  function tablemanager_node_info() {
297  function tablemanager_perm() {  function tablemanager_perm() {
298    $perms = array('administer tables', 'list tables', 'administer/ create own tables');    $perms = array('administer tables', 'list tables', 'administer/ create own tables');
299    foreach (tablemanager_tables() as $table) {    foreach (tablemanager_tables() as $table) {
300      $perms[] = t('create @table content', array('@table' => $table['name']));      $perms[] = sprintf('create %s content', check_plain($table['name']));
301      $perms[] = t('edit any @table content', array('@table' => $table['name']));      $perms[] = sprintf('edit any %s content', check_plain($table['name']));
302      $perms[] = t('edit own @table content', array('@table' => $table['name']));      $perms[] = sprintf('edit own %s content', check_plain($table['name']));
303    }    }
304    return $perms;    return $perms;
305  } // tablemanager_perm  } // tablemanager_perm
# Line 252  function tablemanager_theme() { Line 324  function tablemanager_theme() {
324   * Row add/ delete/ edit form access.   * Row add/ delete/ edit form access.
325   */   */
326  function _tablemanager_manage_row_access($info) {  function _tablemanager_manage_row_access($info) {
 // broken, but broken with a reason...  I need to alter this to cope with multiple selections with both delete AND edit  
327    global $user;    global $user;
328    unset($flag);    $name = tablemanager_fetch_name($info['tid']);
329    $flag = $user->uid == $info->uid && user_access("edit own $info->name content") ? TRUE : $flag;    // Test for table/ admin access
330    $flag = user_access('administer tables') || user_access("edit any $info->name content") ? TRUE : $flag;    if (user_access('administer tables') || ($user->uid == $info['tableuid'] && user_access('administer/ create own tables')) || user_access("edit any $name content")) {
331    $flag = $user->uid == $info->tableuid && user_access('administer/ create own tables') ? TRUE : $flag;      return TRUE;
332    return $flag ? TRUE : FALSE;    }
333    
334      // Start with testing access for each specified row
335      foreach ($info['rows'] as $row) {
336        if (db_result(db_query("SELECT tid FROM {tablemanager_data} WHERE id = %d && uid = %d", $row, $user->uid)) && user_access("edit own $name content")) {
337          $flag = TRUE;
338        }
339        // else is provided here in case of multiple rows being given and each needs to be checked for access
340        else {
341          $flag = FALSE;
342        }
343      }
344    
345      return $flag;
346  } // _tablemanager_manage_row_access  } // _tablemanager_manage_row_access
347    
348  /**  /**
349   * Table add form access.   * Table add form access.
350   */   */
351  function _tablemanager_table_add_access() {  function _tablemanager_table_add_access() {
352    return user_access('administer tables') || user_access('administer/ create own tables') ? TRUE : FALSE;    if (user_access('administer tables') || user_access('administer/ create own tables')) {
353  } // _tablemanager_table_add_access      return TRUE;
   
 /**  
  * Table add form access.  
  */  
 function _tablemanager_table_manage_access() {  
   return user_access("create ". $table['name'] ." content") || user_access('administer tables') || user_access('administer/ create own tables') && $user->uid == $table['uid'] ? TRUE : FALSE;  
 } // _tablemanager_table_manage_access  
   
 /**  
  * Menu load arguments:  
  */  
   
 /**  
  * Row functions.  
  */  
 function tablemanager_function_load($op, $rows, $index) {  
   // remove 'tablemanager/(delete|edit)'  
   $rows = array_slice($rows, $index + 1);  
   unset($tid);  
   switch ($op) {  
     case 'delete':  
     case 'edit':  
       // 'rows' can't be empty  
       if (empty($rows)) return FALSE;  
       foreach ($rows as $row) {  
         // check all rows are numeric  
         if (!is_numeric($row)) {  
           return FALSE;  
         }  
         // check all rows exist and have same parent table  
         $fetch = db_result(db_query('SELECT tid FROM {tablemanager_data} WHERE id = %d', $row));  
         if (!$fetch) {  
           return FALSE;  
         }  
         if (!$tid) {  
           $tid = $fetch;  
           continue;  
         }  
         if ($tid != $fetch) {  
           return FALSE;  
         }  
       }  
       // if tests pass, return something useful  
       return array('tid' => $tid, 'rows' => $rows);  
       break;  
354    }    }
355    
356    return FALSE;    return FALSE;
357  } // tablemanager_function_load  } // _tablemanager_table_add_access
358    
359  /**  /**
360   * Ensures the table-xxx parameter passed through node/add/tablemanager + admin/content/tablemanager/table_delete/xxx is valid.   * Table add content access.
361   */   */
362  function tablemanager_table_load($string) {  function _tablemanager_table_add_content_access($tid = NULL) {
363    $check = preg_match('/^table-(\d+)/i', $string, $match);    if (user_access('administer tables') || user_access('administer/ create own tables')) {
364    if (!$check) {      return TRUE;
     return FALSE;  
365    }    }
366    if (tablemanager_table_exists($match[1])) {    if ($tid) {
367      return $match[1];      if (user_access(sprintf('create %s content', tablemanager_fetch_name($tid)))) {
368          return TRUE;
369        }
370    }    }
371      else {
372        // we're here to test to see if the user has *any* access for any table entries
373        foreach (tablemanager_tables() as $table) {
374          if (user_access(sprintf('create %s content', check_plain($table['name'])))) {
375            return TRUE;
376          }
377        }
378      }
379    
380    return FALSE;    return FALSE;
381  } // tablemanager_table_load  } // _tablemanager_table_add_content_access
382    
383  /**  /**
384   * Tablemanager functions:   * Tablemanager functions:
# Line 354  function tablemanager_css() { Line 404  function tablemanager_css() {
404  } // tablemanager_css  } // tablemanager_css
405    
406  /**  /**
407   * Returns description field from specified table.   * Returns (unescaped) description field from specified table.
408   */   */
409  function tablemanager_desc($tid) {  function tablemanager_desc($tid) {
410    $fetch = db_fetch_object(db_query('SELECT description FROM {tablemanager} WHERE tid = %d', $tid));    $fetch = db_fetch_object(db_query('SELECT description FROM {tablemanager} WHERE tid = %d', $tid));
# Line 390  function tablemanager_display_new($tid, Line 440  function tablemanager_display_new($tid,
440    
441    // to-do...    // to-do...
442    
443  } //tablemanager_display  } // tablemanager_display_new
444    
445  /**  /**
446   * The main user hook to display a table.   * The main user hook to display a table.
447   */   */
448  function tablemanager_display($tid, $list_length = NULL, $links = FALSE, $date = array(), $attrib = array()) {  function tablemanager_display($tid, $list_length = NULL, $links = FALSE, $date = array(), $attrib = array()) {
   global $user, $pager_page_array, $pager_total_items, $pager_total;  
449    if (!$tid || !is_numeric($tid)) {    if (!$tid || !is_numeric($tid)) {
450      return 'Invalid Table ID';      return t('Invalid Table ID');
451    }    }
452      global $user, $pager_page_array, $pager_total_items, $pager_total;
453    if ($date) {    if ($date) {
454      $date['end'] = $date['end'] ? $date['end'] : $date['start'];      $date['end'] = $date['end'] ? $date['end'] : $date['start'];
455    }    }
# Line 417  function tablemanager_display($tid, $lis Line 467  function tablemanager_display($tid, $lis
467        $list_length = 1;        $list_length = 1;
468      }      }
469    }    }
470    $fetch = db_query('SELECT tmd.id, tm.uid AS tableuid, tmd.uid, tm.name, tm.description, tm.header, tmd.data FROM {tablemanager} tm LEFT JOIN {tablemanager_data} tmd ON tm.tid = tmd.tid WHERE tm.tid = %d ORDER BY tmd.id', $tid);    $fetch = db_query('SELECT tmd.id, tm.uid AS tableuid, tmd.uid, tm.name, tm.description, tm.header, tmd.data
471        FROM {tablemanager} tm
472          LEFT JOIN {tablemanager_data} tmd ON tm.tid = tmd.tid
473        WHERE tm.tid = %d ORDER BY tmd.id',
474        $tid
475      );
476    $path = base_path() . drupal_get_path('module', 'tablemanager');    $path = base_path() . drupal_get_path('module', 'tablemanager');
477    unset($flag);    unset($flag);
478    $rows = array();    $rows = array();
# Line 436  function tablemanager_display($tid, $lis Line 491  function tablemanager_display($tid, $lis
491        if (variable_get('tablemanager_css', 0)) {        if (variable_get('tablemanager_css', 0)) {
492          tablemanager_css();          tablemanager_css();
493        }        }
494        $description = variable_get('tablemanager_descriptions', 0) ? $result->description : NULL;        $description = variable_get('tablemanager_descriptions', 0) ? check_plain($result->description) : NULL;
495        switch (variable_get('tablemanager_names', NULL)) {        switch (variable_get('tablemanager_names', NULL)) {
496          case "table number":          case "table number":
497            $name = "Table ". $tid;            $name = "Table ". $tid;
498            break;            break;
499          case "names":          case "names":
500            $name = $result->name == $tid ? "Table ". $tid : $result->name;            $name = $result->name == $tid ? "Table ". $tid : check_plain($result->name);
501            break;            break;
502          default:          default:
503            $name = NULL;            $name = NULL;
# Line 595  function tablemanager_display_rows($para Line 650  function tablemanager_display_rows($para
650  } // tablemanager_display_rows  } // tablemanager_display_rows
651    
652  /**  /**
653   * Returns the user entered name for a table.   * Returns the (escaped) user entered name for a table.
654   */   */
655  function tablemanager_fetch_name($tid) {  function tablemanager_fetch_name($tid) {
656    $result = db_fetch_object(db_query('SELECT name FROM {tablemanager} WHERE tid = %d', $tid));    $result = db_fetch_object(db_query('SELECT name FROM {tablemanager} WHERE tid = %d', $tid));
657    return $result->name;    return check_plain($result->name);
658  } // tablemanager_fetch_name  } // tablemanager_fetch_name
659    
660  /**  /**
# Line 744  function _tablemanager_process_text($tex Line 799  function _tablemanager_process_text($tex
799  } // _tablemanager_process_text  } // _tablemanager_process_text
800    
801  /**  /**
  * Ensures the row parameter passed through tablemanager/xxx is valid.  
  */  
 function tablemanager_row_load($id) {  
   if (!is_numeric($id)) {  
     return FALSE;  
   }  
   $fetch = db_fetch_object(db_query('SELECT tm.tid, tm.uid AS tableuid, tm.name, tmd.uid, tm.header, tmd.data, tmd.format  
                                      FROM {tablemanager} tm  
                                        INNER JOIN {tablemanager_data} tmd ON tm.tid = tmd.tid  
                                      WHERE tmd.id = %d', $id));  
   if ($fetch) {  
     return $fetch;  
   }  
   else {  
     return FALSE;  
   }  
 } // tablemanager_row_load  
   
 /**  
802   * Sets to a valid table if current table is deleted.   * Sets to a valid table if current table is deleted.
803   */   */
804  function tablemanager_set_tid($dir = 'DESC') {  function tablemanager_set_tid($dir = 'DESC') {
# Line 804  function tablemanager_tables() { Line 840  function tablemanager_tables() {
840    $tables = array();    $tables = array();
841    $sql = db_query('SELECT * FROM {tablemanager}');    $sql = db_query('SELECT * FROM {tablemanager}');
842    while ($result = db_fetch_object($sql)) {    while ($result = db_fetch_object($sql)) {
843      $tables[$result->tid] = array('tid' => $result->tid, 'uid' => $result->uid, 'name' => $result->name, 'description' => $result->description, 'header' => $result->header);      $tables[$result->tid] = array('tid' => $result->tid, 'uid' => $result->uid, 'name' => check_plain($result->name), 'description' => check_plain($result->description), 'header' => $result->header);
844    }    }
845    return $tables;    return $tables;
846  } // tablemanager_tables  } // tablemanager_tables

Legend:
Removed from v.1.88.2.8  
changed lines
  Added in v.1.88.2.9

  ViewVC Help
Powered by ViewVC 1.1.2