PageRenderTime 25ms CodeModel.GetById 6ms RepoModel.GetById 0ms app.codeStats 0ms

/kernel/classes/ezworkflowtype.php

https://github.com/eeggenberger/ezpublish
PHP | 393 lines | 312 code | 45 blank | 36 comment | 24 complexity | 7f06c6ff341f55d3e6358517bae93eb7 MD5 | raw file
  1. <?php
  2. /**
  3. * File containing the eZWorkflowType class.
  4. *
  5. * @copyright Copyright (C) 1999-2011 eZ Systems AS. All rights reserved.
  6. * @license http://www.gnu.org/licenses/gpl-2.0.txt GNU General Public License v2
  7. * @version //autogentag//
  8. * @package kernel
  9. */
  10. //!! eZKernel
  11. //! The class eZWorkflowType does
  12. /*!
  13. */
  14. class eZWorkflowType
  15. {
  16. const STATUS_NONE = 0;
  17. const STATUS_ACCEPTED = 1;
  18. const STATUS_REJECTED = 2;
  19. const STATUS_DEFERRED_TO_CRON = 3;
  20. const STATUS_DEFERRED_TO_CRON_REPEAT = 4;
  21. const STATUS_RUN_SUB_EVENT = 5;
  22. const STATUS_WORKFLOW_CANCELLED = 6;
  23. const STATUS_FETCH_TEMPLATE = 7;
  24. const STATUS_FETCH_TEMPLATE_REPEAT = 8;
  25. const STATUS_REDIRECT = 10;
  26. const STATUS_WORKFLOW_DONE = 9;
  27. const STATUS_REDIRECT_REPEAT = 11;
  28. const STATUS_WORKFLOW_RESET = 12;
  29. function eZWorkflowType( $group, $type,
  30. $groupName, $name )
  31. {
  32. $this->Group = $group;
  33. $this->Type = $type;
  34. $this->TypeString = $group . "_" . $type;
  35. $this->GroupName = $groupName;
  36. $this->Name = $name;
  37. $this->Information = "";
  38. $this->ActivationDate = false;
  39. $this->Attributes = array();
  40. $this->Attributes["group"] =& $this->Group;
  41. $this->Attributes["type"] =& $this->Type;
  42. $this->Attributes["type_string"] =& $this->TypeString;
  43. $this->Attributes["group_name"] =& $this->GroupName;
  44. $this->Attributes["name"] =& $this->Name;
  45. $this->Attributes["information"] =& $this->Information;
  46. $this->Attributes["activation_date"] =& $this->ActivationDate;
  47. }
  48. static function statusName( $status )
  49. {
  50. $statusNameMap = self::statusNameMap();
  51. if ( isset( $statusNameMap[$status] ) )
  52. return $statusNameMap[$status];
  53. return false;
  54. }
  55. static function createType( $typeString )
  56. {
  57. $types =& $GLOBALS["eZWorkflowTypes"];
  58. if ( !isset( $types[$typeString] ) )
  59. {
  60. $result = eZWorkflowType::loadAndRegisterType( $typeString );
  61. if ( $result === false )
  62. return null;
  63. }
  64. if ( isset( $types[$typeString] ) )
  65. {
  66. $class_name = $types[$typeString]["class_name"];
  67. if ( !isset( $GLOBALS["eZWorkflowTypeObjects"][$typeString] ) )
  68. {
  69. if ( class_exists( $class_name ) )
  70. {
  71. $GLOBALS["eZWorkflowTypeObjects"][$typeString] = new $class_name();
  72. }
  73. else
  74. {
  75. eZDebug::writeError( "Undefined event type class: $class_name", __METHOD__ );
  76. }
  77. }
  78. return $GLOBALS["eZWorkflowTypeObjects"][$typeString];
  79. }
  80. else
  81. {
  82. eZDebug::writeError( "Undefined type: $typeString", __METHOD__ );
  83. }
  84. return null;
  85. }
  86. static function fetchRegisteredTypes()
  87. {
  88. eZWorkflowType::loadAndRegisterAllTypes();
  89. $types = $GLOBALS["eZWorkflowTypes"];
  90. if ( is_array( $types ) )
  91. {
  92. foreach ( $types as $typeString => $type_def )
  93. {
  94. $class_name = $type_def["class_name"];
  95. $def =& $definition_objects[$typeString];
  96. if ( !isset( $GLOBALS["eZWorkflowTypeObjects"][$typeString] ) )
  97. {
  98. if ( class_exists( $class_name ) )
  99. {
  100. $GLOBALS["eZWorkflowTypeObjects"][$typeString] = new $class_name();
  101. }
  102. else
  103. {
  104. eZDebug::writeError( "Undefined event type class: $class_name", __METHOD__ );
  105. }
  106. }
  107. }
  108. }
  109. return $GLOBALS["eZWorkflowTypeObjects"];
  110. }
  111. static function allowedTypes()
  112. {
  113. if ( !isset( $GLOBALS["eZWorkflowAllowedTypes"] ) ||
  114. !is_array( $GLOBALS["eZWorkflowAllowedTypes"] ) )
  115. {
  116. $wfINI = eZINI::instance( 'workflow.ini' );
  117. $eventTypes = $wfINI->variable( "EventSettings", "AvailableEventTypes" );
  118. $GLOBALS["eZWorkflowAllowedTypes"] = array_unique( $eventTypes );
  119. }
  120. return $GLOBALS["eZWorkflowAllowedTypes"];
  121. }
  122. static function loadAndRegisterAllTypes()
  123. {
  124. $allowedTypes = eZWorkflowType::allowedTypes();
  125. foreach( $allowedTypes as $type )
  126. {
  127. eZWorkflowType::loadAndRegisterType( $type );
  128. }
  129. }
  130. static function registerType( $group, $type, $class_name )
  131. {
  132. $typeString = $group . "_" . $type;
  133. if ( !isset( $GLOBALS["eZWorkflowTypes"] ) || !is_array( $GLOBALS["eZWorkflowTypes"] ) )
  134. {
  135. $GLOBALS["eZWorkflowTypes"] = array();
  136. }
  137. if ( isset( $GLOBALS["eZWorkflowTypes"][$typeString] ) )
  138. {
  139. eZDebug::writeError( "Type already registered: $typeString", __METHOD__ );
  140. }
  141. else
  142. {
  143. $GLOBALS["eZWorkflowTypes"][$typeString] = array( "class_name" => $class_name );
  144. }
  145. }
  146. static function loadAndRegisterType( $typeString )
  147. {
  148. $typeElements = explode( "_", $typeString );
  149. if ( count( $typeElements ) < 2 )
  150. {
  151. eZDebug::writeError( "Workflow type not found: $typeString", __METHOD__ );
  152. return false;
  153. }
  154. $types =& $GLOBALS["eZWorkflowTypes"];
  155. if ( isset( $types[ $typeString ] ) and
  156. isset( $types[ $typeString ][ 'class_name' ] ) and
  157. class_exists( $types[ $typeString ][ 'class_name' ] ) )
  158. {
  159. return true;
  160. }
  161. $group = $typeElements[0];
  162. $type = $typeElements[1];
  163. $baseDirectory = eZExtension::baseDirectory();
  164. $wfINI = eZINI::instance( 'workflow.ini' );
  165. $repositoryDirectories = $wfINI->variable( 'EventSettings', 'RepositoryDirectories' );
  166. $extensionDirectories = $wfINI->variable( 'EventSettings', 'ExtensionDirectories' );
  167. foreach ( $extensionDirectories as $extensionDirectory )
  168. {
  169. $extensionPath = $baseDirectory . '/' . $extensionDirectory . '/eventtypes';
  170. if ( file_exists( $extensionPath ) )
  171. $repositoryDirectories[] = $extensionPath;
  172. }
  173. foreach ( $repositoryDirectories as $repositoryDirectory )
  174. {
  175. $includeFile = "$repositoryDirectory/$group/$type/" . $type . "type.php";
  176. if ( file_exists( $includeFile ) )
  177. {
  178. include_once( $includeFile );
  179. return true;
  180. }
  181. }
  182. eZDebug::writeError( "Workflow type not found: $typeString, searched in these directories: " . implode( ', ', $repositoryDirectories ), __METHOD__ );
  183. return false;
  184. }
  185. function attributes()
  186. {
  187. return array_merge( array( 'description',
  188. 'allowed_triggers' ),
  189. array_keys( $this->Attributes ) );
  190. }
  191. function hasAttribute( $attr )
  192. {
  193. return in_array( $attr, $this->attributes() );
  194. }
  195. function attribute( $attr )
  196. {
  197. switch( $attr )
  198. {
  199. case 'description':
  200. {
  201. return $this->eventDescription();
  202. } break;
  203. case 'allowed_triggers':
  204. {
  205. return $this->TriggerTypes;
  206. } break;
  207. default:
  208. {
  209. if ( isset( $this->Attributes[$attr] ) )
  210. return $this->Attributes[$attr];
  211. } break;
  212. }
  213. eZDebug::writeError( "Attribute '$attr' does not exist", __METHOD__ );
  214. return null;
  215. }
  216. function setAttribute( $attr, $value )
  217. {
  218. if ( array_key_exists( $attr, $this->Attributes ) )
  219. $this->Attributes[$attr] = $value;
  220. }
  221. /*!
  222. Set trigger types.
  223. \param allowed trigger types format :
  224. array( <module> => array( <function> => array( <event> ) ) )
  225. if all is allowed,
  226. array( '*' => true )
  227. */
  228. function setTriggerTypes( $allowedTypes )
  229. {
  230. $this->TriggerTypes = $allowedTypes;
  231. }
  232. function eventDescription()
  233. {
  234. return $this->Attributes["name"];
  235. }
  236. function execute( $process, $event )
  237. {
  238. return eZWorkflowType::STATUS_NONE;
  239. }
  240. function initializeEvent( $event )
  241. {
  242. }
  243. function validateHTTPInput( $http, $base, $event, &$validation )
  244. {
  245. return eZInputValidator::STATE_ACCEPTED;
  246. }
  247. function fixupHTTPInput( $http, $base, $event )
  248. {
  249. return true;
  250. }
  251. function fetchHTTPInput( $http, $base, $event )
  252. {
  253. }
  254. function setActivationDate( $date )
  255. {
  256. $this->ActivationDate = $date;
  257. }
  258. function setInformation( $inf )
  259. {
  260. $this->Information = $inf;
  261. }
  262. function needCleanup()
  263. {
  264. return false;
  265. }
  266. function cleanupAfterRemoving( $attr = array() )
  267. {
  268. }
  269. function cleanup( $process, $event )
  270. {
  271. }
  272. function attributeDecoder( $event, $attr )
  273. {
  274. return null;
  275. }
  276. function typeFunctionalAttributes( )
  277. {
  278. return array();
  279. }
  280. function customWorkflowEventHTTPAction( $http, $action, $workflowEvent )
  281. {
  282. }
  283. function workflowEventContent( $event )
  284. {
  285. return "";
  286. }
  287. function storeEventData( $event, $version )
  288. {
  289. }
  290. function storeDefinedEventData( $event )
  291. {
  292. }
  293. /*!
  294. Check if specified trigger is allowed
  295. \param $moduleName module name
  296. \param $functionName function name
  297. \param $connectType connection type
  298. \return true is allowed, false if not.
  299. */
  300. function isAllowed( $moduleName, $functionName, $connectType )
  301. {
  302. if ( isset( $this->TriggerTypes['*'] ) )
  303. {
  304. return true;
  305. }
  306. else if ( isset( $this->TriggerTypes[$moduleName] ) )
  307. {
  308. if ( isset( $this->TriggerTypes[$moduleName][$functionName] ) )
  309. {
  310. if ( in_array( $connectType, $this->TriggerTypes[$moduleName][$functionName] ) )
  311. {
  312. return true;
  313. }
  314. }
  315. }
  316. return false;
  317. }
  318. /**
  319. * Get status name map.
  320. *
  321. * @return array Status name map
  322. */
  323. static function statusNameMap()
  324. {
  325. return array( self::STATUS_NONE => ezpI18n::tr( 'kernel/classes', 'No state yet' ),
  326. self::STATUS_ACCEPTED => ezpI18n::tr( 'kernel/classes', 'Accepted event' ),
  327. self::STATUS_REJECTED => ezpI18n::tr( 'kernel/classes', 'Rejected event' ),
  328. self::STATUS_DEFERRED_TO_CRON => ezpI18n::tr( 'kernel/classes', 'Event deferred to cron job' ),
  329. self::STATUS_DEFERRED_TO_CRON_REPEAT => ezpI18n::tr( 'kernel/classes', 'Event deferred to cron job, event will be rerun' ),
  330. self::STATUS_RUN_SUB_EVENT => ezpI18n::tr( 'kernel/classes', 'Event runs a sub event' ),
  331. self::STATUS_WORKFLOW_CANCELLED => ezpI18n::tr( 'kernel/classes', 'Canceled whole workflow' ),
  332. self::STATUS_WORKFLOW_RESET => ezpI18n::tr( 'kernel/classes', 'Workflow was reset for reuse' ) );
  333. }
  334. /// \privatesection
  335. public $Group;
  336. public $Type;
  337. public $TypeString;
  338. public $GroupName;
  339. public $Name;
  340. public $ActivationDate;
  341. public $Information;
  342. public $TriggerTypes = array( '*' => true );
  343. }
  344. ?>