Skip to content
Toggle navigation
Projects
Groups
Snippets
Help
Письменов Дмитрий Иванович
/
yourroomads
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Settings
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit 3655103f
authored
May 18, 2021
by
Vladislav
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
#19462 Добавление в настройки городов переменных. (исправление на многие ко многим)
1 parent
f607ecac
Show whitespace changes
Inline
Side-by-side
Showing
10 changed files
with
263 additions
and
143 deletions
app/Http/Controllers/CampaignVariablesController.php
app/Http/Controllers/CampaignVarsController.php
app/Models/Campaigns.php
app/Models/CampaignVar.php → app/Models/Pivots/CampaignVariable.php
app/Models/Variable.php
database/migrations/2021_05_17_101448_create_campaign_vars_table.php → database/migrations/2021_05_17_101048_create_variables_table.php
database/migrations/2021_05_17_101448_create_campaign_variables_table.php
resources/js/Pages/CampaignVars/Edit.vue → resources/js/Pages/CampaignVariables/Edit.vue
resources/js/Pages/Tokens/CitySettings.vue
routes/web.php
app/Http/Controllers/CampaignVariablesController.php
0 → 100644
View file @
3655103
<?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
();
}
}
app/Http/Controllers/CampaignVarsController.php
deleted
100644 → 0
View file @
f607eca
<?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
();
}
}
app/Models/Campaigns.php
View file @
3655103
...
...
@@ -2,6 +2,7 @@
namespace
App\Models
;
use
App\Models\Pivots\CampaignVariable
;
use
App\Models\Pivots\DictionaryCampaign
;
use
Illuminate\Database\Eloquent\Factories\HasFactory
;
use
Illuminate\Database\Eloquent\Model
;
...
...
@@ -53,7 +54,7 @@ class Campaigns extends Model
});
}
else
{
$campaign
->
dictionaries
()
->
detach
();
//$campaign->var
s()->delete
();
//$campaign->var
iables()->detach
();
}
}
});
...
...
@@ -62,8 +63,8 @@ class Campaigns extends Model
static::deleting(function(Campaigns $campaign_delete)
{
$campaign_delete->dictionaries()->detach();
$campaign_delete->variables()->detach();
$campaign_delete->groups()->delete();
$campaign_delete->vars()->delete();
});
*/
}
...
...
@@ -73,9 +74,12 @@ class Campaigns extends Model
return
$this
->
hasMany
(
AdGroup
::
class
,
'campaign_id'
);
}
public
function
vars
()
public
function
var
iable
s
()
{
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
()
...
...
app/Models/
CampaignVar
.php
→
app/Models/
Pivots/CampaignVariable
.php
View file @
3655103
<?php
namespace
App\Models
;
namespace
App\Models
\Pivots
;
use
Illuminate\Database\Eloquent\Factories\HasFactory
;
use
Illuminate\Database\Eloquent\Model
;
use
App\Models\Campaigns
;
use
App\Models\Variable
;
use
Illuminate\Database\Eloquent\Relations\Pivot
;
class
CampaignVar
extends
Model
class
CampaignVar
iable
extends
Pivot
{
protected
$fillable
=
[
'campaign_id'
,
'
name
'
,
'
default_
value'
,
'
variable_id
'
,
'value'
,
];
protected
$casts
=
[
'campaign_id'
=>
'int'
,
'variable_id'
=>
'int'
,
];
static
public
function
getWithPivot
()
{
return
[
'value'
,
];
}
public
function
campaign
()
{
return
$this
->
belongsTo
(
Campaigns
::
class
,
'campaign_id'
);
}
public
function
variable
()
{
return
$this
->
belongsTo
(
Variable
::
class
,
'variable_id'
);
}
}
app/Models/Variable.php
0 → 100644
View file @
3655103
<?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
();
}
}
database/migrations/2021_05_17_101
448_create_campaign_var
s_table.php
→
database/migrations/2021_05_17_101
048_create_variable
s_table.php
View file @
3655103
...
...
@@ -4,7 +4,7 @@ use Illuminate\Database\Migrations\Migration;
use
Illuminate\Database\Schema\Blueprint
;
use
Illuminate\Support\Facades\Schema
;
class
Create
CampaignVar
sTable
extends
Migration
class
Create
Variable
sTable
extends
Migration
{
/**
* Run the migrations.
...
...
@@ -13,15 +13,10 @@ class CreateCampaignVarsTable extends Migration
*/
public
function
up
()
{
Schema
::
create
(
'
campaign_var
s'
,
function
(
Blueprint
$table
)
{
Schema
::
create
(
'
variable
s'
,
function
(
Blueprint
$table
)
{
$table
->
id
();
$table
->
bigInteger
(
'campaign_id'
)
->
unsigned
();
$table
->
string
(
'name'
,
255
);
$table
->
string
(
'default_value'
,
255
);
$table
->
timestamps
();
$table
->
foreign
(
'campaign_id'
)
->
references
(
'id'
)
->
on
(
'campaigns'
);
});
}
...
...
@@ -32,6 +27,6 @@ class CreateCampaignVarsTable extends Migration
*/
public
function
down
()
{
Schema
::
dropIfExists
(
'
campaign_var
s'
);
Schema
::
dropIfExists
(
'
variable
s'
);
}
}
database/migrations/2021_05_17_101448_create_campaign_variables_table.php
0 → 100644
View file @
3655103
<?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'
);
}
}
resources/js/Pages/CampaignVars/Edit.vue
→
resources/js/Pages/CampaignVar
iable
s/Edit.vue
View file @
3655103
<
template
>
<div>
<h1
class=
"mb-8 font-bold text-3xl"
>
Campaign vars
</h1>
<h1
class=
"mb-8 font-bold text-3xl"
>
Campaign var
iable
s
</h1>
<div
class=
"mb-6 flex justify-between items-center"
>
<div
class=
"mt-4 flex flex-wrap"
>
<
text-input
v-model=
"nam
e"
<
select-input
v-model=
"variabl
e"
class=
"pr-6"
placeholder=
"Name"
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"
label=
"New variable name"
>
</text-input>
<text-input
v-model=
"default_value"
<text-input
v-model=
"value"
:error=
"errors.value"
class=
"pr-6"
placeholder
=
"Default value"
label
=
"Default value"
>
</text-input>
<button
class=
"btn-indigo hover:underline"
...
...
@@ -28,23 +42,23 @@
<th
class=
"px-6 pt-6 pb-4"
>
Name
</th>
<th
class=
"px-6 pt-6 pb-4"
>
Default value
</th>
</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"
>
<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"
@
click=
"edit(
campaign_var.id, 'name', campaign_var
.name)"
@
click=
"edit(
variable.id, 'name', variable
.name)"
>
{{
campaign_var
.
name
}}
{{
variable
.
name
}}
</div>
<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"
>
</text-input>
<button
class=
"btn-indigo hover:underline mr-6"
tabindex=
"-1"
type=
"button"
@
click=
"save(
campaign_var
.id, 'name')"
@
click=
"save(
variable
.id, 'name')"
>
Save
...
...
@@ -52,7 +66,7 @@
<button
class=
"btn-indigo hover:underline"
tabindex=
"-1"
type=
"button"
@
click=
"editCancel(
campaign_var
.id, 'name')"
@
click=
"editCancel(
variable
.id, 'name')"
>
Cancel
...
...
@@ -60,21 +74,21 @@
</div>
</td>
<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"
@
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
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"
>
</text-input>
<button
class=
"btn-indigo hover:underline mr-6"
tabindex=
"-1"
type=
"button"
@
click=
"save(
campaign_var.id, 'default_
value')"
@
click=
"save(
variable.id, '
value')"
>
Save
...
...
@@ -82,7 +96,7 @@
<button
class=
"btn-indigo hover:underline"
tabindex=
"-1"
type=
"button"
@
click=
"editCancel(
campaign_var.id, 'default_
value')"
@
click=
"editCancel(
variable.id, '
value')"
>
Cancel
...
...
@@ -93,14 +107,14 @@
<button
class=
"px-4 flex items-center"
type=
"button"
tabindex=
"-1"
@
click=
"destroy(
campaign_var
.id)"
@
click=
"destroy(
variable
.id)"
>
<icon
name=
"trash"
class=
"block w-6 h-6 fill-gray-400"
/>
</button>
</td>
</tr>
<tr
v-if=
"campaign.vars.length === 0"
>
<td
class=
"border-t px-6 py-4"
colspan=
"4"
>
No campaign vars found.
</td>
<tr
v-if=
"campaign.var
iable
s.length === 0"
>
<td
class=
"border-t px-6 py-4"
colspan=
"4"
>
No campaign var
iable
s found.
</td>
</tr>
</table>
</div>
...
...
@@ -111,23 +125,27 @@
import
Layout
from
'@/Shared/Layout'
import
Icon
from
'@/Shared/Icon'
import
TextInput
from
'@/Shared/TextInput'
import
SelectInput
from
"../../Shared/SelectInput"
;
export
default
{
metaInfo
:
{
title
:
'Campaign vars'
},
metaInfo
:
{
title
:
'Campaign var
iable
s'
},
components
:
{
Icon
,
TextInput
,
SelectInput
,
},
layout
:
Layout
,
props
:
{
token
:
Object
,
variables
:
Array
,
campaign
:
Object
,
errors
:
Object
,
},
data
()
{
return
{
variable
:
this
.
variables
.
length
?
null
:
'add'
,
name
:
''
,
default_
value
:
''
,
value
:
''
,
inputs
:
{},
}
},
...
...
@@ -135,13 +153,14 @@
},
methods
:
{
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
,
default_value
:
this
.
default_
value
,
value
:
this
.
value
,
});
this
.
name
=
''
;
this
.
default_
value
=
''
;
this
.
value
=
''
;
},
edit
(
campaign_var_id
,
key
,
val
)
{
this
.
inputs
=
{
...
...
@@ -168,7 +187,11 @@
};
},
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
,
[
key
]:
this
.
inputs
[
campaign_var_id
][
key
].
val
,
});
...
...
@@ -176,7 +199,7 @@
},
destroy
(
campaign_var_id
)
{
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.var
iable
s'
,
[
this
.
token
.
id
,
this
.
campaign
.
id
]),
{
id
:
campaign_var_id
,
});
}
...
...
resources/js/Pages/Tokens/CitySettings.vue
View file @
3655103
...
...
@@ -63,7 +63,7 @@
<td
class=
"border-t"
>
<span
class=
"px-6 py-4 flex items-center 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.var
iable
s', [token.id, campaign.id])"
>
{{
campaign
.
pivot
.
name
}}
</inertia-link>
...
...
@@ -78,7 +78,7 @@
<td
class=
"border-t"
>
<inertia-link
class=
"px-4 flex items-center"
tabindex=
"-1"
:href=
"route('token.campaign.vars', [token.id, campaign.id])"
:href=
"route('token.campaign.var
iable
s', [token.id, campaign.id])"
>
<icon
name=
"book"
class=
"block w-6 h-6 fill-gray-400"
/>
</inertia-link>
...
...
routes/web.php
View file @
3655103
<?php
use
App\Http\Controllers\Auth\LoginController
;
use
App\Http\Controllers\CampaignVarsController
;
use
App\Http\Controllers\CampaignVar
iable
sController
;
use
App\Http\Controllers\ContactsController
;
use
App\Http\Controllers\DashboardController
;
use
App\Http\Controllers\ImagesController
;
...
...
@@ -180,8 +180,8 @@ Route::post('token/campaigns/managed/{token}/{campaign_id}', [TokensController::
Route
::
post
(
'token/campaigns/enabled/{token}/{campaign_id}'
,
[
TokensController
::
class
,
'enabledCampaign'
])
->
name
(
'token.campaign.enabled'
)
->
middleware
(
'auth'
);
Route
::
any
(
'token/campaigns/vars/{token}/{campaign_id}'
,
CampaignVarsController
::
class
)
->
name
(
'token.campaign.vars'
)
Route
::
any
(
'token/campaigns/vars/{token}/{campaign_id}'
,
CampaignVar
iable
sController
::
class
)
->
name
(
'token.campaign.var
iable
s'
)
->
middleware
(
'auth'
);
Route
::
post
(
'token/city/store/{token}/{city}'
,
[
TokensController
::
class
,
'storeCity'
])
...
...
Write
Preview
Markdown
is supported
Attach a file
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to post a comment