PageRenderTime 103ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/plugins/Content/src/Model/Table/ContentTypesTable.php

http://github.com/QuickAppsCMS/QuickApps-CMS
PHP | 132 lines | 84 code | 8 blank | 40 comment | 3 complexity | fe6c338ecb85472c286197fa33d2fcc0 MD5 | raw file
Possible License(s): LGPL-2.1, MPL-2.0-no-copyleft-exception, GPL-3.0
  1. <?php
  2. /**
  3. * Licensed under The GPL-3.0 License
  4. * For full copyright and license information, please see the LICENSE.txt
  5. * Redistributions of files must retain the above copyright notice.
  6. *
  7. * @since 2.0.0
  8. * @author Christopher Castro <chris@quickapps.es>
  9. * @link http://www.quickappscms.org
  10. * @license http://opensource.org/licenses/gpl-3.0.html GPL-3.0 License
  11. */
  12. namespace Content\Model\Table;
  13. use Cake\Datasource\EntityInterface;
  14. use Cake\Event\Event;
  15. use Cake\ORM\Table;
  16. use Cake\Validation\Validator;
  17. use \ArrayObject;
  18. /**
  19. * Represents "content_types" database table.
  20. *
  21. * @property \User\Model\Table\RolesTable $Roles
  22. * @property \User\Model\Table\ContentTypePermissions $ContentTypePermissions
  23. * @method void unbindSluggable()
  24. */
  25. class ContentTypesTable extends Table
  26. {
  27. /**
  28. * Initialize a table instance. Called after the constructor.
  29. *
  30. * @param array $config Configuration options passed to the constructor
  31. * @return void
  32. */
  33. public function initialize(array $config)
  34. {
  35. $this->belongsToMany('User.Roles', [
  36. 'propertyName' => 'permissions',
  37. 'through' => 'Content.ContentTypePermissions',
  38. ]);
  39. $this->addBehavior('Sluggable', [
  40. 'label' => 'name',
  41. 'slug' => 'slug',
  42. 'on' => 'insert',
  43. ]);
  44. $this->addBehavior('Serializable', [
  45. 'columns' => ['settings', 'defaults']
  46. ]);
  47. }
  48. /**
  49. * {@inheritDoc}
  50. */
  51. public function patchEntity(EntityInterface $type, array $data, array $options = [])
  52. {
  53. $return = parent::patchEntity($type, $data);
  54. if (!empty($data['permissions'])) {
  55. $roles = [];
  56. foreach ($data['permissions'] as $rule => $ids) {
  57. foreach ($ids as $roleId) {
  58. if (!empty($roleId)) {
  59. $role = $this->Roles->get($roleId);
  60. $role->set('_joinData', $this->ContentTypePermissions->newEntity(['action' => $rule]));
  61. $roles[] = $role;
  62. }
  63. }
  64. }
  65. $return->set('permissions', $roles);
  66. }
  67. return $return;
  68. }
  69. /**
  70. * Default validation rules set.
  71. *
  72. * @param \Cake\Validation\Validator $validator The validator object
  73. * @return \Cake\Validation\Validator
  74. */
  75. public function validationDefault(Validator $validator)
  76. {
  77. $validator
  78. ->requirePresence('name')
  79. ->add('name', [
  80. 'notBlank' => [
  81. 'rule' => 'notBlank',
  82. 'message' => __d('content', 'You need to provide a content type name.'),
  83. ],
  84. 'length' => [
  85. 'rule' => ['minLength', 3],
  86. 'message' => __d('content', 'Name need to be at least 3 characters long.'),
  87. ],
  88. ])
  89. ->requirePresence('slug', 'create')
  90. ->notEmpty('slug', __d('content', 'Machine-name cannot be left empty.'), 'create')
  91. ->add('slug', 'checkSlug', [
  92. 'rule' => function ($value, $context) {
  93. return (preg_match('/^[a-z0-9\-]{3,}$/', $value) === 1);
  94. },
  95. 'message' => __d('content', 'Invalid machine-name.'),
  96. ])
  97. ->requirePresence('title_label')
  98. ->add('title_label', [
  99. 'notBlank' => [
  100. 'rule' => 'notBlank',
  101. 'message' => __d('content', 'You need to provide a "Title Label".'),
  102. ],
  103. 'length' => [
  104. 'rule' => ['minLength', 3],
  105. 'message' => __d('content', '"Title Label" need to be at least 3 characters long.'),
  106. ],
  107. ]);
  108. return $validator;
  109. }
  110. /**
  111. * Regenerates snapshot after new content type is created.
  112. *
  113. * @param \Cake\Event\Event $event The event that was triggered
  114. * @param \Cake\Datasource\EntityInterface $entity The entity that was saved
  115. * @param \ArrayObject $options Array of options
  116. * @return void
  117. */
  118. public function afterSave(Event $event, EntityInterface $entity, ArrayObject $options = null)
  119. {
  120. if ($entity->isNew()) {
  121. snapshot();
  122. }
  123. }
  124. }