Schlaefer /
Saito
| 1 | <?php |
||||
| 2 | |||||
| 3 | declare(strict_types=1); |
||||
| 4 | |||||
| 5 | /** |
||||
| 6 | * Saito - The Threaded Web Forum |
||||
| 7 | * |
||||
| 8 | * @copyright Copyright (c) the Saito Project Developers |
||||
| 9 | * @link https://github.com/Schlaefer/Saito |
||||
| 10 | * @license http://opensource.org/licenses/MIT |
||||
| 11 | */ |
||||
| 12 | |||||
| 13 | namespace App\Model\Table; |
||||
| 14 | |||||
| 15 | use App\Lib\Model\Table\AppTable; |
||||
| 16 | use App\Model\Entity\Entry; |
||||
| 17 | use Cake\Chronos\Chronos; |
||||
| 18 | use Cake\Event\Event; |
||||
| 19 | use Cake\ORM\RulesChecker; |
||||
| 20 | use Cake\ORM\Rule\IsUnique; |
||||
| 21 | use Cake\Validation\Validator; |
||||
| 22 | |||||
| 23 | /** |
||||
| 24 | * Table storing the drafts for unfinished posting submissions. |
||||
| 25 | * |
||||
| 26 | * Indices: |
||||
| 27 | * - user_id, pid - Main lookup index for retrieving and checking for uniqness. |
||||
| 28 | * Have user_id first to use it for other purposes (find drafts for user). |
||||
| 29 | * - modified - Used for garbage collection deleting outdated drafts. |
||||
| 30 | */ |
||||
| 31 | class DraftsTable extends AppTable |
||||
| 32 | { |
||||
| 33 | /** @var string Creation time after a draft is considered outdated. */ |
||||
| 34 | public const OUTDATED = '-30 days'; |
||||
| 35 | |||||
| 36 | /** |
||||
| 37 | * {@inheritDoc} |
||||
| 38 | */ |
||||
| 39 | public function initialize(array $config) |
||||
| 40 | { |
||||
| 41 | parent::initialize($config); |
||||
| 42 | |||||
| 43 | $this->addBehavior( |
||||
| 44 | 'Cron.Cron', |
||||
| 45 | ['outdatedGc' => ['id' => 'Drafts.outdatedGc', 'due' => '+1 day']] |
||||
| 46 | ); |
||||
| 47 | $this->addBehavior('Timestamp'); |
||||
| 48 | } |
||||
| 49 | |||||
| 50 | /** |
||||
| 51 | * {@inheritDoc} |
||||
| 52 | */ |
||||
| 53 | public function validationDefault(Validator $validator) |
||||
| 54 | { |
||||
| 55 | /// pid |
||||
| 56 | $validator |
||||
| 57 | ->requirePresence('pid', 'create'); |
||||
| 58 | |||||
| 59 | /// subject |
||||
| 60 | $validator |
||||
| 61 | ->allowEmptyString('subject') |
||||
| 62 | ->add( |
||||
| 63 | 'subject', |
||||
| 64 | [ |
||||
| 65 | 'maxLength' => [ |
||||
| 66 | 'rule' => ['maxLength', $this->getConfig('subject_maxlength')], |
||||
| 67 | ], |
||||
| 68 | ] |
||||
| 69 | ); |
||||
| 70 | |||||
| 71 | /// text |
||||
| 72 | $validator->allowEmptyString('text'); |
||||
| 73 | |||||
| 74 | /// user_id |
||||
| 75 | $validator |
||||
| 76 | ->requirePresence('user_id', 'create'); |
||||
| 77 | |||||
| 78 | return $validator; |
||||
| 79 | } |
||||
| 80 | |||||
| 81 | /** |
||||
| 82 | * {@inheritDoc} |
||||
| 83 | */ |
||||
| 84 | public function beforeMarshal(Event $event, \ArrayObject $data, \ArrayObject $options) |
||||
|
0 ignored issues
–
show
The parameter
$options is not used and could be removed.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for parameters that have been defined for a function or method, but which are not used in the method body. Loading history...
|
|||||
| 85 | { |
||||
| 86 | /// Trim whitespace on subject and text |
||||
| 87 | $toTrim = ['subject', 'text']; |
||||
| 88 | foreach ($toTrim as $field) { |
||||
| 89 | if (!empty($data[$field])) { |
||||
| 90 | $data[$field] = trim($data[$field]); |
||||
| 91 | } |
||||
| 92 | } |
||||
| 93 | } |
||||
| 94 | |||||
| 95 | /** |
||||
| 96 | * {@inheritDoc} |
||||
| 97 | */ |
||||
| 98 | public function buildRules(RulesChecker $rules) |
||||
| 99 | { |
||||
| 100 | $rules = parent::buildRules($rules); |
||||
| 101 | |||||
| 102 | $rules->addCreate(new IsUnique(['pid', 'user_id'])); |
||||
| 103 | |||||
| 104 | $rules->add( |
||||
| 105 | function ($entity) { |
||||
| 106 | $validated = false; |
||||
| 107 | $fields = ['subject', 'text']; |
||||
| 108 | foreach ($fields as $field) { |
||||
| 109 | if (!empty($entity->get($field))) { |
||||
| 110 | $validated = true; |
||||
| 111 | break; |
||||
| 112 | } |
||||
| 113 | } |
||||
| 114 | |||||
| 115 | return $validated; |
||||
| 116 | }, |
||||
| 117 | 'checkThatAtLeastOnFieldIsPopulated', |
||||
| 118 | ['errorField' => 'oneNotEmpty', 'message' => 'dummy'] |
||||
| 119 | ); |
||||
| 120 | |||||
| 121 | return $rules; |
||||
| 122 | } |
||||
| 123 | |||||
| 124 | /** |
||||
| 125 | * Deletes a draft which might have been the source for a posting. |
||||
| 126 | * |
||||
| 127 | * @param Entry $entry The posting which might have been created by a draft. |
||||
| 128 | * @return void |
||||
| 129 | */ |
||||
| 130 | public function deleteDraftForPosting(Entry $entry): void |
||||
| 131 | { |
||||
| 132 | $where = ['pid' => $entry->get('pid'), 'user_id' => $entry->get('user_id')]; |
||||
| 133 | $this->deleteAll($where); |
||||
| 134 | } |
||||
| 135 | |||||
| 136 | /** |
||||
| 137 | * Garbage collect outdated drafts. |
||||
| 138 | * |
||||
| 139 | * @return void |
||||
| 140 | */ |
||||
| 141 | public function outdatedGc(): void |
||||
| 142 | { |
||||
| 143 | $this->deleteAll(['modified <' => new Chronos(self::OUTDATED)]); |
||||
| 144 | } |
||||
| 145 | } |
||||
| 146 |
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.