PageRenderTime 728ms CodeModel.GetById 41ms RepoModel.GetById 2ms app.codeStats 0ms

/Croogo/Lib/CroogoStatus.php

https://github.com/kareypowell/croogo
PHP | 159 lines | 101 code | 23 blank | 35 comment | 15 complexity | 9a01e974f2b0f2d670278238af8eb176 MD5 | raw file
  1. <?php
  2. App::uses('AuthComponent', 'Controller/Component');
  3. App::uses('CakeLog', 'Log');
  4. App::uses('Permission', 'Model');
  5. /**
  6. * CroogoStatus
  7. *
  8. * @package Croogo.Croogo.Lib
  9. * @author Rachman Chavik <rchavik@xintesa.com>
  10. * @license http://www.opensource.org/licenses/mit-license.php The MIT License
  11. * @link http://www.croogo.org
  12. */
  13. class CroogoStatus implements ArrayAccess {
  14. const UNPUBLISHED = 0;
  15. const PUBLISHED = 1;
  16. const PREVIEW = 2;
  17. const PENDING = 0;
  18. const APPROVED = 1;
  19. const PROMOTED = 1;
  20. const UNPROMOTED = 0;
  21. protected $_statuses = array();
  22. /**
  23. * Constructor
  24. */
  25. public function __construct() {
  26. $this->_statuses = array(
  27. 'publishing' => array(
  28. self::UNPUBLISHED => __d('croogo', 'Unpublished'),
  29. self::PUBLISHED => __d('croogo', 'Published'),
  30. self::PREVIEW => __d('croogo', 'Preview'),
  31. ),
  32. 'approval' => array(
  33. self::APPROVED => __d('croogo', 'Approved'),
  34. self::PENDING => __d('croogo', 'Pending'),
  35. ),
  36. );
  37. $event = Croogo::dispatchEvent('Croogo.Status.setup', null, $this);
  38. }
  39. public function offsetExists($offset) {
  40. return isset($this->_statuses[$offset]);
  41. }
  42. public function &offsetGet($offset) {
  43. $result = null;
  44. if (isset($this->_statuses[$offset])) {
  45. $result =& $this->_statuses[$offset];
  46. }
  47. return $result;
  48. }
  49. public function offsetSet($offset, $value) {
  50. $this->_statuses[$offset] = $value;
  51. }
  52. public function offsetUnset($offset) {
  53. if (isset($this->_statuses[$offset])) {
  54. unset($this->_statuses[$offset]);
  55. }
  56. }
  57. /**
  58. * Returns a list of status id and its descriptions
  59. *
  60. * @return array List of status id and its descriptions
  61. */
  62. public function statuses($type = 'publishing') {
  63. if (array_key_exists($type, $this->_statuses)) {
  64. return $this->_statuses[$type];
  65. }
  66. return array();
  67. }
  68. /**
  69. * Gets valid statuses based on type
  70. *
  71. * @param string $type Status type if applicable
  72. * @return array Array of statuses
  73. */
  74. public function status($statusType = 'publishing', $accessType = 'public') {
  75. $values = $this->_defaultStatus($statusType);
  76. $data = compact('statusType', 'accessType', 'values');
  77. $event = Croogo::dispatchEvent('Croogo.Status.status', null, $data);
  78. if (array_key_exists('values', $event->data)) {
  79. return $event->data['values'];
  80. } else {
  81. return $values;
  82. }
  83. }
  84. /**
  85. * Default status
  86. */
  87. protected function _defaultStatus($statusType) {
  88. static $Permission = null;
  89. $status[$statusType] = array(self::PUBLISHED);
  90. $roleId = AuthComponent::user('role_id');
  91. $allow = false;
  92. if ($roleId && $roleId != 1) {
  93. if ($Permission === null) {
  94. $Permission = ClassRegistry::init('Permission');
  95. }
  96. try {
  97. $allow = $Permission->check(
  98. array('model' => 'Role', 'foreign_key' => $roleId),
  99. 'controllers/Nodes/Nodes/admin_edit'
  100. );
  101. } catch (CakeException $e) {
  102. CakeLog::error($e->getMessage());
  103. }
  104. }
  105. switch ($statusType) {
  106. case 'publishing':
  107. if ($roleId == 1 || $allow) {
  108. $status[$statusType][] = self::PREVIEW;
  109. }
  110. break;
  111. }
  112. return $status[$statusType];
  113. }
  114. /**
  115. * Get the status id from description
  116. *
  117. * @return int|mixed Status Id
  118. */
  119. public function byDescription($title, $statusType = 'publishing', $strict = true) {
  120. if (array_key_exists($statusType, $this->_statuses)) {
  121. return array_search($title, $this->_statuses[$statusType], $strict);
  122. }
  123. return false;
  124. }
  125. /**
  126. * Get the description from id
  127. *
  128. * @return string|null Status Description
  129. */
  130. public function byId($id, $statusType = 'publishing') {
  131. if (isset($this->_statuses[$statusType][$id])) {
  132. return $this->_statuses[$statusType][$id];
  133. }
  134. return null;
  135. }
  136. }