PageRenderTime 45ms CodeModel.GetById 9ms RepoModel.GetById 1ms app.codeStats 0ms

/Croogo/Model/Behavior/TrackableBehavior.php

https://github.com/kareypowell/croogo
PHP | 130 lines | 78 code | 16 blank | 36 comment | 12 complexity | 78d000b01eb9be30c9c487c30a70b8af MD5 | raw file
  1. <?php
  2. App::uses('ModelBehavior', 'Model');
  3. App::uses('AuthComponent', 'Controller/Component');
  4. App::uses('CakeSession', 'Model/Datasource');
  5. /**
  6. * Trackable Behavior
  7. *
  8. * Populate `created_by` and `updated_by` fields from session data.
  9. *
  10. * @package Croogo.Croogo.Model.Behavior
  11. * @since 1.6
  12. * @author Rachman Chavik <rchavik@gmail.com>
  13. * @license http://www.opensource.org/licenses/mit-license.php The MIT License
  14. * @link http://www.croogo.org
  15. */
  16. class TrackableBehavior extends ModelBehavior {
  17. /**
  18. * Default settings
  19. */
  20. protected $_defaults = array(
  21. 'userModel' => 'Users.User',
  22. 'fields' => array(
  23. 'created_by' => 'created_by',
  24. 'updated_by' => 'updated_by',
  25. ),
  26. );
  27. /**
  28. * Setup
  29. */
  30. public function setup(Model $model, $config = array()) {
  31. $this->settings[$model->alias] = Set::merge($this->_defaults, $config);
  32. if ($this->_hasTrackableFields($model)) {
  33. $this->_setupBelongsTo($model);
  34. }
  35. }
  36. /**
  37. * Checks wether model has the required fields
  38. *
  39. * @return bool True if $model has the required fields
  40. */
  41. protected function _hasTrackableFields(Model $model) {
  42. $fields = $this->settings[$model->alias]['fields'];
  43. return
  44. $model->hasField($fields['created_by']) &&
  45. $model->hasField($fields['updated_by']);
  46. }
  47. /**
  48. * Bind relationship on the fly
  49. */
  50. protected function _setupBelongsTo(Model $model) {
  51. if (!empty($model->belongsTo['TrackableCreator'])) {
  52. return;
  53. }
  54. $config = $this->settings[$model->alias];
  55. list($plugin, $modelName) = pluginSplit($config['userModel']);
  56. $className = isset($plugin) ? $plugin . '.' . $modelName : $modelName;
  57. $model->bindModel(array(
  58. 'belongsTo' => array(
  59. 'TrackableCreator' => array(
  60. 'className' => $className,
  61. 'foreignKey' => $config['fields']['created_by'],
  62. ),
  63. 'TrackableUpdater' => array(
  64. 'className' => $className,
  65. 'foreignKey' => $config['fields']['updated_by'],
  66. ),
  67. )
  68. ), false);
  69. }
  70. /**
  71. * Fill the created_by and updated_by fields
  72. *
  73. * Note: Since shells do not have Sessions, created_by/updated_by fields
  74. * will not be populated. If a shell needs to populate these fields, you
  75. * can simulate a logged in user by setting `Trackable.Auth` config:
  76. *
  77. * Configure::write('Trackable.User', array('id' => 1));
  78. *
  79. * Note that value stored in this variable overrides session data.
  80. */
  81. public function beforeSave(Model $model, $options = array()) {
  82. if (!$this->_hasTrackableFields($model)) {
  83. return true;
  84. }
  85. $config = $this->settings[$model->alias];
  86. $User = ClassRegistry::init($config['userModel']);
  87. $userAlias = $User->alias;
  88. $userPk = $User->primaryKey;
  89. $user = Configure::read('Trackable.Auth.User');
  90. if (!$user && CakeSession::started()) {
  91. $user = AuthComponent::user();
  92. }
  93. if ($user && array_key_exists($userPk, $user)) {
  94. $userId = $user[$userPk];
  95. }
  96. if (empty($user) || empty($userId)) {
  97. return true;
  98. }
  99. $alias = $model->alias;
  100. $createdByField = $config['fields']['created_by'];
  101. $updatedByField = $config['fields']['updated_by'];
  102. if (empty($model->data[$alias][$createdByField])) {
  103. if (!$model->exists()) {
  104. $model->data[$alias][$createdByField] = $user[$userPk];
  105. }
  106. }
  107. $model->data[$alias][$updatedByField] = $userId;
  108. if (!empty($model->whitelist)) {
  109. $model->whitelist[] = $createdByField;
  110. $model->whitelist[] = $updatedByField;
  111. }
  112. return true;
  113. }
  114. }