All pastes #291833 Raw Edit

Something

public php v1 · immutable
#291833 ·published 2006-12-25 20:27 UTC
rendered paste body
<?php/*** postjob.module* v 1.0* The postjob module allows users to post jobs.* by craze3* 12/24/06**//*** A small example module of using the form api* to display and process a form* that is not used to extend the basic node content type** In this sample the form collects two numbers* and multiplies them together*//*** Implementation of hook_help().** Throughout Drupal, hook_help() is used to display help text at the top of* pages. Some other parts of Drupal pages get explanatory text from these hooks* as well. We use it here to provide a description of the module on the* module administration page.*/function postjob_help($section) {switch ($section) {   case 'admin/modules#description':     // This description is shown in the listing at admin/modules.     return t('The postjob module allows users to post jobs.');}}/*** Implementation of hook_perm().**/function postjob_perm() {return array('posting jobs');}/*** Implementation of hook_menu().**/function postjob_menu($may_cache) {$items = array();   $access = user_access('posting jobs');  if ($may_cache) {     // This determines the path used to show the form and also makes a menu entry     // This first path is for the entry form   $items[] = array('path' => 'postjob/form', 'title' => t('Post a project'),     'callback' => postjob_form, 'access' => $access);       /*// This path is for the routine that actually does the multplication   // It is only setup as a callback (no menu entry)   $items[] = array('path' => 'postjob/post', 'title' => t('Posting Results'),     'type' => MENU_CALLBACK, 'callback' => postjob_post, 'access' => $access);*/   }     return $items;}function postjob_form() {   $form = array();     // Build up the form   // See api.drupal.org/api/4.7/file/developer/topics/forms_api.html (quickstart guide)   // and api.drupal.org/api/4.7/file/developer/topics/forms_api_reference.html (forms api)   // for more information on creating forms     $form['name'] = array(     '#type' => 'textfield',     '#title' => t('Project Name'),     '#size' => 30,     '#maxlength' => 25,     '#description' => t('The name of your project. Domains/URLs not allowed!')   );     $form['length'] = array(     '#type' => 'textfield',     '#title' => t('Project length (days)'),     '#size' => 3,     '#maxlength' => 2,     '#description' => t('The amount of days your project will be listed and open to bidding.')   );     // Make sure we have a submit button   $form['submit'] = array('#type' => 'submit', '#value' => t('Post Project'));     // drupal_get_form produces the form   // which return to the drupal system   // which produces the page     // The first parameter to drupal_get_form   // is the form id. It also determines the   // default validation and submit function names   //   // In this case   //   Validation: myform_sample_validate()   // Submit: my_form_submit()     return drupal_get_form('postjob_form', $form);}function postjob_form_validate($form_id, $form_values) {   if ( !isset($form_values['name']) ) {       form_set_error('name', t('You must provide a project name.'));   }   if ( !isset($form_values['length']) ) {       form_set_error('length', t('You must provide a project length.'));   }   else if ( !is_numeric($form_values['length']) ) {       form_set_error('length', t('The project length must be a number.'));   }     else if ($form_values['length'] >= 36){   		form_set_error('length', t('The project length must be 35 days or less.'));	}}function postjob_form_submit($form_id, $form_values) {   // Submit routines do not directly produce any output   // They do something with the form values   // which is typically to store them in the database   // then return a path to determine what page is shown next     // In this case the path constructe includes the two values     //return 'postjob/post/' . $form_values['value1'] . '/' . $form_values['value2'];   //Get all the variables   global $user;   $username = $user->name;   $name = $form_values['name'];   $length = $form_values['length'];   //Do MySQL   //   //Generate Output   $output = "Posting successful!"   return $output;}  /*function postjob_post($value1, $value2) {   $output = '<p>';   $output .= $value1. ' * ' . $value2 . ' = ' . ($value1 * $value2);   $output .= '</p>';     return $output;}*/?>