PluginProbe
Fluent Support – Helpdesk & Customer Support Ticket System / 2.4.5
Fluent Support – Helpdesk & Customer Support Ticket System v2.4.5
2.4.5 2.4.0 2.3.2 2.3.1 2.3.0 2.2.1 2.2.0 trunk 1.10.0 1.10.1 1.10.2 1.10.3 1.10.4 1.10.5 1.4.0 1.4.1 1.4.2 1.4.5 1.4.6 1.4.7 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 All 69 releases
fluent-support / app / Models / Product.php

Product.php in Fluent Support – Helpdesk & Customer Support Ticket System 2.4.5, at app/Models/Product.php

126 lines 2.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace FluentSupport\App\Models;
4
5 class Product extends Model
6 {
7 protected $table = 'fs_products';
8
9
10 /**
11 * The attributes that are mass assignable.
12 *
13 * @var array
14 */
15 protected $fillable = [
16 'source_uid',
17 'title',
18 'description',
19 'settings',
20 'source',
21 'created_by'
22 ];
23
24 protected $searchable = ['title', 'description'];
25
26 /**
27 * @param ModelQueryBuilder $query
28 * @param string $search
29 * @return ModelQueryBuilder
30 */
31
32 public function scopeSearchBy($query, $search)
33 {
34 if ($search) {
35 $fields = $this->searchable;
36 $query->where(function ($query) use ($fields, $search) {
37 $query->where(array_shift($fields), 'LIKE', "%$search%");
38
39 foreach ($fields as $field) {
40 $query->orWhere($field, 'LIKE', "$search%");
41 }
42 });
43 }
44
45 return $query;
46 }
47
48 /**
49 * @param $query
50 * @return mixed
51 */
52 public function scopeOrderedByTitle($query)
53 {
54 return $query->orderBy('title', 'ASC');
55 }
56
57 /**
58 * @param string $search
59 * @return array
60 */
61 public function getProducts ( $search )
62 {
63 return [
64 'products' => $this->orderedByTitle()->searchBy($search)->paginate()
65 ];
66 }
67
68 /**
69 * @param int $productId
70 * @return array
71 */
72 public function getProduct ( $productId )
73 {
74 return [
75 'product' => $this->findOrFail($productId)
76 ];
77 }
78
79 /**
80 * @param array $data
81 * @return array
82 */
83 public function createProduct ( $data )
84 {
85 $data = wp_unslash($data);
86 $product = $this->create($data);
87
88 return [
89 'message' => __('Product has been successfully created', 'fluent-support'),
90 'product' => $product
91 ];
92 }
93
94 /**
95 * @param int $productId
96 * @param array $data
97 * @return array
98 */
99 public function updateProduct ( $productId, $data )
100 {
101 $data = wp_unslash($data);
102 $product = $this->findOrFail($productId);
103 $product->fill($data);
104 $product->save();
105
106 return [
107 'message' => __('Product has been updated', 'fluent-support'),
108 'product' => $product
109 ];
110 }
111
112 /**
113 * @param int $productId
114 * @return array
115 */
116 public function deleteProduct ( $productId )
117 {
118 $this->where('id', $productId)->delete();
119
120 return [
121 'message' => __('Product has been deleted', 'fluent-support')
122 ];
123 }
124
125 }
126