| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | /* | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * This file is part of the Tinyissue package. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * (c) Mohamed Alsharaf <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * For the full copyright and license information, please view the LICENSE | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * file that was distributed with this source code. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | namespace Tinyissue\Model\Traits\Project\Note; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use Illuminate\Database\Eloquent; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | use Tinyissue\Model; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  * CrudTrait is trait class containing the methods for adding/editing/deleting the Project\Note model. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  * @author Mohamed Alsharaf <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  * @property static $this | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  | trait CrudTrait | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |      * Create a new note. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |      * @param array $input | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |      * @return $this | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |     public function createNote(array $input) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |         $this->body       = $input['note_body']; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |         $this->project_id = $this->project->id; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |         $this->created_by = $this->createdBy->id; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |         // Add event on successful save | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |         static::saved(function (Model\Project\Note $note) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |             $this->queueAdd($note, $note->createdBy); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 | 2 |  |         }); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 | 2 |  |         $this->save(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 | 2 |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 | 2 |  |         // Add to user's activity log | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 | 2 |  |         $this->activity()->save(new Model\User\Activity([ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |             'type_id'   => Model\Activity::TYPE_NOTE, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |             'parent_id' => $this->project->id, | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 | 2 |  |             'user_id'   => $this->createdBy->id, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 | 2 |  |         ])); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 | 2 |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 | 2 |  |         return $this; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 | 2 |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |      * Update the note body. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |      * @param string     $body | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |      * @param Model\User $user | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |      * @return Eloquent\Model | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 63 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 64 |  |  |     public function updateBody($body, Model\User $user) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 65 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 66 | 1 |  |         $this->body = $body; | 
            
                                                                        
                            
            
                                    
            
            
                | 67 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 68 | 1 |  |         // Add event on successful save | 
            
                                                                        
                            
            
                                    
            
            
                | 69 |  |  |         static::saved(function (Model\Project\Note $note) use ($user) { | 
            
                                                                        
                            
            
                                    
            
            
                | 70 | 1 |  |             $this->queueUpdate($note, $user); | 
            
                                                                        
                            
            
                                    
            
            
                | 71 |  |  |         }); | 
            
                                                                        
                            
            
                                    
            
            
                | 72 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 73 |  |  |         return $this->save(); | 
            
                                                                        
                            
            
                                    
            
            
                | 74 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |      * Delete a note. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |      * @param Model\User $user | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |      * @return bool|null | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |      * @throws \Exception | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |     public function deleteNote(Model\User $user) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |         $this->activity()->delete(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |         // Add event on successful delete | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |         static::deleted(function (Model\Project\Note $note) use ($user) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |             $this->queueDelete($note, $user); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |         }); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |         return $this->delete(); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |     abstract public function activity(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |     abstract public function save(array $options = []); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |     abstract public function queueDelete(Model\Project\Note $note, Model\User $changeBy); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |     abstract public function queueUpdate(Model\Project\Note $note, Model\User $changeBy); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |     abstract public function queueAdd(Model\Project\Note $note, Model\User $changeBy); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |     abstract public function fill(array $attributes); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 103 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 104 |  |  |  | 
            
                        
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: