diff -u b/dbtng_example/dbtng_example.module b/dbtng_example/dbtng_example.module --- b/dbtng_example/dbtng_example.module +++ b/dbtng_example/dbtng_example.module @@ -346,8 +346,7 @@ break; case 'examples/dbtng/ordered_list': - $output = t('Generate an ordered list of all the records in the database. '); - $output .= t('The records will be displayed in ascending order for column "Name".'); + $output = t('Generate a list of database records in Ordering format. The records will be displayed in ascending order for column "Name".'); } return $output; } @@ -395,7 +394,7 @@ $items['examples/dbtng/ordered_list'] = array( 'title' => 'Ordered list', 'page callback' => 'dbtng_example_ordered_list', - 'access callback' => TRUE, + 'access arguments' => array('access dbtng example ordered list'), 'type' => MENU_LOCAL_TASK, ); @@ -403,6 +402,22 @@ } /** + * Implements hook_permission(). + * + * This hook can supply permissions that the module defines, so that they can + * be selected on the user permissions page and used to grant or restrict + * access to actions the module performs. + */ +function dbtng_example_permission() { + return array( + 'access dbtng example ordered list' => array( + 'title' => t('Permission to view an Ordered List'), + 'description' => t('Give permission to access an Ordered List.'), + ), + ); +} + +/** * Render a list of entries in the database. */ function dbtng_example_list() { @@ -588,39 +603,46 @@ /** * Render an ordered list of entries in the database. * - * This function queries the database and retrives all the records in an ascending order. + * This function queries the database and retrives all the records in an + * ascending order. * - * SELECT ex.pid AS pid, ex.uid AS uid, ex.name AS name, ex.surname AS surname, ex.age AS age + * SELECT ex.pid AS pid, ex.uid AS uid, ex.name AS name, ex.surname AS surname, + * ex.age AS age * FROM {dbtng_example} ex * ORDER BY ex.name ASC * */ function dbtng_example_ordered_list() { - $output = ''; - - $select = db_select('dbtng_example', 'ex'); - $select->fields('ex', array('pid', 'uid', 'name', 'surname', 'age')); + $dbtng_example_execute_ordered_select_query = db_select('dbtng_example', 'ex')->fields('ex'); // 'ex.name' is the field on which to order. - // Legal values for sorting are "ASC" and "DESC". Any other value will be converted to "ASC". - $select->orderBy('ex.name', 'ASC'); + // Legal values for sorting are "ASC" and "DESC". Any other value will be + // converted to "ASC". + $dbtng_example_execute_ordered_select_query->orderBy('ex.name', 'ASC'); // generate the result in object format. - $result = $select->execute()->fetchAll(); + $dbtng_example_result = $dbtng_example_execute_ordered_select_query->execute()->fetchAll(); - if ($result) { + $output = ''; + if ($dbtng_example_result) { $rows = array(); - foreach ($result as $row) { + foreach ($dbtng_example_result as $row) { // Sanitize the data before handing it off to the theme layer. $rows[] = array_map('check_plain', (array) $row); } - // Make a table for them. - $header = array(t('Id'), t('uid'), t('Name'), t('Surname'), t('Age')); - $output .= theme('table', array('header' => $header, 'rows' => $rows)); + $output .= dbtng_example_render_resultset_as_table($rows); } else { drupal_set_message(t('No records found.')); } return $output; } + +function dbtng_example_render_resultset_as_table($rows = array()) { + // Make a table for them. + $header = array(t('Id'), t('uid'), t('Name'), t('Surname'), t('Age')); + $output = theme('table', array('header' => $header, 'rows' => $rows)); + + return $output; +} /** * @} End of "defgroup dbtng_example". */