123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149 |
- <?php
- namespace App\Http\Controllers\API;
- use App\Http\Controllers\Controller;
- use App\Http\Requests\API\NamingRule\AutonameRequest;
- use App\Http\Requests\API\NamingRule\CombinationSettingRequest;
- use App\Http\Requests\API\NamingRule\CreateOrUpdateRequest;
- use App\Http\Resources\API\NamingRuleCombinationResource;
- use App\Http\Resources\API\NamingRuleResource;
- use App\Models\CustomField;
- use App\Models\NamingRule;
- use App\Models\Project;
- use Illuminate\Http\Request;
- use Illuminate\Support\Facades\Auth;
- use Illuminate\Support\Facades\DB;
- class NameRuleController extends Controller
- {
- /**
- * Display a listing of the resource.
- */
- public function index(Request $request)
- {
- $namingRules = NamingRule::allowed()->with(['company'])->filter($request->all())->paginate();
- return NamingRuleResource::collection($namingRules);
- }
- /**
- * Store a newly created resource in storage.
- */
- public function store(CreateOrUpdateRequest $request)
- {
- $formData = $request->only(['name', 'status', 'company_id', 'global']);
- if (! Auth::user()->super_admin) {
- $formData['company_id'] = Auth::user()->company_id;
- unset($formData['global']);
- }
- NamingRule::create($formData);
- return $this->created();
- }
- /**
- * Display the specified resource.
- */
- public function show(string $id)
- {
- $namingRule = NamingRule::query()->allowed()->findOrFail($id);
- return new NamingRuleResource($namingRule);
- }
- /**
- * Update the specified resource in storage.
- */
- public function update(CreateOrUpdateRequest $request, string $id)
- {
- $namingRule = NamingRule::query()->allowed()->findOrFail($id);
- $namingRule->fill($request->only([
- 'name', 'status', 'global'
- ]));
- $namingRule->save();
- return $this->noContent();
- }
- /**
- * Remove the specified resource from storage.
- */
- public function destroy(string $id)
- {
- DB::transaction(function () use ($id) {
- $namerule = NamingRule::query()->allowed()->findOrFail($id);
- $namerule->delete();
- $customfield = CustomField::query()->where('group', $id);
- if (!empty($customfield)) {
- $customfield->delete();
- }
- });
- return $this->noContent();
- }
- public function enabled()
- {
- $namingRules = NamingRule::where('status', 1)->where(function ($query) {
- return $query->where("company_id", Auth::user()->company_id)->orWhere([
- 'company_id' => 0,
- 'global' => 1
- ]);
- })->select(['id', 'name'])->get();
- return $this->success([
- 'data' => $namingRules
- ]);
- }
- public function combinationSetting(CombinationSettingRequest $request, string $id)
- {
- $namingRule = NamingRule::query()->allowed()->findOrFail($id);
- $namingRule->combination_rules = $request->all();
- $namingRule->save();
- return $this->noContent();
- }
- public function combinationShow(string $id){
- $nameRule=NamingRule::query()->allowed()->findOrFail($id);
- return new NamingRuleCombinationResource($nameRule);
- }
- public function autoname(AutonameRequest $request, string $id)
- {
- $namingRule = NamingRule::query()->allowed()->findOrFail($id);
- if (! $namingRule->combination_rules) {
- return $this->badRequest("Please set naming combination rules");
- }
- $code = match ($request->code_type) {
- "project" => Project::query()->find($request->code_id)?->code,
- default => ""
- };
- if (! $code) {
- return $this->badRequest("The supported type or selected item does not exist");
- }
- $name = $code;
- foreach ($namingRule->combination_rules as $rule) {
- if (! isset($request->fields[$rule['field']]) || !$request->fields[$rule['field']]) {
- return $this->badRequest(sprintf("%s field is required", $rule['field']));
- }
- $name .= $rule['link'] . $request->fields[$rule['field']];
- }
- return $this->success([
- 'data' => $name
- ]);
- }
- }
|