PageRenderTime 46ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/kernel/classes/ezcollaborationitemparticipantlink.php

http://github.com/ezsystems/ezpublish
PHP | 342 lines | 288 code | 35 blank | 19 comment | 29 complexity | fd86abb1ea14b48f2d988a893cd0a49b MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1
  1. <?php
  2. /**
  3. * File containing the eZCollaborationItemParticipantLink class.
  4. *
  5. * @copyright Copyright (C) eZ Systems AS. All rights reserved.
  6. * @license For full copyright and license information view LICENSE file distributed with this source code.
  7. * @version //autogentag//
  8. * @package kernel
  9. */
  10. /*!
  11. \class eZCollaborationItemParticipantLink ezcollaborationitemparticipantlink.php
  12. \brief The class eZCollaborationItemParticipantLink does
  13. */
  14. class eZCollaborationItemParticipantLink extends eZPersistentObject
  15. {
  16. const TYPE_USER = 1;
  17. const TYPE_USERGROUP = 2;
  18. // Everything from 1024 and above is considered custom and is specific per collaboration handler.
  19. const TYPE_CUSTOM = 1024;
  20. const ROLE_STANDARD = 1;
  21. const ROLE_OBSERVER = 2;
  22. const ROLE_OWNER = 3;
  23. const ROLE_APPROVER = 4;
  24. const ROLE_AUTHOR = 5;
  25. // Everything from 1024 and above is considered custom and is specific per collaboration handler.
  26. const ROLE_CUSTOM = 1024;
  27. static function definition()
  28. {
  29. return array( 'fields' => array( 'collaboration_id' => array( 'name' => 'CollaborationID',
  30. 'datatype' => 'integer',
  31. 'default' => 0,
  32. 'required' => true,
  33. 'foreign_class' => 'eZCollaborationItem',
  34. 'foreign_attribute' => 'id',
  35. 'multiplicity' => '1..*' ),
  36. 'participant_id' => array( 'name' => 'ParticipantID',
  37. 'datatype' => 'integer',
  38. 'default' => 0,
  39. 'required' => true,
  40. 'foreign_class' => 'eZContentObject',
  41. 'foreign_attribute' => 'id',
  42. 'multiplicity' => '1..*' ),
  43. 'participant_type' => array( 'name' => 'ParticipantType',
  44. 'datatype' => 'integer',
  45. 'default' => 1,
  46. 'required' => true ),
  47. 'participant_role' => array( 'name' => 'ParticipantRole',
  48. 'datatype' => 'integer',
  49. 'default' => 1,
  50. 'required' => true ),
  51. 'last_read' => array( 'name' => 'LastRead',
  52. 'datatype' => 'integer',
  53. 'default' => 0,
  54. 'required' => true ),
  55. 'created' => array( 'name' => 'Created',
  56. 'datatype' => 'integer',
  57. 'default' => 0,
  58. 'required' => true ),
  59. 'modified' => array( 'name' => 'Modified',
  60. 'datatype' => 'integer',
  61. 'default' => 0,
  62. 'required' => true ) ),
  63. 'keys' => array( 'collaboration_id', 'participant_id' ),
  64. 'function_attributes' => array( 'collaboration_item' => 'collaborationItem',
  65. 'participant' => 'participant',
  66. 'participant_type_string' => 'participantTypeString',
  67. 'participant_role_string' => 'participantRoleString',
  68. 'is_builtin_type' => 'isBuiltinType',
  69. 'is_builtin_role' => 'isBuiltinRole' ),
  70. 'class_name' => 'eZCollaborationItemParticipantLink',
  71. 'name' => 'ezcollab_item_participant_link' );
  72. }
  73. static function create( $collaborationID, $participantID,
  74. $participantRole = self::ROLE_STANDARD, $participantType = self::TYPE_USER )
  75. {
  76. $dateTime = time();
  77. $row = array( 'collaboration_id' => $collaborationID,
  78. 'participant_id' => $participantID,
  79. 'participant_role' => $participantRole,
  80. 'participant_type' => $participantType,
  81. 'created' => $dateTime,
  82. 'modified' => $dateTime );
  83. return new eZCollaborationItemParticipantLink( $row );
  84. }
  85. /*!
  86. \note transaction unsafe
  87. */
  88. static function setLastRead( $collaborationID, $userID = false, $timestamp = false )
  89. {
  90. if ( $userID === false )
  91. {
  92. $userID = eZUser::currentUserID();
  93. }
  94. if ( $timestamp === false )
  95. {
  96. $timestamp = time();
  97. }
  98. $db = eZDB::instance();
  99. $userID = (int) $userID;
  100. $timestamp = (int) $timestamp;
  101. $sql = "UPDATE ezcollab_item_participant_link set last_read='$timestamp'
  102. WHERE collaboration_id='$collaborationID' AND participant_id='$userID'";
  103. $db->query( $sql );
  104. if ( !empty( $GLOBALS["eZCollaborationItemParticipantLinkCache"][$collaborationID][$userID] ) )
  105. $GLOBALS["eZCollaborationItemParticipantLinkCache"][$collaborationID][$userID]->setAttribute( 'last_read', $timestamp );
  106. }
  107. static function fetch( $collaborationID, $participantID, $asObject = true )
  108. {
  109. if ( empty( $GLOBALS["eZCollaborationItemParticipantLinkCache"][$collaborationID][$participantID] ) )
  110. {
  111. $GLOBALS["eZCollaborationItemParticipantLinkCache"][$collaborationID][$participantID] =
  112. eZPersistentObject::fetchObject( eZCollaborationItemParticipantLink::definition(),
  113. null,
  114. array( "collaboration_id" => $collaborationID,
  115. 'participant_id' => $participantID ),
  116. $asObject );
  117. }
  118. return $GLOBALS["eZCollaborationItemParticipantLinkCache"][$collaborationID][$participantID];
  119. }
  120. static function fetchParticipantList( $parameters = array() )
  121. {
  122. $parameters = array_merge( array( 'as_object' => true,
  123. 'item_id' => false,
  124. 'offset' => false,
  125. 'limit' => false,
  126. 'sort_by' => false ),
  127. $parameters );
  128. $cacheHashKey = md5( serialize( $parameters ) );
  129. if ( isset( $GLOBALS['eZCollaborationItemParticipantLinkListCache'][$cacheHashKey] ) )
  130. {
  131. return $GLOBALS['eZCollaborationItemParticipantLinkListCache'][$cacheHashKey];
  132. }
  133. $itemID = $parameters['item_id'];
  134. $asObject = $parameters['as_object'];
  135. $offset = $parameters['offset'];
  136. $limit = $parameters['limit'];
  137. $linkList = null;
  138. $limitArray = null;
  139. if ( $offset and $limit )
  140. {
  141. $limitArray = array( 'offset' => $offset, 'length' => $limit );
  142. }
  143. $linkList = eZPersistentObject::fetchObjectList( eZCollaborationItemParticipantLink::definition(),
  144. null,
  145. array( "collaboration_id" => $itemID ),
  146. null, $limitArray,
  147. $asObject );
  148. foreach( $linkList as $linkItem )
  149. {
  150. if ( $asObject )
  151. {
  152. $participantID = $linkItem->attribute( 'participant_id' );
  153. }
  154. else
  155. {
  156. $participantID = $linkItem['participant_id'];
  157. }
  158. if ( !isset( $GLOBALS["eZCollaborationItemParticipantLinkCache"][$itemID][$participantID] ) )
  159. {
  160. $GLOBALS["eZCollaborationItemParticipantLinkCache"][$itemID][$participantID] = $linkItem;
  161. }
  162. }
  163. return $GLOBALS['eZCollaborationItemParticipantLinkListCache'][$cacheHashKey] = $linkList;
  164. }
  165. static function fetchParticipantMap( $originalParameters = array() )
  166. {
  167. $parameters = array_merge( array( 'sort_field' => 'role' ),
  168. $originalParameters );
  169. $itemID = $parameters['item_id'];
  170. $sortField = $parameters['sort_field'];
  171. $list = eZCollaborationItemParticipantLink::fetchParticipantList( $originalParameters );
  172. if ( $list === null )
  173. {
  174. $listMap = null;
  175. return $listMap;
  176. }
  177. $listMap = array();
  178. foreach ( $list as $listItem )
  179. {
  180. $sortKey = null;
  181. if ( $sortField == 'role' )
  182. {
  183. $sortKey = $listItem->attribute( 'participant_role' );
  184. }
  185. if ( $sortKey !== null )
  186. {
  187. if ( !isset( $listMap[$sortKey] ) )
  188. {
  189. if ( $sortField == 'role' )
  190. {
  191. $sortName = eZCollaborationItemParticipantLink::roleName( $itemID, $sortKey );
  192. }
  193. $listMap[$sortKey] = array( 'name' => $sortName,
  194. 'items' => array() );
  195. }
  196. $listMap[$sortKey]['items'][] = $listItem;
  197. }
  198. }
  199. return $listMap;
  200. }
  201. static function typeString( $participantType )
  202. {
  203. if ( !isset( $GLOBALS['eZCollaborationParticipantTypeMap'] ) )
  204. {
  205. $GLOBALS['eZCollaborationParticipantTypeMap'] = array( self::TYPE_USER => 'user',
  206. self::TYPE_USERGROUP => 'usergroup' );
  207. }
  208. if ( isset( $GLOBALS['eZCollaborationParticipantTypeMap'][$participantType] ) )
  209. {
  210. return $GLOBALS['eZCollaborationParticipantTypeMap'][$participantType];
  211. }
  212. return null;
  213. }
  214. static function roleString( $participantRole )
  215. {
  216. if ( empty( $GLOBALS['eZCollaborationParticipantRoleMap'] ) )
  217. {
  218. $GLOBALS['eZCollaborationParticipantRoleMap'] =
  219. array( self::ROLE_STANDARD => 'standard',
  220. self::ROLE_OBSERVER => 'observer',
  221. self::ROLE_OWNER => 'owner',
  222. self::ROLE_APPROVER => 'approver',
  223. self::ROLE_AUTHOR => 'author' );
  224. }
  225. $roleMap = $GLOBALS['eZCollaborationParticipantRoleMap'];
  226. if ( isset( $roleMap[$participantRole] ) )
  227. {
  228. return $roleMap[$participantRole];
  229. }
  230. return null;
  231. }
  232. static function roleName( $collaborationID, $roleID )
  233. {
  234. if ( $roleID < self::TYPE_CUSTOM )
  235. {
  236. if ( empty( $GLOBALS['eZCollaborationParticipantRoleNameMap'] ) )
  237. {
  238. $GLOBALS['eZCollaborationParticipantRoleNameMap'] =
  239. array( self::ROLE_STANDARD => ezpI18n::tr( 'kernel/classes', 'Standard' ),
  240. self::ROLE_OBSERVER => ezpI18n::tr( 'kernel/classes', 'Observer' ),
  241. self::ROLE_OWNER => ezpI18n::tr( 'kernel/classes', 'Owner' ),
  242. self::ROLE_APPROVER => ezpI18n::tr( 'kernel/classes', 'Approver' ),
  243. self::ROLE_AUTHOR => ezpI18n::tr( 'kernel/classes', 'Author' ) );
  244. }
  245. $roleNameMap = $GLOBALS['eZCollaborationParticipantRoleNameMap'];
  246. if ( isset( $roleNameMap[$roleID] ) )
  247. {
  248. return $roleNameMap[$roleID];
  249. }
  250. return null;
  251. }
  252. $item = eZCollaborationItem::fetch( $collaborationID );
  253. return $item->handler()->roleName( $collaborationID, $roleID );
  254. }
  255. function collaborationItem()
  256. {
  257. return eZCollaborationItem::fetch( $this->CollaborationID );
  258. }
  259. function participant()
  260. {
  261. if ( $this->ParticipantType == self::TYPE_USER )
  262. {
  263. return eZUser::fetch( $this->ParticipantID );
  264. }
  265. else if ( $this->ParticipantType == self::TYPE_USERGROUP )
  266. {
  267. return eZContentObject::fetch( $this->ParticipantID );
  268. }
  269. return null;
  270. }
  271. function participantTypeString()
  272. {
  273. if ( $this->ParticipantType < self::TYPE_CUSTOM )
  274. {
  275. return eZCollaborationItemParticipantLink::typeString( $this->ParticipantType );
  276. }
  277. $item = eZCollaborationItem::fetch( $this->CollaborationID );
  278. return $item->attribute( 'type_identifier' ) . '_' . $item->handler()->participantTypeString( $this->ParticipantType );
  279. }
  280. function participantRoleString()
  281. {
  282. if ( $this->ParticipantRole < self::ROLE_CUSTOM )
  283. {
  284. return eZCollaborationItemParticipantLink::roleString( $this->ParticipantRole );
  285. }
  286. $item = eZCollaborationItem::fetch( $this->CollaborationID );
  287. return $item->attribute( 'type_identifier' ) . '_' . $item->handler()->participantRoleString( $this->ParticipantRole );
  288. }
  289. function isBuiltinType()
  290. {
  291. return $this->ParticipantType < self::TYPE_CUSTOM;
  292. }
  293. function isBuiltinRole()
  294. {
  295. return $this->ParticipantRole < self::ROLE_CUSTOM;
  296. }
  297. /// \privatesection
  298. public $CollaborationID;
  299. public $ParticipantID;
  300. public $ParticipantType;
  301. public $IsRead;
  302. public $IsActive;
  303. public $Created;
  304. public $Modified;
  305. }
  306. ?>