|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * @author Samuel Leathers |
| 4 | + * @copyright Copyright (c) 2011, The Pennsylvania State University |
| 5 | + * version 0.1 |
| 6 | + * mvc.module |
| 7 | + * |
| 8 | + * @package mvc |
| 9 | + */ |
| 10 | + |
| 11 | + |
| 12 | +// $Id$ |
| 13 | + |
| 14 | +/** |
| 15 | + * This module allows creating mvc applications from within drupal |
| 16 | + * |
| 17 | + */ |
| 18 | + /** |
| 19 | + * Instantiate class autoloader |
| 20 | + */ |
| 21 | +module_load_include('inc', 'mvc', 'autoload'); |
| 22 | + |
| 23 | +/** |
| 24 | + * Implementation of hook_init(). |
| 25 | + */ |
| 26 | +function mvc_init() { |
| 27 | + global $mvc_modules; |
| 28 | + $mvc_modules = module_invoke_all('mvc_app'); |
| 29 | + foreach($mvc_modules as $mod) { |
| 30 | + AutoLoader::addPath($mod['path']); |
| 31 | + } |
| 32 | + AutoLoader::addPath(drupal_get_path('module','mvc')); |
| 33 | +} |
| 34 | + |
| 35 | + |
| 36 | +/** |
| 37 | + * Sample Implementation of hook_mvc_app |
| 38 | + * |
| 39 | + * {{{ |
| 40 | + * function mvc_mvc_app() { |
| 41 | + * $mvc = new ArrayObject(); |
| 42 | + * $mvc['base'] = 'mvc'; |
| 43 | + * $mvc['path'] = drupal_get_path('module','mvc'); |
| 44 | + * return $mvc; |
| 45 | + * } |
| 46 | + * }}} |
| 47 | + * |
| 48 | + */ |
| 49 | + |
| 50 | + |
| 51 | + |
| 52 | +/** |
| 53 | + * Implementation of hook_menu(). |
| 54 | + */ |
| 55 | +function mvc_menu() { |
| 56 | + global $mvc_modules; |
| 57 | + $items = array(); |
| 58 | + // Graduate Application Page |
| 59 | + foreach($mvc_modules as $mod) { |
| 60 | + $items[ $mod['base'].'/%/%'] = array( |
| 61 | + 'page callback' => 'mvc_controller', |
| 62 | + 'page arguments' => array($mod['path']), |
| 63 | + 'access callback' => TRUE, |
| 64 | + 'type' => MENU_CALLBACK |
| 65 | + ); |
| 66 | + } |
| 67 | + return $items; |
| 68 | +} |
| 69 | + |
| 70 | +/** |
| 71 | + * dispatcher function for controller |
| 72 | + * @return object View rendered by controller |
| 73 | + */ |
| 74 | +function mvc_controller($modpath) { |
| 75 | + global $controllerName, $method, $module_path; |
| 76 | + $module_path = $modpath; |
| 77 | + $controllerName = arg(1); |
| 78 | + $fullControllerName = $controllerName.'Controller'; |
| 79 | + $method = arg(2); |
| 80 | + $params = array_merge(array_diff(arg(),array(arg(0),arg(1),arg(2)))); |
| 81 | + $controller = new $fullControllerName; |
| 82 | + if($controller->access()) { |
| 83 | + if(method_exists($controller, $method)) { |
| 84 | + return $controller->$method($params); |
| 85 | + } |
| 86 | + else { |
| 87 | + drupal_not_found(); |
| 88 | + } |
| 89 | + } |
| 90 | + else { |
| 91 | + drupal_access_denied(); |
| 92 | + } |
| 93 | +} |
0 commit comments