PluginProbe
Fluent Support – Helpdesk & Customer Support Ticket System / trunk
Fluent Support – Helpdesk & Customer Support Ticket System vtrunk
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 1.5.5 All 68 releases
fluent-support / app / Models / Product.php

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

133 lines 3.0 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 * Local scope to filter products by search/query string
28 * @param ModelQueryBuilder $query
29 * @param string $search
30 * @return ModelQueryBuilder
31 */
32
33 public function scopeSearchBy($query, $search)
34 {
35 if ($search) {
36 $fields = $this->searchable;
37 $query->where(function ($query) use ($fields, $search) {
38 $query->where(array_shift($fields), 'LIKE', "%$search%");
39
40 foreach ($fields as $field) {
41 $query->orWhere($field, 'LIKE', "$search%");
42 }
43 });
44 }
45
46 return $query;
47 }
48
49 /**
50 * Local scope to order products alphabetically by title
51 * @param $query
52 * @return mixed
53 */
54 public function scopeOrderedByTitle($query)
55 {
56 return $query->orderBy('title', 'ASC');
57 }
58
59 /**
60 * This `getProducts` method will return the list of products
61 * @param string $search
62 * @return array
63 */
64 public function getProducts ( $search )
65 {
66 return [
67 'products' => $this->orderedByTitle()->searchBy($search)->paginate()
68 ];
69 }
70
71 /**
72 * This `getProduct` method will get product by id and return
73 * @param int $productId
74 * @return array
75 */
76 public function getProduct ( $productId )
77 {
78 return [
79 'product' => $this->findOrFail($productId)
80 ];
81 }
82
83 /**
84 * This `createProduct` method will create a new product
85 * @param array $data
86 * @return array
87 */
88 public function createProduct ( $data )
89 {
90 $data = wp_unslash($data);
91 $product = $this->create($data);
92
93 return [
94 'message' => __('Product has been successfully created', 'fluent-support'),
95 'product' => $product
96 ];
97 }
98
99 /**
100 * This `updateProduct` method will update an exiting product by id
101 * @param int $productId
102 * @param array $data
103 * @return array
104 */
105 public function updateProduct ( $productId, $data )
106 {
107 $data = wp_unslash($data);
108 $product = $this->findOrFail($productId);
109 $product->fill($data);
110 $product->save();
111
112 return [
113 'message' => __('Product has been updated', 'fluent-support'),
114 'product' => $product
115 ];
116 }
117
118 /**
119 * This `deleteProduct` method will delete an exiting product by id
120 * @param int $productId
121 * @return array
122 */
123 public function deleteProduct ( $productId )
124 {
125 $this->where('id', $productId)->delete();
126
127 return [
128 'message' => __('Product has been deleted', 'fluent-support')
129 ];
130 }
131
132 }
133