Code Duplication    Length = 136-143 lines in 2 locations

app/Http/Controllers/Admin/ArticleController.php 1 location

@@ 21-156 (lines=136) @@
18
use Illuminate\Support\Facades\Validator;
19
use Illuminate\Support\Facades\Auth;
20
21
class ArticleController extends AdminModuleController {
22
23
    /**
24
     * Contructor
25
     * 
26
     * @param Request $request
27
     */
28
    public function __construct(Request $request) {
29
        parent::__construct();
30
31
        /**
32
         * Add lookup tables
33
         */
34
        $this->middleware('add.lookup.tables:User', ['only' => ['create', 'edit']]);
35
        $this->middleware('add.lookup.tables:ArticleCategory', ['only' => ['create', 'edit']]);
36
        $this->middleware('add.published.at', ['only' => ['store', 'update']]);
37
38
        /**
39
         * Other tables
40
         */
41
        $this->middleware('media.image.select:Image', ['only' => ['create', 'edit']]);
42
    }
43
44
    /**
45
     * Validation rules
46
     * 
47
     * @var array
48
     */
49
    protected $arValidationArray = [
50
        'name' => 'required|max:255|unique:article,name',
51
        'meta_title' => 'max:255',
52
        'meta_description' => 'max:255',
53
        'meta_keywords' => 'max:255',
54
        'text' => 'max:1000000',
55
        'url' => 'required|max:255|unique:article,url',
56
        'author_name' => 'max:255',
57
        'published_at' => 'date',
58
    ];
59
60
    /**
61
     * Associate relationships to other table
62
     * 
63
     * @param object $object
64
     * @param Request $request
65
     */
66
    public function associateRelationships($object, Request $request) {
67
68
        /**
69
         * Validate user ID, if failed set to actual authorized user
70
         */
71
        $validator = Validator::make($request->all(), [
72
                    'user_id' => 'required|integer|min:1|exists:users,id',
73
        ]);
74
        
75
        /**
76
         * Validator fails - save current logged in user
77
         */
78
        if ($validator->fails()) {
79
80
            $object->users()->associate(Auth::user()->id);   
81
        } 
82
        /**
83
         * Validator OK - save it
84
         */
85
        else {
86
87
            $object->users()->associate($request->input('user_id'));
88
        }
89
90
        /**
91
         * Validate image ID, if failed set to default
92
         */
93
        $validator = Validator::make($request->all(), [
94
                    'image_id' => 'integer|min:1|exists:image,id',
95
        ]);
96
        
97
        /**
98
         * Validator fails - nothing to do
99
         */
100
        if ($validator->fails()) {
101
102
        } 
103
        /**
104
         * Validator OK - save it
105
         */
106
        else {
107
108
            $object->images()->associate($request->input('image_id'));
109
        }
110
    }
111
    
112
    /**
113
     * Associate relationships to other table, where ID if object must be present
114
     * 
115
     * @param object $object
116
     * @param Request $request
117
     */
118
    public function associateRelationshipsWithID($object, Request $request) {
119
        
120
        /**
121
         * Validate article category ID, if failed set to default
122
         */
123
        if($request->has('articlecategory_id')){
124
            
125
            $validIDs = [];
126
127
            foreach ($request->input('articlecategory_id') as $articlecategory_id) {
128
129
                $arrayForValidator = ['articlecategory_id' =>  $articlecategory_id];
130
131
                $validator = Validator::make($arrayForValidator, [
132
                            'articlecategory_id' => 'required|integer|min:1|exists:articlecategory,id',
133
                ]);
134
                
135
                /**
136
                 * Validator fails - nothing to do
137
                 */
138
                if ($validator->fails()) {
139
140
                } 
141
                
142
                /**
143
                 * Validator OK - save it
144
                 */ else {
145
146
                    $validIDs[] = $articlecategory_id;
147
                }
148
            }
149
            
150
            /**
151
             * Sync all to pivot
152
             */
153
            $object->articlecategories()->sync($validIDs);
154
        }
155
    }
156
}
157

