Commit 3655103f by Vladislav

#19462 Добавление в настройки городов переменных. (исправление на многие ко многим)

1 parent f607ecac
<?php
namespace App\Http\Controllers;
use App\Models\Tokens;
use App\Models\Variable;
use Inertia\Inertia;
class CampaignVariablesController extends Controller
{
protected $rule_variable = 'required|exists:variables,id';
protected $rule_variable_name = 'required|alpha|regex:/[a-zA-z]/';
protected $rule_value = 'required';
function __invoke(Tokens $token, $campaign_id)
{
if ($token->isMain()) {
return back();
}
$campaign = Tokens::where('type', Tokens::MAIN)->get()->first()->campaignsForEnabled()->with('variables')->find($campaign_id);
if (!$campaign) {
return back();
};
switch (request()->method()) {
case 'GET':
return Inertia::render('CampaignVariables/Edit', [
'variables' => Variable::defaultOrderBy()->get(),
'token' => $token,
'campaign' => $campaign,
]);
break;
case 'POST':
if (request('variable') === 'add') {
request()->validate([
'name' => $this->rule_variable_name,
'value' => $this->rule_value,
]);
$variable_new = Variable::create([
'name' => request('name'),
]);
$variable_id = $variable_new->getKey();
$value = request('value');
} else {
request()->validate([
'variable' => $this->rule_variable,
'value' => $this->rule_value,
]);
$variable_id = request('variable');
$value = request('value');
}
$campaign->variables()->syncWithoutDetaching([
$variable_id => [
'value' => $value,
],
]);
return back()->with('success', 'Campaign variable added.');
break;
case 'PUT':
$campaign_variable = $campaign->variables()->find(request('id'));
if (!$campaign_variable) {
return back();
}
if (request()->validate(['name' => $this->rule_variable_name])) {
$campaign_variable->update(request()->only('name'));
}
return back()->with('success', 'Variable name updated.');
break;
case 'PATCH':
$campaign_variable = $campaign->variables()->find(request('id'));
if (!$campaign_variable) {
return back();
}
if (request()->validate(['value' => $this->rule_value])) {
$campaign_variable->update(request()->only('value'));
}
return back()->with('success', 'Campaign variable updated.');
break;
case 'DELETE':
$campaign_variable = $campaign->vars()->find(request('id'));
if (!$campaign_variable) {
return back();
}
$campaign_variable->delete();
return back()->with('success', 'Campaign variable deleted.');
break;
}
return back();
}
}
<?php
namespace App\Http\Controllers;
use App\Models\Campaigns;
use App\Models\Dictionary;
use App\Models\Tokens;
use App\Service\API\API;
use App\Service\Requests\APIRequest;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Redirect;
use Illuminate\Support\Facades\Request;
use Inertia\Inertia;
class CampaignVarsController extends Controller
{
protected $rule_name = 'required|alpha|regex:/[a-zA-z]/';
protected $rule_default_value = 'required';
function __invoke(Tokens $token, $campaign_id)
{
if ($token->isMain()) {
return Redirect::back();
}
$campaign = Tokens::where('type', Tokens::MAIN)->get()->first()->campaignsForEnabled()->with('vars')->find($campaign_id);
if (!$campaign) {
return Redirect::back();
};
switch (request()->method()) {
case 'GET':
return Inertia::render('CampaignVars/Edit', [
'token' => $token,
'campaign' => $campaign,
]);
break;
case 'POST':
request()->validate([
'name' => $this->rule_name,
'default_value' => $this->rule_default_value,
]);
$campaign->vars()->create(request()->only('name', 'default_value'));
return back()->with('success', 'Campaign var added.');
break;
case 'PATCH':
$campaign_var = $campaign->vars()->find(request('id'));
if (!$campaign_var) {
return back();
}
if (
request()->has('name')
&&
request()->validate(['name' => $this->rule_name])
) {
$campaign_var->update(request()->only('name'));
} elseif (request()->has('default_value')
&&
request()->validate(['name' => $this->rule_default_value])
) {
$campaign_var->update(request()->only('default_value'));
}
return back()->with('success', 'Campaign var updated.');
break;
case 'DELETE':
$campaign_var = $campaign->vars()->find(request('id'));
if (!$campaign_var) {
return back();
}
$campaign_var->delete();
return back()->with('success', 'Campaign var deleted.');
break;
}
return back();
}
}
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
namespace App\Models; namespace App\Models;
use App\Models\Pivots\CampaignVariable;
use App\Models\Pivots\DictionaryCampaign; use App\Models\Pivots\DictionaryCampaign;
use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Model;
...@@ -53,7 +54,7 @@ class Campaigns extends Model ...@@ -53,7 +54,7 @@ class Campaigns extends Model
}); });
} else { } else {
$campaign->dictionaries()->detach(); $campaign->dictionaries()->detach();
//$campaign->vars()->delete(); //$campaign->variables()->detach();
} }
} }
}); });
...@@ -62,8 +63,8 @@ class Campaigns extends Model ...@@ -62,8 +63,8 @@ class Campaigns extends Model
static::deleting(function(Campaigns $campaign_delete) static::deleting(function(Campaigns $campaign_delete)
{ {
$campaign_delete->dictionaries()->detach(); $campaign_delete->dictionaries()->detach();
$campaign_delete->variables()->detach();
$campaign_delete->groups()->delete(); $campaign_delete->groups()->delete();
$campaign_delete->vars()->delete();
}); });
*/ */
} }
...@@ -73,9 +74,12 @@ class Campaigns extends Model ...@@ -73,9 +74,12 @@ class Campaigns extends Model
return $this->hasMany(AdGroup::class, 'campaign_id'); return $this->hasMany(AdGroup::class, 'campaign_id');
} }
public function vars() public function variables()
{ {
return $this->hasMany(CampaignVar::class, 'campaign_id'); return $this->belongsToMany(Variable::class, 'campaign_variables', 'campaign_id', 'variable_id')
->using(CampaignVariable::class)
->withPivot(CampaignVariable::getWithPivot())
->withTimestamps();
} }
public function dictionaries() public function dictionaries()
......
<?php <?php
namespace App\Models; namespace App\Models\Pivots;
use Illuminate\Database\Eloquent\Factories\HasFactory; use App\Models\Campaigns;
use Illuminate\Database\Eloquent\Model; use App\Models\Variable;
use Illuminate\Database\Eloquent\Relations\Pivot;
class CampaignVar extends Model class CampaignVariable extends Pivot
{ {
protected $fillable = [ protected $fillable = [
'campaign_id', 'campaign_id',
'name', 'variable_id',
'default_value', 'value',
]; ];
protected $casts = [ protected $casts = [
'campaign_id' => 'int', 'campaign_id' => 'int',
'variable_id' => 'int',
]; ];
static public function getWithPivot()
{
return [
'value',
];
}
public function campaign() public function campaign()
{ {
return $this->belongsTo(Campaigns::class, 'campaign_id'); return $this->belongsTo(Campaigns::class, 'campaign_id');
} }
public function variable()
{
return $this->belongsTo(Variable::class, 'variable_id');
}
} }
<?php
namespace App\Models;
use App\Models\Pivots\CampaignVariable;
use Illuminate\Database\Eloquent\Builder;
class Variable extends Model
{
public function scopeDefaultOrderBy(Builder $query)
{
return $query->orderBy('name');
}
public function campaigns()
{
return $this->belongsToMany(Campaigns::class, 'campaign_variables', 'variable_id', 'campaign_id')
->using(CampaignVariable::class)
->withPivot(CampaignVariable::getWithPivot())
->withTimestamps();
}
}
...@@ -4,7 +4,7 @@ use Illuminate\Database\Migrations\Migration; ...@@ -4,7 +4,7 @@ use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint; use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema; use Illuminate\Support\Facades\Schema;
class CreateCampaignVarsTable extends Migration class CreateVariablesTable extends Migration
{ {
/** /**
* Run the migrations. * Run the migrations.
...@@ -13,15 +13,10 @@ class CreateCampaignVarsTable extends Migration ...@@ -13,15 +13,10 @@ class CreateCampaignVarsTable extends Migration
*/ */
public function up() public function up()
{ {
Schema::create('campaign_vars', function (Blueprint $table) { Schema::create('variables', function (Blueprint $table) {
$table->id(); $table->id();
$table->bigInteger('campaign_id')->unsigned();
$table->string('name', 255); $table->string('name', 255);
$table->string('default_value', 255);
$table->timestamps(); $table->timestamps();
$table->foreign('campaign_id')->references('id')->on('campaigns');
}); });
} }
...@@ -32,6 +27,6 @@ class CreateCampaignVarsTable extends Migration ...@@ -32,6 +27,6 @@ class CreateCampaignVarsTable extends Migration
*/ */
public function down() public function down()
{ {
Schema::dropIfExists('campaign_vars'); Schema::dropIfExists('variables');
} }
} }
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateCampaignVariablesTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('campaign_variables', function (Blueprint $table) {
$table->id();
$table->bigInteger('campaign_id')->unsigned();
$table->bigInteger('variable_id')->unsigned();
$table->string('value', 255);
$table->timestamps();
$table->foreign('campaign_id')->references('id')->on('campaigns');
$table->foreign('variable_id')->references('id')->on('variables');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('campaign_variables');
}
}
<template> <template>
<div> <div>
<h1 class="mb-8 font-bold text-3xl">Campaign vars</h1> <h1 class="mb-8 font-bold text-3xl">Campaign variables</h1>
<div class="mb-6 flex justify-between items-center"> <div class="mb-6 flex justify-between items-center">
<div class="mt-4 flex flex-wrap"> <div class="mt-4 flex flex-wrap">
<text-input v-model="name" <select-input v-model="variable"
class="pr-6"
label="Variable"
>
<option value="add">
[Add new]
</option>
<option v-for="variable in variables" :key="variable.id" :value="variable.id">
{{ variable.name }}
</option>
</select-input>
<text-input v-if="variable === 'add'"
v-model="name"
:error="errors.name"
class="pr-6" class="pr-6"
placeholder="Name" label="New variable name"
> >
</text-input> </text-input>
<text-input v-model="default_value" <text-input v-model="value"
:error="errors.value"
class="pr-6" class="pr-6"
placeholder="Default value" label="Default value"
> >
</text-input> </text-input>
<button class="btn-indigo hover:underline" <button class="btn-indigo hover:underline"
...@@ -28,23 +42,23 @@ ...@@ -28,23 +42,23 @@
<th class="px-6 pt-6 pb-4">Name</th> <th class="px-6 pt-6 pb-4">Name</th>
<th class="px-6 pt-6 pb-4">Default value</th> <th class="px-6 pt-6 pb-4">Default value</th>
</tr> </tr>
<tr v-for="campaign_var in campaign.vars" :key="campaign_var.id" class="hover:bg-gray-100 focus-within:bg-gray-100"> <tr v-for="variable in campaign.variables" :key="variable.id" class="hover:bg-gray-100 focus-within:bg-gray-100">
<td class="px-6 py-4 border-t"> <td class="px-6 py-4 border-t">
<div v-if="!inputs[campaign_var.id] || !inputs[campaign_var.id].name || !inputs[campaign_var.id].name.editable" <div v-if="!inputs[variable.id] || !inputs[variable.id].name || !inputs[variable.id].name.editable"
class="hover:text-indigo-500 focus:text-indigo-500 cursor-pointer" class="hover:text-indigo-500 focus:text-indigo-500 cursor-pointer"
@click="edit(campaign_var.id, 'name', campaign_var.name)" @click="edit(variable.id, 'name', variable.name)"
> >
{{ campaign_var.name }} {{ variable.name }}
</div> </div>
<div v-else class="inline-flex"> <div v-else class="inline-flex">
<text-input v-model="inputs[campaign_var.id].name.val" <text-input v-model="inputs[variable.id].name.val"
class="pr-6" class="pr-6"
> >
</text-input> </text-input>
<button class="btn-indigo hover:underline mr-6" <button class="btn-indigo hover:underline mr-6"
tabindex="-1" tabindex="-1"
type="button" type="button"
@click="save(campaign_var.id, 'name')" @click="save(variable.id, 'name')"
> >
Save Save
...@@ -52,7 +66,7 @@ ...@@ -52,7 +66,7 @@
<button class="btn-indigo hover:underline" <button class="btn-indigo hover:underline"
tabindex="-1" tabindex="-1"
type="button" type="button"
@click="editCancel(campaign_var.id, 'name')" @click="editCancel(variable.id, 'name')"
> >
Cancel Cancel
...@@ -60,21 +74,21 @@ ...@@ -60,21 +74,21 @@
</div> </div>
</td> </td>
<td class="px-6 py-4 border-t"> <td class="px-6 py-4 border-t">
<div v-if="!inputs[campaign_var.id] || !inputs[campaign_var.id].default_value || !inputs[campaign_var.id].default_value.editable" <div v-if="!inputs[variable.id] || !inputs[variable.id].value || !inputs[variable.id].value.editable"
class="hover:text-indigo-500 focus:text-indigo-500 cursor-pointer" class="hover:text-indigo-500 focus:text-indigo-500 cursor-pointer"
@click="edit(campaign_var.id, 'default_value', campaign_var.default_value)" @click="edit(variable.id, 'value', variable.pivot.value)"
> >
{{ campaign_var.default_value }} {{ variable.pivot.value }}
</div> </div>
<div v-else class="inline-flex"> <div v-else class="inline-flex">
<text-input v-model="inputs[campaign_var.id].default_value.val" <text-input v-model="inputs[variable.id].value.val"
class="pr-6" class="pr-6"
> >
</text-input> </text-input>
<button class="btn-indigo hover:underline mr-6" <button class="btn-indigo hover:underline mr-6"
tabindex="-1" tabindex="-1"
type="button" type="button"
@click="save(campaign_var.id, 'default_value')" @click="save(variable.id, 'value')"
> >
Save Save
...@@ -82,7 +96,7 @@ ...@@ -82,7 +96,7 @@
<button class="btn-indigo hover:underline" <button class="btn-indigo hover:underline"
tabindex="-1" tabindex="-1"
type="button" type="button"
@click="editCancel(campaign_var.id, 'default_value')" @click="editCancel(variable.id, 'value')"
> >
Cancel Cancel
...@@ -93,14 +107,14 @@ ...@@ -93,14 +107,14 @@
<button class="px-4 flex items-center" <button class="px-4 flex items-center"
type="button" type="button"
tabindex="-1" tabindex="-1"
@click="destroy(campaign_var.id)" @click="destroy(variable.id)"
> >
<icon name="trash" class="block w-6 h-6 fill-gray-400"/> <icon name="trash" class="block w-6 h-6 fill-gray-400"/>
</button> </button>
</td> </td>
</tr> </tr>
<tr v-if="campaign.vars.length === 0"> <tr v-if="campaign.variables.length === 0">
<td class="border-t px-6 py-4" colspan="4">No campaign vars found.</td> <td class="border-t px-6 py-4" colspan="4">No campaign variables found.</td>
</tr> </tr>
</table> </table>
</div> </div>
...@@ -111,23 +125,27 @@ ...@@ -111,23 +125,27 @@
import Layout from '@/Shared/Layout' import Layout from '@/Shared/Layout'
import Icon from '@/Shared/Icon' import Icon from '@/Shared/Icon'
import TextInput from '@/Shared/TextInput' import TextInput from '@/Shared/TextInput'
import SelectInput from "../../Shared/SelectInput";
export default { export default {
metaInfo: { title: 'Campaign vars' }, metaInfo: { title: 'Campaign variables' },
components: { components: {
Icon, Icon,
TextInput, TextInput,
SelectInput,
}, },
layout: Layout, layout: Layout,
props: { props: {
token: Object, token: Object,
variables: Array,
campaign: Object, campaign: Object,
errors: Object, errors: Object,
}, },
data() { data() {
return { return {
variable: this.variables.length ? null : 'add',
name: '', name: '',
default_value: '', value: '',
inputs: {}, inputs: {},
} }
}, },
...@@ -135,13 +153,14 @@ ...@@ -135,13 +153,14 @@
}, },
methods: { methods: {
add() { add() {
this.$inertia.post(this.route('token.campaign.vars', [this.token.id, this.campaign.id]), { this.$inertia.post(this.route('token.campaign.variables', [this.token.id, this.campaign.id]), {
variable: this.variable,
name: this.name, name: this.name,
default_value: this.default_value, value: this.value,
}); });
this.name = ''; this.name = '';
this.default_value = ''; this.value = '';
}, },
edit(campaign_var_id, key, val) { edit(campaign_var_id, key, val) {
this.inputs = { this.inputs = {
...@@ -168,7 +187,11 @@ ...@@ -168,7 +187,11 @@
}; };
}, },
save(campaign_var_id, key) { save(campaign_var_id, key) {
this.$inertia.patch(this.route('token.campaign.vars', [this.token.id, this.campaign.id]), { const method = key === 'name'
? 'post'
: 'patch';
this.$inertia[method](this.route('token.campaign.variables', [this.token.id, this.campaign.id]), {
id: campaign_var_id, id: campaign_var_id,
[key]: this.inputs[campaign_var_id][key].val, [key]: this.inputs[campaign_var_id][key].val,
}); });
...@@ -176,7 +199,7 @@ ...@@ -176,7 +199,7 @@
}, },
destroy(campaign_var_id) { destroy(campaign_var_id) {
if (confirm('Are you sure you want to delete this campaign var?')) { if (confirm('Are you sure you want to delete this campaign var?')) {
this.$inertia.delete(this.route('token.campaign.vars', [this.token.id, this.campaign.id]), { this.$inertia.delete(this.route('token.campaign.variables', [this.token.id, this.campaign.id]), {
id: campaign_var_id, id: campaign_var_id,
}); });
} }
......
...@@ -63,7 +63,7 @@ ...@@ -63,7 +63,7 @@
<td class="border-t"> <td class="border-t">
<span class="px-6 py-4 flex items-center focus:text-indigo-500"> <span class="px-6 py-4 flex items-center focus:text-indigo-500">
<inertia-link class="hover:text-indigo-500 focus:text-indigo-500" <inertia-link class="hover:text-indigo-500 focus:text-indigo-500"
:href="route('token.campaign.vars', [token.id, campaign.id])" :href="route('token.campaign.variables', [token.id, campaign.id])"
> >
{{ campaign.pivot.name }} {{ campaign.pivot.name }}
</inertia-link> </inertia-link>
...@@ -78,7 +78,7 @@ ...@@ -78,7 +78,7 @@
<td class="border-t"> <td class="border-t">
<inertia-link class="px-4 flex items-center" <inertia-link class="px-4 flex items-center"
tabindex="-1" tabindex="-1"
:href="route('token.campaign.vars', [token.id, campaign.id])" :href="route('token.campaign.variables', [token.id, campaign.id])"
> >
<icon name="book" class="block w-6 h-6 fill-gray-400"/> <icon name="book" class="block w-6 h-6 fill-gray-400"/>
</inertia-link> </inertia-link>
......
<?php <?php
use App\Http\Controllers\Auth\LoginController; use App\Http\Controllers\Auth\LoginController;
use App\Http\Controllers\CampaignVarsController; use App\Http\Controllers\CampaignVariablesController;
use App\Http\Controllers\ContactsController; use App\Http\Controllers\ContactsController;
use App\Http\Controllers\DashboardController; use App\Http\Controllers\DashboardController;
use App\Http\Controllers\ImagesController; use App\Http\Controllers\ImagesController;
...@@ -180,8 +180,8 @@ Route::post('token/campaigns/managed/{token}/{campaign_id}', [TokensController:: ...@@ -180,8 +180,8 @@ Route::post('token/campaigns/managed/{token}/{campaign_id}', [TokensController::
Route::post('token/campaigns/enabled/{token}/{campaign_id}', [TokensController::class, 'enabledCampaign']) Route::post('token/campaigns/enabled/{token}/{campaign_id}', [TokensController::class, 'enabledCampaign'])
->name('token.campaign.enabled') ->name('token.campaign.enabled')
->middleware('auth'); ->middleware('auth');
Route::any('token/campaigns/vars/{token}/{campaign_id}', CampaignVarsController::class) Route::any('token/campaigns/vars/{token}/{campaign_id}', CampaignVariablesController::class)
->name('token.campaign.vars') ->name('token.campaign.variables')
->middleware('auth'); ->middleware('auth');
Route::post('token/city/store/{token}/{city}', [TokensController::class, 'storeCity']) Route::post('token/city/store/{token}/{city}', [TokensController::class, 'storeCity'])
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!