[ Index ] |
|
Code source de eZ Publish 3.9.0 |
1 <?php 2 // 3 // Definition of eZModule class 4 // 5 // Created on: <17-Apr-2002 11:11:39 amos> 6 // 7 // SOFTWARE NAME: eZ publish 8 // SOFTWARE RELEASE: 3.9.0 9 // BUILD VERSION: 17785 10 // COPYRIGHT NOTICE: Copyright (C) 1999-2006 eZ systems AS 11 // SOFTWARE LICENSE: GNU General Public License v2.0 12 // NOTICE: > 13 // This program is free software; you can redistribute it and/or 14 // modify it under the terms of version 2.0 of the GNU General 15 // Public License as published by the Free Software Foundation. 16 // 17 // This program is distributed in the hope that it will be useful, 18 // but WITHOUT ANY WARRANTY; without even the implied warranty of 19 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 20 // GNU General Public License for more details. 21 // 22 // You should have received a copy of version 2.0 of the GNU General 23 // Public License along with this program; if not, write to the Free 24 // Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 25 // MA 02110-1301, USA. 26 // 27 // 28 29 /*! 30 \class eZModule ezmodule.php 31 \ingroup eZUtils 32 \brief Allows execution of modules and functions 33 34 */ 35 36 include_once ( "lib/ezutils/classes/ezdebug.php" ); 37 38 define( "EZ_MODULE_STATUS_IDLE", 0 ); 39 define( "EZ_MODULE_STATUS_OK", 1 ); 40 define( "EZ_MODULE_STATUS_FAILED", 2 ); 41 define( "EZ_MODULE_STATUS_REDIRECT", 3 ); 42 define( "EZ_MODULE_STATUS_RERUN", 4 ); 43 44 define( "EZ_MODULE_HOOK_STATUS_OK", 0 ); 45 define( "EZ_MODULE_HOOK_STATUS_CANCEL_RUN", 1 ); 46 define( "EZ_MODULE_HOOK_STATUS_FAILED", 2 ); 47 48 class eZModule 49 { 50 function eZModule( $path, $file, $moduleName ) 51 { 52 $this->initialize( $path, $file, $moduleName ); 53 } 54 55 /*! 56 \private 57 Initializes the module object with the path and file and name. 58 It will look for a file called \a $file and include the contents 59 of that file, it will then assume that some variables were set 60 which defines the module and it's view/functions. 61 */ 62 function initialize( $path, $file, $moduleName ) 63 { 64 unset( $FunctionArray ); 65 unset( $Module ); 66 if ( file_exists( $file ) ) 67 { 68 include( $file ); 69 $this->Functions = $ViewList; 70 if ( isset( $FunctionList ) ) 71 { 72 $this->FunctionList = $FunctionList; 73 } 74 else 75 { 76 $this->FunctionList = array(); 77 } 78 $this->Module =& $Module; 79 $this->Name = $moduleName; 80 $this->Path = $path; 81 $this->Title = ""; 82 $this->UIContext = 'navigation'; 83 $this->UIComponent = $moduleName; 84 85 $uiComponentMatch = 'module'; 86 if ( isset( $this->Module['ui_component_match'] ) ) 87 { 88 $uiComponentMatch = $this->Module['ui_component_match']; 89 } 90 $this->UIComponentMatch = $uiComponentMatch; 91 92 foreach( $this->Functions as $key => $dummy) 93 { 94 $this->Functions[$key]["uri"] = "/$moduleName/$key"; 95 } 96 } 97 else 98 { 99 $this->Functions = array(); 100 $this->Module = array( "name" => "null", 101 "variable_params" => false, 102 "function" => array() ); 103 $this->Name = $moduleName; 104 $this->Path = $path; 105 $this->Title = ""; 106 $this->UIContext = 'navigation'; 107 $this->UIComponent = $moduleName; 108 $this->UIComponentMatch = 'module'; 109 } 110 $this->HookList = array(); 111 $this->ExitStatus = EZ_MODULE_STATUS_IDLE; 112 $this->ErrorCode = 0; 113 $this->ViewActions = array(); 114 $this->OriginalParameters = null; 115 $this->UserParameters = array(); 116 117 // Load in navigation part overrides 118 $ini =& eZINI::instance( 'module.ini' ); 119 $this->NavigationParts = $ini->variable( 'ModuleOverrides', 'NavigationPart' ); 120 } 121 122 /*! 123 \return the URI of the module. 124 \sa functionURI 125 */ 126 function uri() 127 { 128 return "/" . $this->Name; 129 } 130 131 /*! 132 \return the URI to the view \a $function. If the view is empty or the module is a singleView type 133 it will return the result of uri(). If the view does not exist the \c null is returned. 134 \sa uri 135 */ 136 function functionURI( $function ) 137 { 138 if ( $this->singleFunction() or 139 $function == '' ) 140 return $this->uri(); 141 if ( isset( $this->Functions[$function] ) ) 142 return $this->Functions[$function]["uri"]; 143 else 144 return null; 145 } 146 147 /*! 148 \return the title of the current view run, it's normally set by the view 149 and display as the title of view pages. 150 */ 151 function title() 152 { 153 return $this->Title; 154 } 155 156 /*! 157 Sets the current view for the module to \a $title. 158 */ 159 function setTitle( $title ) 160 { 161 $this->Title = $title; 162 } 163 164 /*! 165 Sets the name of the currently running module to \a $name. 166 */ 167 function setCurrentName( $name ) 168 { 169 $this->Name = $name; 170 foreach( $this->Functions as $key => $dummy ) 171 { 172 $this->Functions[$key]["uri"] = "/$name/$key"; 173 } 174 } 175 176 /*! 177 Sets the name of the currently running view to \a $name. 178 */ 179 function setCurrentView( $name ) 180 { 181 $GLOBALS['eZModuleCurrentView'] = $name; 182 } 183 184 /*! 185 \return true if the module acts a single view. 186 */ 187 function singleFunction() 188 { 189 return count( $this->Functions ) == 0; 190 } 191 192 /*! 193 \return A string describing the current UI context, the default is \c 'navigation' 194 195 Change the context with setUIContextName(). 196 */ 197 function uiContextName() 198 { 199 return $this->UIContext; 200 } 201 202 /*! 203 \return A string describing the current UI component. 204 205 The default value is the name of the currently running module, can be changed with setUIComponentName(). 206 */ 207 function uiComponentName() 208 { 209 return $this->UIComponent; 210 } 211 212 /*! 213 Sets the current context string to \a $context. 214 */ 215 function setUIContextName( $context ) 216 { 217 $this->UIContext = $context; 218 } 219 220 /*! 221 Sets the current component string to \a $component. 222 */ 223 function setUIComponentName( $component ) 224 { 225 $this->UIComponent = $component; 226 } 227 228 /*! 229 \return the current status from the module. 230 */ 231 function exitStatus() 232 { 233 return $this->ExitStatus; 234 } 235 236 /*! 237 Sets the current status for the module to \a $stat, the status can trigger 238 a redirect or tell the client that the view failed. 239 */ 240 function setExitStatus( $stat ) 241 { 242 $this->ExitStatus = $stat; 243 } 244 245 /*! 246 \return the error code if the function failed to run or \c 0 if no error code. 247 \sa setErrorCode 248 */ 249 function errorCode() 250 { 251 return $this->ErrorCode; 252 } 253 254 /*! 255 Sets the current error code. 256 \note You need to set the exit status to EZ_MODULE_STATUS_FAILED for the error code to be used. 257 \sa setExitStatus, errorCode 258 */ 259 function setErrorCode( $errorCode ) 260 { 261 $this->ErrorCode = $errorCode; 262 } 263 264 /*! 265 \return the name of the module which will be run on errors. 266 The default name is 'error'. 267 \sa handleError 268 */ 269 function errorModule() 270 { 271 $globalErrorModule =& $GLOBALS['eZModuleGlobalErrorModule']; 272 if ( !isset( $globalErrorModule ) ) 273 $globalErrorModule = array( 'module' => 'error', 274 'view' => 'view' ); 275 return $globalErrorModule; 276 } 277 278 /*! 279 Sets the name of the module which will be run on errors. 280 \sa handleError 281 */ 282 function setErrorModule( $moduleName, $viewName ) 283 { 284 $globalErrorModule =& $GLOBALS['eZModuleGlobalErrorModule']; 285 $globalErrorModule = array( 'module' => $moduleName, 286 'view' => $viewName ); 287 } 288 289 /*! 290 Tries to run the error module with the error code \a $errorCode. 291 Sets the state of the module object to \c failed and sets the error code. 292 */ 293 function &handleError( $errorCode, $errorType = false, $parameters = array(), $userParameters = false ) 294 { 295 if ( !$errorType ) 296 { 297 eZDebug::writeWarning( "No error type specified for error code $errorCode, assuming kernel.\nA specific error type should be supplied, please check your code.", 298 'eZModule::handleError' ); 299 $errorType = 'kernel'; 300 } 301 $errorModule = eZModule::errorModule(); 302 $module =& eZModule::findModule( $errorModule['module'], $this ); 303 304 if ( $module === null ) 305 { 306 $retValue = false; 307 return $retValue; 308 } 309 310 $result =& $module->run( $errorModule['view'], array( $errorType, $errorCode, $parameters, $userParameters ) ); 311 // The error module may want to redirect to another URL, see error.ini 312 if ( $this->exitStatus() != EZ_MODULE_STATUS_REDIRECT and 313 $this->exitStatus() != EZ_MODULE_STATUS_RERUN ) 314 { 315 $this->setExitStatus( EZ_MODULE_STATUS_FAILED ); 316 $this->setErrorCode( $errorCode ); 317 } 318 return $result; 319 } 320 321 /*! 322 Redirects the page to the module \a $moduleName and view \a $viewName with parameters \a $parameters 323 and unorderedParameters \a $unorderedParameters. If you already have the module object use redirectModule 324 instead or if you need to redirect to a view in the current module use redirectToView. 325 \return false if the view could not redirected to. 326 \sa redirectionURI 327 */ 328 function redirect( $moduleName, $viewName, $parameters = array(), 329 $unorderedParameters = null, $userParameters = false, 330 $anchor = false ) 331 { 332 $module =& eZModule::exists( $moduleName ); 333 if ( $module ) 334 { 335 return $this->redirectModule( $module, $viewName, $parameters, 336 $unorderedParameters, $userParameters, $anchor ); 337 } 338 else 339 { 340 eZDebug::writeError( 'Undefined module: ' . $moduleName, 'eZModule::redirect' ); 341 } 342 return false; 343 } 344 345 /*! 346 Same as redirect() only redirects in the current module. 347 */ 348 function redirectToView( $viewName = '', $parameters = array(), 349 $unorderedParameters = null, $userParameters = false, 350 $anchor = false ) 351 { 352 return $this->redirectModule( $this, $viewName, $parameters, 353 $unorderedParameters, $userParameters, $anchor ); 354 } 355 356 /*! 357 Same as redirect() but takes a module object instead of the name. 358 */ 359 function redirectModule( &$module, $viewName, $parameters = array(), 360 $unorderedParameters = null, $userParameters = false, 361 $anchor = false ) 362 { 363 $uri = $this->redirectionURIForModule( $module, $viewName, $parameters, 364 $unorderedParameters, $userParameters, $anchor ); 365 $this->redirectTo( $uri ); 366 return true; 367 } 368 369 /*! 370 \return the URI for the module \a $moduleName and view \a $viewName using parameters \a $parameters 371 and unordered parameters \a $unorderedParameters. 372 \sa redirect 373 */ 374 function redirectionURI( $moduleName, $viewName, $parameters = array(), 375 $unorderedParameters = null, $userParameters = false, 376 $anchor = false ) 377 { 378 $module =& eZModule::exists( $moduleName ); 379 if ( $module ) 380 { 381 return $this->redirectionURIForModule( $module, $viewName, $parameters, 382 $unorderedParameters, $userParameters, $anchor ); 383 } 384 else 385 eZDebug::writeError( 'Undefined module: ' . $moduleName, 'eZModule::redirectionURI' ); 386 return false; 387 } 388 389 /*! 390 \return The URI of the currently run view in the current module with the current parameters. 391 */ 392 function currentRedirectionURI() 393 { 394 $module =& $this; 395 $viewName = eZModule::currentView(); 396 $parameters = $this->OriginalViewParameters; 397 $unorderedParameters = $this->OriginalUnorderedParameters; 398 $userParameters = $this->UserParameters; 399 return $this->redirectionURIForModule( $module, $viewName, $parameters, 400 $unorderedParameters, $userParameters ); 401 } 402 403 /*! 404 Redirects to the current module and view, it will use currentRedirectionURI() to 405 figure out the URL. 406 \note By changing using setCurrentName() and setCurrentView() first it is possible to 407 redirect to another module or view with the same parameters. 408 */ 409 function redirectCurrent() 410 { 411 $this->redirectTo( $this->currentRedirectionURI() ); 412 } 413 414 /*! 415 Sames as redirectionURI but takes a module object instead of the name. 416 */ 417 function redirectionURIForModule( &$module, $viewName, $parameters = array(), 418 $unorderedParameters = null, $userParameters = false, 419 $anchor = false ) 420 { 421 if ( $viewName == '' ) 422 $viewName = eZModule::currentView(); 423 $uri = $module->functionURI( $viewName ); 424 $uri .= '/'; 425 $viewParameters =& $module->parameters( $viewName ); 426 $parameterIndex = 0; 427 $unorderedURI = ''; 428 $hasUnorderedParameter = false; 429 if ( $unorderedParameters !== null ) 430 { 431 $unorderedViewParameters =& $module->unorderedParameters( $viewName ); 432 if ( is_array( $unorderedViewParameters ) ) 433 { 434 foreach ( $unorderedViewParameters as $unorderedViewParameterName => $unorderedViewParameterVariable ) 435 { 436 if ( isset( $unorderedParameters[$unorderedViewParameterVariable] ) ) 437 { 438 $unorderedURI .= $unorderedViewParameterName . '/' . $unorderedParameters[$unorderedViewParameterVariable] . '/'; 439 $hasUnorderedParameter = true; 440 } 441 } 442 } 443 } 444 445 if( !isset( $viewParameters ) ) 446 $viewParameters = array(); // prevent PHP warning below 447 448 foreach ( $viewParameters as $viewParameter ) 449 { 450 if ( !isset( $parameters[$parameterIndex] ) ) 451 { 452 // We don't show a warning anymore since some parameters can be optional 453 // In future versions we will need required and optional parameters 454 // for modules and give warnings for required ones. 455 // eZDebug::writeWarning( "Missing parameter(s) " . implode( ', ', array_slice( $viewParameters, $parameterIndex ) ) . 456 // " in view '$viewName'", 'eZModule::redirect' ); 457 } 458 else 459 $uri .= $parameters[$parameterIndex] . '/'; 460 ++$parameterIndex; 461 } 462 if ( $hasUnorderedParameter ) 463 { 464 $uri .= $unorderedURI; 465 } 466 467 if ( is_array( $userParameters ) ) 468 { 469 foreach ( $userParameters as $name => $value ) 470 { 471 $uri .= '/(' . $name . ')/' . $value; 472 } 473 } 474 475 $uri = preg_replace( "#(^.*)(//+)$#", "\$1", $uri ); 476 if ( $anchor !== false ) 477 $uri .= '#' . urlencode( $anchor ); 478 return $uri; 479 } 480 481 /*! 482 \return the parameter definition for the view \a $viewName. If \a $viewName 483 is empty the current view is used. 484 \sa unorderedParameters, viewData, currentView, currentModule 485 */ 486 function ¶meters( $viewName = '' ) 487 { 488 if ( $viewName == '' ) 489 $viewName = eZModule::currentView(); 490 $viewData =& $this->viewData( $viewName ); 491 if ( isset( $viewData['params'] ) ) 492 return $viewData['params']; 493 $retValue = null; 494 return $retValue; 495 } 496 497 /*! 498 \return the unordered parameter definition for the view \a $viewName. If \a $viewName 499 is empty the current view is used. 500 \sa parameters, viewData, currentView, currentModule 501 */ 502 function &unorderedParameters( $viewName = '' ) 503 { 504 if ( $viewName == '' ) 505 $viewName = eZModule::currentView(); 506 $viewData =& $this->viewData( $viewName ); 507 if ( isset( $viewData['unordered_params'] ) ) 508 return $viewData['unordered_params']; 509 $retValue = null; 510 return $retValue; 511 } 512 513 /*! 514 \return the view data for the view \a $viewName. If \a $viewName 515 is empty the current view is used. 516 \sa parameters, unorderedParameters, currentView, currentModule 517 */ 518 function &viewData( $viewName = '' ) 519 { 520 if ( $viewName == '' ) 521 $viewName = eZModule::currentView(); 522 if ( $this->singleFunction() ) 523 $viewData =& $this->Module["function"]; 524 else 525 $viewData =& $this->Functions[$viewName]; 526 return $viewData; 527 } 528 529 /*! 530 Makes sure that the module is redirected to the URI \a $uri when the function exits. 531 \sa setRedirectURI, setExitStatus 532 */ 533 function redirectTo( $uri ) 534 { 535 $originalURI = $uri; 536 $uri = preg_replace( "#(^.*)(/+)$#", "\$1", $uri ); 537 if ( strlen( $originalURI ) != 0 and 538 strlen( $uri ) == 0 ) 539 $uri = '/'; 540 $this->RedirectURI = $uri; 541 $this->setExitStatus( EZ_MODULE_STATUS_REDIRECT ); 542 } 543 544 /*! 545 \return the URI which will be redirected to when the function exits. 546 */ 547 function redirectURI() 548 { 549 return $this->RedirectURI; 550 } 551 552 /*! 553 Sets the URI which will be redirected to when the function exits. 554 */ 555 function setRedirectURI( $uri ) 556 { 557 $this->RedirectURI = $uri; 558 } 559 560 /*! 561 \return the status which will be set when redirecting. 562 */ 563 function redirectStatus() 564 { 565 return $this->RedirectStatus; 566 } 567 568 /*! 569 Sets the status which will be set when redirecting. 570 \note The status must be a valid HTTP status with number and text. 571 */ 572 function setRedirectStatus( $status ) 573 { 574 $this->RedirectStatus = $status; 575 } 576 577 /*! 578 \return an array with the available attributes. 579 */ 580 function attributes() 581 { 582 return array( "uri", 583 "functions", 584 "name", 585 "path", 586 "info", 587 "aviable_functions", 588 "available_functions" ); 589 } 590 591 /*! 592 \return true if the attribute \a $attr is available. 593 */ 594 function hasAttribute( $attr ) 595 { 596 return in_array( $attr, $this->attributes() ); 597 } 598 599 /*! 600 \return the attribute value for attribute \a $attr if it is available, otherwise \c null. 601 */ 602 function &attribute( $attr ) 603 { 604 switch( $attr ) 605 { 606 case "uri": 607 $retValue = $this->uri(); 608 break; 609 case "functions": 610 return $this->Functions; 611 case "views": 612 return $this->Functions; 613 case "name": 614 return $this->Name; 615 case "path": 616 return $this->Path; 617 case "info": 618 return $this->Module; 619 case 'aviable_functions': 620 case 'available_functions': 621 return $this->FunctionList; 622 default: 623 { 624 eZDebug::writeError( "Attribute '$attr' does not exist", 'eZModule::attribute' ); 625 $retValue = null; 626 } 627 break; 628 } 629 return $retValue; 630 } 631 632 /*! 633 Sets the current action in view \a $view to \a $actionName. 634 \sa currentAction, isCurrentAction 635 */ 636 function setCurrentAction( $actionName, $view = '' ) 637 { 638 if ( $view == '' ) 639 $view = eZModule::currentView(); 640 if ( $view == '' or $actionName == '' ) 641 return false; 642 $this->ViewActions[$view] = $actionName; 643 } 644 645 /*! 646 \return the current action for the view \a $view. 647 648 If the current action is not yet determined it will use the definitions in 649 \c module.php for finding out the current action. It first looks trough 650 the \c single_post_actions array in the selected view mode, the key to 651 each element is the name of the post-variable to match, if it matches the 652 element value is set as the action. 653 \code 654 'single_post_actions' => array( 'PreviewButton' => 'Preview', 655 'PublishButton' => 'Publish' ) 656 \endcode 657 If none of these matches it will use the elements from the \c post_actions 658 array to find a match. It uses the element value for each element to match 659 agains a post-variable, if it is found the contents of the post-variable 660 is set as the action. 661 \code 662 'post_actions' => array( 'BrowseActionName' ) 663 \endcode 664 \sa setCurrentAction 665 */ 666 function currentAction( $view = '' ) 667 { 668 if ( $view == '' ) 669 $view = eZModule::currentView(); 670 if ( isset( $this->ViewActions[$view] ) ) 671 return $this->ViewActions[$view]; 672 include_once ( "lib/ezutils/classes/ezhttptool.php" ); 673 $http =& eZHTTPTool::instance(); 674 if ( isset( $this->Functions[$view]['default_action'] ) ) 675 { 676 $defaultAction = $this->Functions[$view]['default_action']; 677 foreach ( $defaultAction as $defaultActionStructure ) 678 { 679 $actionName = $defaultActionStructure['name']; 680 $type = $defaultActionStructure['type']; 681 if ( $type == 'post' ) 682 { 683 $parameters = array(); 684 if ( isset( $defaultActionStructure['parameters'] ) ) 685 $parameters = $defaultActionStructure['parameters']; 686 $hasParameters = true; 687 foreach ( $parameters as $parameterName ) 688 { 689 if ( !$http->hasPostVariable( $parameterName ) ) 690 { 691 $hasParameters = false; 692 break; 693 } 694 } 695 if ( $hasParameters ) 696 { 697 $this->ViewActions[$view] = $actionName; 698 return $this->ViewActions[$view]; 699 } 700 } 701 else 702 { 703 eZDebug::writeWarning( 'Unknown default action type: ' . $type, 'eZModule::currentAction' ); 704 } 705 } 706 } 707 if ( isset( $this->Functions[$view]['single_post_actions'] ) ) 708 { 709 $singlePostActions =& $this->Functions[$view]['single_post_actions']; 710 foreach( $singlePostActions as $postActionName => $realActionName ) 711 { 712 if ( $http->hasPostVariable( $postActionName ) ) 713 { 714 $this->ViewActions[$view] = $realActionName; 715 return $this->ViewActions[$view]; 716 } 717 } 718 } 719 if ( isset( $this->Functions[$view]['post_actions'] ) ) 720 { 721 $postActions =& $this->Functions[$view]['post_actions']; 722 foreach( $postActions as $postActionName ) 723 { 724 if ( $http->hasPostVariable( $postActionName ) ) 725 { 726 $this->ViewActions[$view] = $http->postVariable( $postActionName ); 727 return $this->ViewActions[$view]; 728 } 729 } 730 } 731 /* if ( isset( $this->Functions[$view]['group_post_actions'] ) ) 732 { 733 $singlePostActions =& $this->Functions[$view]['group_post_actions']; 734 foreach( $singlePostActions as $postActionName => $realActionName ) 735 { 736 if ( $http->hasPostVariable( $postActionName ) ) 737 { 738 $this->ViewActions[$view] = $realActionName; 739 return $this->ViewActions[$view]; 740 } 741 } 742 } 743 */ 744 $this->ViewActions[$view] = false; 745 return false; 746 } 747 748 function setActionParameter( $parameterName, $parameterValue, $view = '' ) 749 { 750 if ( $view == '' ) 751 $view = eZModule::currentView(); 752 $this->ViewActionParameters[$view][$parameterName] = $parameterValue; 753 } 754 755 function actionParameter( $parameterName, $view = '' ) 756 { 757 if ( $view == '' ) 758 $view = eZModule::currentView(); 759 if ( isset( $this->ViewActionParameters[$view][$parameterName] ) ) 760 return $this->ViewActionParameters[$view][$parameterName]; 761 $currentAction = $this->currentAction( $view ); 762 include_once ( "lib/ezutils/classes/ezhttptool.php" ); 763 $http =& eZHTTPTool::instance(); 764 if ( isset( $this->Functions[$view]['post_action_parameters'][$currentAction] ) ) 765 { 766 $postParameters =& $this->Functions[$view]['post_action_parameters'][$currentAction]; 767 if ( isset( $postParameters[$parameterName] ) and 768 $http->hasPostVariable( $postParameters[$parameterName] ) ) 769 return $http->postVariable( $postParameters[$parameterName] ); 770 eZDebug::writeError( "No such action parameter: $parameterName", 'eZModule::actionParameter' ); 771 } 772 if ( isset( $this->Functions[$view]['post_value_action_parameters'][$currentAction] ) ) 773 { 774 $postParameters =& $this->Functions[$view]['post_value_action_parameters'][$currentAction]; 775 if ( isset( $postParameters[$parameterName] ) ) 776 { 777 $postVariables =& $http->attribute( 'post' ); 778 $postVariableNameMatch = $postParameters[$parameterName]; 779 $regMatch = "/^" . $postVariableNameMatch . "_(.+)$/"; 780 foreach ( $postVariables as $postVariableName => $postVariableValue ) 781 { 782 if ( preg_match( $regMatch, $postVariableName, $matches ) ) 783 { 784 $parameterValue = $matches[1]; 785 $this->ViewActionParameters[$view][$parameterName] = $parameterValue; 786 return $parameterValue; 787 } 788 } 789 eZDebug::writeError( "No such action parameter: $parameterName", 'eZModule::actionParameter' ); 790 } 791 } 792 return null; 793 } 794 795 function hasActionParameter( $parameterName, $view = '' ) 796 { 797 if ( $view == '' ) 798 $view = eZModule::currentView(); 799 if ( isset( $this->ViewActionParameters[$view][$parameterName] ) ) 800 return true; 801 $currentAction = $this->currentAction( $view ); 802 include_once ( "lib/ezutils/classes/ezhttptool.php" ); 803 $http =& eZHTTPTool::instance(); 804 if ( isset( $this->Functions[$view]['post_action_parameters'][$currentAction] ) ) 805 { 806 $postParameters =& $this->Functions[$view]['post_action_parameters'][$currentAction]; 807 if ( isset( $postParameters[$parameterName] ) and 808 $http->hasPostVariable( $postParameters[$parameterName] ) ) 809 return true; 810 } 811 if ( isset( $this->Functions[$view]['post_value_action_parameters'][$currentAction] ) ) 812 { 813 $postParameters =& $this->Functions[$view]['post_value_action_parameters'][$currentAction]; 814 if ( isset( $postParameters[$parameterName] ) ) 815 { 816 $postVariables =& $http->attribute( 'post' ); 817 $postVariableNameMatch = $postParameters[$parameterName]; 818 $regMatch = "/^" . $postVariableNameMatch . "_(.+)$/"; 819 foreach ( $postVariables as $postVariableName => $postVariableValue ) 820 { 821 if ( preg_match( $regMatch, $postVariableName, $matches ) ) 822 { 823 $parameterValue = $matches[1]; 824 $this->ViewActionParameters[$view][$parameterName] = $parameterValue; 825 return true; 826 } 827 } 828 } 829 } 830 return false; 831 } 832 833 /*! 834 \return true if the current action matches the action name \a $actionName in view \a $view. 835 Always returns false if either \a $view or \a $actionName is empty. 836 \sa currentAction, setCurrentAction 837 */ 838 function isCurrentAction( $actionName, $view = '' ) 839 { 840 if ( $view == '' ) 841 $view = eZModule::currentView(); 842 if ( $view == '' or $actionName == '' ) 843 return false; 844 return $this->currentAction( $view ) == $actionName; 845 } 846 847 /*! 848 Adds an entry to the hook named \a $hookName. The entry is placed 849 before all other existing entries unless \a $append is set to \c true 850 in which case the entry is placed at the end. 851 \param $function Either the name of the function to be run or an 852 array where the first entry is the object and the second 853 is the method name. 854 */ 855 function addHook( $hookName, $function, $priority = 1, $expandParameters = true, $append = false ) 856 { 857 $hookEntries =& $this->HookList[$hookName]; 858 if ( !is_array( $hookEntries ) ) 859 $hookEntries = array(); 860 $entry = array( 'function' => $function, 861 'expand_parameters' => $expandParameters ); 862 863 $position = $priority; 864 if ( $append ) 865 { 866 while ( isset( $hookEntries[$position] ) ) 867 ++$position; 868 } 869 else 870 { 871 while ( isset( $hookEntries[$position] ) ) 872 --$position; 873 } 874 $hookEntries[$position] = $entry; 875 } 876 877 /*! 878 Runs all hooks found in the hook list named \a $hookName. 879 The parameter array \a $parameters will be passed to each hook function. 880 */ 881 function runHooks( $hookName, $parameters = null ) 882 { 883 $status = null; 884 $hookEntries =& $this->HookList[$hookName]; 885 if ( isset( $hookEntries ) and 886 is_array( $hookEntries ) ) 887 { 888 ksort( $hookEntries ); 889 foreach ( array_keys( $hookEntries ) as $hookKey ) 890 { 891 $hookEntry =& $hookEntries[$hookKey]; 892 $function =& $hookEntry['function']; 893 $expandParameters =& $hookEntry['expand_parameters']; 894 if ( is_string( $function ) ) 895 { 896 $functionName = $function; 897 if ( function_exists( $functionName ) ) 898 { 899 if ( $parameters === null or $expandParameters === null ) 900 $retVal =& $functionName( $this ); 901 else if ( $expandParameters ) 902 $retVal = call_user_func_array( $functionName, array_merge( array( &$this ), $parameters ) ); 903 else 904 $retVal =& $functionName( $this, $parameters ); 905 } 906 else 907 eZDebug::writeError( "Unknown hook function '$functionName' in hook: $hookName", 'eZModule::runHooks' ); 908 } 909 else if ( is_array( $function ) ) 910 { 911 if ( isset( $function[0] ) and 912 isset( $function[1] ) ) 913 { 914 $object =& $function[0]; 915 $functionName = $function[1]; 916 if ( method_exists( $object, $functionName ) ) 917 { 918 if ( $parameters === null ) 919 $retVal =& $object->$function( $this ); 920 else if ( $expandParameters ) 921 $retVal = call_user_method_array( $functionName, $object, array_merge( array( &$this ), $parameters ) ); 922 else 923 $retVal =& $object->$functionName( $this, $parameters ); 924 } 925 else 926 eZDebug::writeError( "Unknown hook method '$functionName' in class '" . get_class( $object ) . "' in hook: $hookName", 'eZModule::runHooks' ); 927 } 928 else 929 eZDebug::writeError( "Missing data for method handling in hook: $hookName", 'eZModule::runHooks' ); 930 } 931 else 932 eZDebug::writeError( 'Unknown entry type ' . gettype( $function ) . 'in hook: ' . $hookName, 'eZModule::runHooks' ); 933 934 switch( $retVal ) 935 { 936 case EZ_MODULE_HOOK_STATUS_OK: 937 break; 938 case EZ_MODULE_HOOK_STATUS_FAILED: 939 { 940 eZDebug::writeWarning( 'Hook execution failed in hook: ' . $hookName, 'eZModule::runHooks' ); 941 break; 942 } 943 case EZ_MODULE_HOOK_STATUS_CANCEL_RUN: 944 { 945 $status = $retVal; 946 return $status; 947 break; 948 } 949 } 950 } 951 } 952 return $status; 953 } 954 955 function setViewResult( &$result, $view = '' ) 956 { 957 if ( $view == '' ) 958 $view = $this->currentView(); 959 $this->ViewResult[$view] =& $result; 960 } 961 962 function hasViewResult( $view = '' ) 963 { 964 if ( $view == '' ) 965 $view = $this->currentView(); 966 return isset( $this->ViewResult[$view] ); 967 } 968 969 function &viewResult( $view = '' ) 970 { 971 if ( $view == '' ) 972 $view = $this->currentView(); 973 if ( isset( $this->ViewResult[$view] ) ) 974 return $this->ViewResult[$view]; 975 $retValue = null; 976 return $retValue; 977 } 978 979 function &forward( &$module, $functionName, $parameters = false ) 980 { 981 $Return = null; 982 if ( $module && $functionName ) 983 { 984 $viewName = eZModule::currentView(); 985 986 if ( $parameters === false) 987 { 988 $parameters = array(); 989 } 990 991 $parameters = array_merge( $parameters, $this->OriginalViewParameters ); 992 $unorderedParameters = $this->OriginalUnorderedParameters; 993 $userParameters = $this->UserParameters; 994 995 $Return =& $module->run( $functionName, $parameters, $unorderedParameters, $userParameters ); 996 997 // override default navigation part 998 if ( $Return['is_default_navigation_part'] === true ) 999 { 1000 if ( $this->singleFunction() ) 1001 $function =& $this->Module["function"]; 1002 else 1003 $function =& $this->Functions[$functionName]; 1004 1005 if ( isset( $function['default_navigation_part'] ) ) 1006 { 1007 $Return['navigation_part'] = $function['default_navigation_part']; 1008 } 1009 } 1010 1011 $this->RedirectURI = $module->redirectURI(); 1012 $this->setExitStatus( $module->exitStatus() ); 1013 } 1014 return $Return; 1015 } 1016 1017 /*! 1018 Tries to run the function \a $functionName in the current module. 1019 \param parameters An indexed list of parameters, these will be mapped 1020 onto real parameter names using the defined parameter 1021 names in the module/function definition. 1022 If this variable is shorter than the required parameters 1023 they will be set to \c null. 1024 \param overrideParameters An associative array of parameters which 1025 will override any parameters found using the 1026 defined parameters. 1027 \return null if function could not be run or no return value was found. 1028 */ 1029 function &run( $functionName, $parameters, $overrideParameters = false, $userParameters = false ) 1030 { 1031 if ( count( $this->Functions ) > 0 and 1032 !isset( $this->Functions[$functionName] ) ) 1033 { 1034 eZDebug::writeError( "Undefined view: " . $this->Module["name"] . "::$functionName ", 1035 "eZModule" ); 1036 $this->setExitStatus( EZ_MODULE_STATUS_FAILED ); 1037 $Return = null; 1038 return $Return; 1039 } 1040 if ( $this->singleFunction() ) 1041 $function =& $this->Module["function"]; 1042 else 1043 $function =& $this->Functions[$functionName]; 1044 $functionParameterDefinitions =& $function["params"]; 1045 $params = array(); 1046 $i = 0; 1047 $parameterValues = array(); 1048 if ( isset( $functionParameterDefinitions ) ) 1049 { 1050 foreach ( $functionParameterDefinitions as $param ) 1051 { 1052 if ( isset( $parameters[$i] ) ) 1053 { 1054 $params[$param] = $parameters[$i]; 1055 $parameterValues[] = $parameters[$i]; 1056 } 1057 else 1058 { 1059 $params[$param] = null; 1060 $parameterValues[] = null; 1061 } 1062 ++$i; 1063 } 1064 } 1065 1066 $this->ViewParameters =& $parameters; 1067 $this->OriginalParameters = $parameters; 1068 $this->OriginalViewParameters = $parameterValues; 1069 $this->NamedParameters = $params; 1070 1071 $GLOBALS['eZRequestedModuleParams'] = array( 'module_name' => $this->Name, 1072 'function_name' => $functionName, 1073 'parameters' => $params ); 1074 1075 $this->UserParameters = $userParameters; 1076 1077 if ( isset( $function['ui_context'] ) ) 1078 { 1079 $this->UIContext = $function['ui_context']; 1080 } 1081 if ( isset( $function['ui_component'] ) ) 1082 { 1083 $this->UIComponent = $function['ui_component']; 1084 } 1085 else if ( $this->UIComponentMatch == 'view' ) 1086 { 1087 $this->UIComponent = $functionName; 1088 } 1089 1090 if ( array_key_exists( 'Limitation', $parameters ) ) 1091 { 1092 $params['Limitation'] =& $parameters[ 'Limitation' ]; 1093 } 1094 1095 // check for unordered parameters and initialize variables if they exist 1096 $unorderedParametersList = array(); 1097 $unorderedParameters = array(); 1098 if ( isset( $function["unordered_params"] ) ) 1099 { 1100 $unorderedParams =& $function["unordered_params"]; 1101 1102 foreach ( $unorderedParams as $urlParamName => $variableParamName ) 1103 { 1104 if ( in_array( $urlParamName, $parameters ) ) 1105 { 1106 $pos = array_search( $urlParamName, $parameters ); 1107 1108 $params[$variableParamName] = $parameters[$pos + 1]; 1109 $unorderedParameters[$variableParamName] = $parameters[$pos + 1]; 1110 $unorderedParametersList[$variableParamName] = $parameters[$pos + 1]; 1111 } 1112 else 1113 { 1114 $params[$variableParamName] = false; 1115 $unorderedParameters[$variableParamName] = false; 1116 } 1117 } 1118 } 1119 1120 // Loop through user defines parameters 1121 if ( $userParameters !== false ) 1122 { 1123 if ( !isset( $params['UserParameters'] ) or 1124 !is_array( $params['UserParameters'] ) ) 1125 { 1126 $params['UserParameters'] = array(); 1127 } 1128 1129 if ( is_array( $userParameters ) && count( $userParameters ) > 0 ) 1130 { 1131 foreach ( array_keys( $userParameters ) as $paramKey ) 1132 { 1133 if( isset( $function['unordered_params'] ) && 1134 $unorderedParams != null ) 1135 { 1136 if ( array_key_exists( $paramKey, $unorderedParams ) ) 1137 { 1138 $params[$unorderedParams[$paramKey]] = $userParameters[$paramKey]; 1139 $unorderedParametersList[$unorderedParams[$paramKey]] = $userParameters[$paramKey]; 1140 } 1141 } 1142 1143 $params['UserParameters'][$paramKey] = $userParameters[$paramKey]; 1144 } 1145 } 1146 } 1147 1148 $this->OriginalUnorderedParameters = $unorderedParametersList; 1149 1150 if ( is_array( $overrideParameters ) ) 1151 { 1152 foreach ( $overrideParameters as $param => $value ) 1153 { 1154 $params[$param] = $value; 1155 } 1156 } 1157 $params["Module"] =& $this; 1158 $params["ModuleName"] = $this->Name; 1159 $params["FunctionName"] = $functionName; 1160 $params["Parameters"] =& $parameters; 1161 $params_as_var = isset( $this->Module["variable_params"] ) ? $this->Module["variable_params"] : false; 1162 include_once ( "lib/ezutils/classes/ezprocess.php" ); 1163 $this->ExitStatus = EZ_MODULE_STATUS_OK; 1164 // eZDebug::writeNotice( $params, 'module parameters1' ); 1165 1166 1167 $currentView =& $GLOBALS['eZModuleCurrentView']; 1168 $viewStack =& $GLOBALS['eZModuleViewStack']; 1169 if ( !isset( $currentView ) ) 1170 $currentView = false; 1171 if ( !isset( $viewStack ) ) 1172 $viewStack = array(); 1173 if ( is_array( $currentView ) ) 1174 $viewStack[] = $currentView; 1175 $currentView = array( 'view' => $functionName, 1176 'module' => $this->Name ); 1177 $Return = eZProcess::run( $this->Path . "/" . $this->Name . "/" . $function["script"], 1178 $params, 1179 $params_as_var ); 1180 1181 if ( $this->hasViewResult( $functionName ) ) 1182 { 1183 $Return = $this->viewResult( $functionName ); 1184 } 1185 1186 if ( count( $viewStack ) > 0 ) 1187 $currentView = array_pop( $viewStack ); 1188 else 1189 $currentView = false; 1190 1191 // Check if the module has set the navigation part, if not default to module setting 1192 if ( !isset( $Return['navigation_part'] ) ) 1193 { 1194 $Return['is_default_navigation_part'] = true; 1195 if ( isset( $function['default_navigation_part'] ) ) 1196 $Return['navigation_part'] = $function['default_navigation_part']; 1197 1198 } 1199 else 1200 { 1201 $Return['is_default_navigation_part'] = false; 1202 } 1203 1204 // Check if we have overrides for navigation part 1205 $viewName = $this->Name . '/' . $functionName; 1206 if ( isset( $this->NavigationParts[$viewName] ) ) 1207 { 1208 $Return['is_default_navigation_part'] = false; 1209 $Return['navigation_part'] = $this->NavigationParts[$viewName]; 1210 } 1211 else if ( isset( $this->NavigationParts[$this->Name] ) ) 1212 { 1213 $Return['is_default_navigation_part'] = false; 1214 $Return['navigation_part'] = $this->NavigationParts[$this->Name]; 1215 } 1216 1217 return $Return; 1218 } 1219 1220 /*! 1221 \static 1222 \return the current view which is run or \c false if no view is active. 1223 \sa currentModule 1224 */ 1225 function currentView() 1226 { 1227 $currentView =& $GLOBALS['eZModuleCurrentView']; 1228 if ( $currentView !== false ) 1229 return $currentView['view']; 1230 return false; 1231 } 1232 1233 /*! 1234 \static 1235 \return the current module which is run or \c false if no module is active. 1236 \sa currentModule 1237 */ 1238 function currentModule() 1239 { 1240 $currentView =& $GLOBALS['eZModuleCurrentView']; 1241 if ( $currentView !== false ) 1242 return $currentView['module']; 1243 return false; 1244 } 1245 1246 /*! 1247 \static 1248 \return the global path list which is used for finding modules. Returns \c null if no 1249 list is available. 1250 \sa setGlobalPathList, addGlobalPathList 1251 */ 1252 function globalPathList() 1253 { 1254 if ( !isset( $GLOBALS['eZModuleGlobalPathList'] ) ) 1255 return null; 1256 return $GLOBALS['eZModuleGlobalPathList']; 1257 } 1258 1259 /*! 1260 \static 1261 Sets the global path list which is used for finding modules. 1262 \param $pathList Is either an array with path strings or a single path string 1263 \sa addGlobalPathList 1264 */ 1265 function setGlobalPathList( $pathList ) 1266 { 1267 $globalPathList =& $GLOBALS['eZModuleGlobalPathList']; 1268 if ( !is_array( $pathList ) ) 1269 $pathList = array( $pathList ); 1270 $globalPathList = $pathList; 1271 } 1272 1273 /*! 1274 \static 1275 Adds the pathlist entries \a $pathList to the global path list which is used for finding modules. 1276 \param $pathList Is either an array with path strings or a single path string 1277 \sa setGlobalPathList 1278 */ 1279 function addGlobalPathList( $pathList ) 1280 { 1281 $globalPathList =& $GLOBALS['eZModuleGlobalPathList']; 1282 if ( !is_array( $globalPathList ) ) 1283 $globalPathList = array(); 1284 if ( !is_array( $pathList ) ) 1285 $pathList = array( $pathList ); 1286 $globalPathList = array_merge( $globalPathList, $pathList ); 1287 } 1288 1289 /*! 1290 \static 1291 Tries to locate the module named \a $moduleName and returns an eZModule object 1292 for it. Returns \c null if no module can be found. 1293 1294 It uses the globalPathList() to search for modules, use \a $pathList to add 1295 additional path. 1296 \param $moduleName The name of the module to find 1297 \param $pathList Is either an array with path strings or a single path string 1298 */ 1299 function &exists( $moduleName, $pathList = null ) 1300 { 1301 $module = null; 1302 eZModule::findModule( $moduleName, $module, $pathList ); 1303 return $module; 1304 } 1305 1306 /*! 1307 \static 1308 Tries to locate the module named \a $moduleName and sets the \a $module parameter 1309 with the eZModule object, if \a $module is already a module object it's contents 1310 are overwritten. Returns \c null if no module can be found. 1311 1312 It uses the globalPathList() to search for modules, use \a $pathList to add 1313 additional path. 1314 \param $moduleName The name of the module to find 1315 \param $pathList Is either an array with path strings or a single path string 1316 */ 1317 function &findModule( $moduleName, &$module, $pathList = null ) 1318 { 1319 if ( $pathList === null ) 1320 $pathList = array(); 1321 else if ( !is_array( $pathList ) ) 1322 $pathList = array( $pathList ); 1323 $searchPathList = eZModule::globalPathList(); 1324 if ( $searchPathList === null ) 1325 $searchPathList = array(); 1326 $searchPathList = array_merge( $searchPathList, $pathList ); 1327 $triedList = array(); 1328 $foundADir = false; 1329 foreach ( $searchPathList as $path ) 1330 { 1331 $dir = "$path/$moduleName"; 1332 $file = "$dir/module.php"; 1333 if ( file_exists( $file ) ) 1334 { 1335 if ( $module === null ) 1336 $module = new eZModule( $path, $file, $moduleName ); 1337 else 1338 $module->initialize( $path, $file, $moduleName ); 1339 return $module; 1340 } 1341 else if ( !file_exists( $dir ) ) 1342 { 1343 $triedDirList[] = $dir; 1344 } 1345 else 1346 { 1347 $foundADir = true; 1348 $triedList[] = $dir; 1349 } 1350 } 1351 if ( $foundADir ) 1352 { 1353 $msg = ( "Could not find module named '$moduleName'\n" . 1354 "These directories had a directory named '$moduleName' but did not contain the module.php file:\n" . implode( ", ", $triedList ) . "\n" . 1355 "This usually means it is missing or has a wrong name." ); 1356 if ( count( $triedDirList ) > 0 ) 1357 $msg .= "\n\nThese directories were tried too but none of them exists:\n" . implode( ', ', $triedDirList ); 1358 eZDebug::writeWarning( $msg ); 1359 } 1360 else 1361 { 1362 eZDebug::writeWarning( "Could not find module named '$moduleName'\n" . 1363 "These directories were tried none of them exists:\n" . implode( ", ", $triedDirList ) ); 1364 } 1365 $retValue = null; 1366 return $retValue; 1367 } 1368 function &getNamedParameters() 1369 { 1370 return $this->NamedParameters; 1371 } 1372 1373 /// \privatesection 1374 var $Functions; 1375 var $Module; 1376 var $Name; 1377 var $Path; 1378 var $ExitStatus; 1379 var $ErrorCode; 1380 var $RedirectURI; 1381 var $RedirectStatus; 1382 var $Title; 1383 var $HookList; 1384 var $ViewActions; 1385 var $ViewResult; 1386 var $ViewParameters; 1387 var $OriginalParameters; 1388 var $OriginalViewParameters; 1389 var $NamedParameters; 1390 var $OriginalUnorderedParameters; 1391 var $UserParameters; 1392 1393 /// The current UI context, by default 'navigation' but can be changed depending on module or PHP code 1394 var $UIContext; 1395 /// The current UI context, by default the current module but can be changed depending on module or PHP code 1396 var $UIComponent; 1397 /// Controls at which level UI component matching is done, either 'module' which uses module name or 'view' which uses view name 1398 var $UIComponentMatch; 1399 } 1400 1401 ?>
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
Généré le : Sat Feb 24 10:30:04 2007 | par Balluche grâce à PHPXref 0.7 |