File size: 7,390 Bytes
5f32fbd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
<?php

namespace App\Http\Controllers\Api;

use App\Helpers\Classes\Helper;
use App\Http\Controllers\Controller;
use App\Models\Company;
use App\Models\Product;
use Illuminate\Http\Request;
use Illuminate\Support\Str;

class BrandController extends Controller
{
    /**
     * Display brand companies
     *
     * @OA\Get(
     *      path="/api/brandvoice",
     *      operationId="index",
     *      tags={"Brand Voice"},
     *      summary="List of companies",
     *      description="Returns list of companies",
     *      security={{ "passport": {} }},
     *
     *      @OA\Parameter(
     *          name="id",
     *          in="path",
     *          description="ID of company to get. If null, returns all companies",
     *          required=false,
     *
     *          @OA\Schema(type="string")
     *      ),
     *
     *      @OA\Response(
     *          response=200,
     *          description="Successful operation",
     *
     *          @OA\JsonContent(
     *              type="object",
     *          ),
     *      ),
     *
     *      @OA\Response(
     *          response=401,
     *          description="Unauthenticated",
     *      ),
     * )
     */
    public function index($id = null)
    {
        if ($id != null) {
            $list = Company::where('id', $id)->where('user_id', auth()->user()->id)->firstOrFail();
            $products = Product::where('user_id', auth()->user()->id)->where('company_id', $id)->get();
            $list->products = $products;

            return response()->json($list);
        }

        $list = Company::where('user_id', auth()->user()->id)->orderBy('name', 'asc')->get();
        foreach ($list as $company) {
            $products = Product::where('user_id', auth()->user()->id)->where('company_id', $company->id)->get();
            $company->products = $products;
        }

        return response()->json($list);

        // return view('panel.user.companies.list', compact('list'));
    }

    /**
     * Delete brand company
     *
     * @OA\Delete(
     *      path="/api/brandvoice",
     *      operationId="delete",
     *      tags={"Brand Voice"},
     *      summary="Delete a company",
     *      description="Delete a company by id",
     *      security={{ "passport": {} }},
     *
     *      @OA\Parameter(
     *          name="id",
     *          in="path",
     *          description="ID of company to delete",
     *          required=true,
     *
     *          @OA\Schema(type="string")
     *      ),
     *
     *      @OA\Response(
     *          response=200,
     *          description="Successful operation",
     *
     *          @OA\JsonContent(
     *              type="object",
     *          ),
     *      ),
     *
     *      @OA\Response(
     *          response=401,
     *          description="Unauthenticated",
     *      ),
     * )
     */
    public function delete($id = null)
    {
        if (Helper::appIsDemo()) {
            return response()->json(__('This feature is disabled in Demo version.'), 419);
        }
        Product::where('user_id', auth()->user()->id)->where('company_id', $id)->delete();
        Company::where('id', $id)->where('user_id', auth()->user()->id)->delete();

        return response()->json(['message' => __('Deleted Successfully'), 'type' => 'success']);

        // return back()->with(['message' => __('Deleted Successfully'), 'type' => 'success']);
    }

    public function addOrUpdate($id = null)
    {
        if ($id == null) {
            $item = null;
        } else {
            $item = Company::where('id', $id)->where('user_id', auth()->user()->id)->firstOrFail();
        }

        return view('panel.user.companies.form', compact('item'));
    }

    /**
     * Update or create brand companies
     *
     * @OA\Post(
     *      path="/api/brandvoice",
     *      operationId="store",
     *      tags={"Brand Voice"},
     *      summary="Update / Create a company",
     *      description="Update / Create a company",
     *      security={{ "passport": {} }},
     *
     *      @OA\Response(
     *          response=200,
     *          description="Successful operation",
     *
     *          @OA\JsonContent(
     *              type="object",
     *          ),
     *      ),
     *
     *      @OA\Response(
     *          response=401,
     *          description="Unauthenticated",
     *      ),
     * )
     */
    public function store(Request $request)
    {
        if (Helper::appIsDemo()) {
            return response()->json(__('This feature is disabled in Demo version.'), 419);
        }
        if ($request->item_id != 'undefined') {
            $item = Company::where('id', $request->item_id)->firstOrFail();
        } else {
            $item = new Company;
        }

        if ($request->hasFile('c_logo')) {
            $path = 'upload/images/companies/';
            $image = $request->file('c_logo');
            $image_name = Str::random(4) . '-' . Str::slug($request->c_name) . '-logo.' . $image->getClientOriginalExtension();

            $imageTypes = ['jpg', 'jpeg', 'png', 'svg', 'webp'];
            if (! in_array(Str::lower($image->getClientOriginalExtension()), $imageTypes)) {
                $data = [
                    'errors' => ['The file extension must be jpg, jpeg, png, webp or svg.'],
                ];

                return response()->json($data, 419);
            }

            $image->move($path, $image_name);

            $item->logo = $path . $image_name;
        }

        $item->name = $request->c_name;
        $item->website = $request->c_website;
        $item->tagline = $request->c_tagline;
        $item->description = $request->c_description;
        $item->brand_color = $request->c_color;
        $item->industry = $request->c_industry;
        $item->tone_of_voice = $request->tone_of_voice;
        $item->target_audience = $request->target_audience;

        $inputNames = explode(',', $request->input_name);
        $inputFeatures = explode(',', $request->input_features);
        $inputTypes = explode(',', $request->input_type);

        $item->user_id = auth()->user()->id;
        $item->save();

        foreach ($inputNames as $key => $inputName) {
            if ($request->item_id != 'undefined') {
                $product = Product::where('user_id', auth()->user()->id)->where('company_id', $item->id)->where('name', $inputName)->first();
                if ($product == null) {
                    $product = new Product;
                }
            } else {
                $product = new Product;
            }
            $product->name = $inputName;
            $product->key_features = $inputFeatures[$key];
            $product->type = $inputTypes[$key];
            $product->user_id = auth()->user()->id;
            $product->company_id = $item->id;
            $product->save();
        }
        // delete products that are not in the input
        Product::where('user_id', auth()->user()->id)->where('company_id', $item->id)->whereNotIn('name', $inputNames)->delete();

        return response()->json(['message' => __('Saved Successfully'), 'type' => 'success']);
    }

    public function getProducts($id)
    {
        $products = Product::where('user_id', auth()->user()->id)->where('company_id', $id)->get();

        return response()->json($products);
    }
}