app/Http/Controllers/Admin/PageController.php 1 location

@@ 21-163 (lines=143) @@
18
use Illuminate\Support\Facades\Validator;
19
use Illuminate\Support\Facades\Auth;
20
21
class PageController extends AdminModuleController {
22
23
    /**
24
     * Constructor
25
     * 
26
     * @var Request $request
27
     */
28
    public function __construct(Request $request) {
29
        parent::__construct();
30
31
        /**
32
         * Add lookup tables
33
         */
34
        $this->middleware('add.lookup.tables:User', ['only' => ['create', 'edit']]);
35
        $this->middleware('add.lookup.tables:PageCategory', ['only' => ['create', 'edit']]);
36
        $this->middleware('add.published.at', ['only' => ['store', 'update']]);
37
38
        /**
39
         * Other tables
40
         */
41
        $this->middleware('media.image.select:Image', ['only' => ['create', 'edit']]);
42
    }
43
44
    /**
45
     * Validation rules
46
     * 
47
     * @var array
48
     */
49
    protected $arValidationArray = [
50
        'name' => 'required|max:255|unique:page,name',
51
        'meta_title' => 'max:255',
52
        'meta_description' => 'max:255',
53
        'meta_keywords' => 'max:255',
54
        'text' => 'max:1000000',
55
        'url' => 'required|max:255|unique:page,url',
56
        'author_name' => 'max:255',
57
        'published_at' => 'date',
58
    ];
59
60
    /**
61
     * Associate relationships to other table
62
     * 
63
     * @param object $object
64
     * @param Request $request
65
     */
66
    public function associateRelationships($object, Request $request) {
67
68
        /**
69
         * Validate user ID, if failed set to actual authorized user
70
         */
71
        $validator = Validator::make($request->all(), [
72
                    'user_id' => 'required|integer|min:1|exists:users,id',
73
        ]);
74
        
75
        /**
76
         * Validator fails - add actual authorized user
77
         */
78
        if ($validator->fails()) {
79
80
            $object->users()->associate(Auth::user()->id);
81
            
82
        } 
83
        
84
        /**
85
         * Validator OK - save it
86
         */
87
        else {
88
89
            $object->users()->associate($request->input('user_id'));
90
        }
91
92
        /**
93
         * Validate image ID, if failed set to default
94
         */
95
        $validator = Validator::make($request->all(), [
96
                    'image_id' => 'integer|min:1|exists:image,id',
97
        ]);
98
99
        /**
100
         * Validator fails - nothing to do
101
         */
102
        if ($validator->fails()) {
103
104
           
105
        } 
106
        
107
        /**
108
         * Validator OK - save it
109
         */
110
        else {
111
112
            $object->images()->associate($request->input('image_id'));
113
        }
114
    }
115
    
116
    /**
117
     * Associate relationships to other table, where ID if object must be present
118
     * 
119
     * @param object $object
120
     * @param Request $request
121
     */
122
    public function associateRelationshipsWithID($object, Request $request) {
123
        
124
        /**
125
         * Validate page category ID, if failed set to default
126
         */
127
        if($request->has('pagecategory_id')){
128
            $validIDs = [];
129
            
130
            /**
131
             * For each category
132
             */
133
            foreach ($request->input('pagecategory_id') as $pagecategory_id) {
134
135
                $arrayForValidator = ['pagecategory_id' =>  $pagecategory_id];
136
137
                $validator = Validator::make($arrayForValidator, [
138
                            'pagecategory_id' => 'required|integer|min:1|exists:pagecategory,id',
139
                ]);
140
141
                /**
142
                 * Validator fails - nothing to do
143
                 */
144
                if ($validator->fails()) {
145
146
                } 
147
                
148
                /**
149
                 * Validator OK - save it
150
                 */
151
                else {
152
153
                    $validIDs[] = $pagecategory_id;
154
                }
155
            }
156
            
157
            /**
158
             * Sync to pivot
159
             */
160
            $object->pagecategories()->sync($validIDs);
161
        }
162
    }
163
}
164