Skip to content

Commit

Permalink
fix meta
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniele Rapisarda committed Jan 29, 2018
1 parent 5160c51 commit 90fb2a0
Show file tree
Hide file tree
Showing 6 changed files with 136 additions and 62 deletions.
3 changes: 3 additions & 0 deletions src/Controllers/FieldsDescriptorController.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,9 @@ public function content_types(Request $r, $content_type)
// 'show' => (int)(\Request::input('show')[$k] == 'on'),
'sort' => (int)\Request::input('sort')[$k],
];
if(!$meta){
unset ($field['in_meta']);
}
if (\Request::input('id')[$k]&& \Request::input('id')[$k]>0) {
$field['id'] = \Request::input('id')[$k];
// if($names[$k]=='_default'){
Expand Down
18 changes: 9 additions & 9 deletions src/Controllers/Rapyd/EditController.php
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@ public function index(Request $r, $content_type, $lang_id = 0)
// сохранение description для модели
if ($desc_table_exists && isset($_POST[$desc_table])) {
$lang_records = \Request::all()[$desc_table];
// dd($lang_records);
foreach ($lang_records as $post_lang_id => $post_lang) {
$lang_records[$post_lang_id]['content_id'] = $row->id;
$lang_records[$post_lang_id]['lang_id'] = $post_lang_id;
Expand Down Expand Up @@ -344,16 +345,15 @@ public function getValue($item,$content_value,$modelName,$type){
}

private function save_meta_data($content,$data){
// $data->update();
// dd($data);
// dd($content);
if($content->is_system==0){
foreach (MetaSettings::$columns as $col){
\DB::table($content->table)->where('id','=',$data->id)->update([$col=>$data[$col]]);
if(MetaSettings::NeedMeta($content->id)) {
if (!MetaSettings::is_description_table($content->table)) {
dd('not description');
if ($content->is_system == 0) {
foreach (MetaSettings::$columns as $col) {
\DB::table($content->table)->where('id', '=', $data->id)->update([$col => $data[$col]]);
}
}
}
}
// dump($data['meta_title']);
// dd('save data meta',$data);
// \DB::table($content->table)->where()
}
}
124 changes: 87 additions & 37 deletions src/CustomClasses/MetaSettings.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ public static function NeedMeta($content_type_id){
/**
* Add meta columns to table
*
* @param $table_name Content_Type
* @param $content Content_Type
* @return object data
*/
public function AddMetaFieldsTo($content){
try{
Expand All @@ -50,10 +51,11 @@ public function AddMetaFieldsTo($content){
if($settings){
return $settings;
}else{
$settings =[];
/// create new obj
$settings = new Meta;
foreach (MetaSettings::$columns as $col){
$settings[$col] = "";

$settings->$col = "";
}
return $settings;
}
Expand Down Expand Up @@ -81,22 +83,32 @@ public function AddMetaFieldsTo($content){
public static function is_description_table($table_name){
$table_name_desc = $table_name."_description";
if(\Schema::hasTable($table_name_desc)){
// dd(true);
return true;
}else{
// dd(false);
return false;
}
}


/**
* Save settings
* @param array $data
* @param $table_name
*/
public function add(array $data,$table_name){
$add = Meta::where(['content_type_id'=>$data['content_type_id']])->first();
if(empty($add)){
$add = new Meta();
$add->content_type_id = $data['content_type_id'];
$add->save();
}
$langs = Languages::pluck('id');
$temp = \DB::table(MetaSettings::$table_name_meta.'_description')->where('content_id','=',$add->id)->get();

if($this->is_description_table($table_name)){ /// for multi lang
$langs = Languages::pluck('id');
$this->checkNULL(true,$data,$langs);
$temp = \DB::table(MetaSettings::$table_name_meta.'_description')->where('content_id','=',$add->id)->get();
if(empty(count($temp))){/// insert
foreach ($langs as $lang_id){
\DB::table(MetaSettings::$table_name_meta.'_description')
Expand All @@ -109,16 +121,43 @@ public function add(array $data,$table_name){
}
}
}else{// one lang
if(empty(count($temp))){// insert
$temp = \DB::table(MetaSettings::$table_name_meta.'_description')->where('content_id','=',$add->id)->first();
$this->checkNULL(false,$data);
if(empty($temp)){// insert
\DB::table(MetaSettings::$table_name_meta.'_description')
->insert(['content_id'=>$add->id,'lang_id'=>'"-1"','meta_title'=>$data['meta_title'][0],'meta_description'=>$data['meta_description'][0]]);
->insert(['content_id'=>$add->id,'lang_id'=>'-1','meta_title'=>$data['meta_title'],'meta_description'=>$data['meta_description']]);
}else{// update
\DB::table(MetaSettings::$table_name_meta.'_description')->where([['content_id','=',$add->id],['lang_id','=','-1']])
->update(['meta_title'=>$data['meta_title'][0],'meta_description'=>$data['meta_description'][0]]);
\DB::table(MetaSettings::$table_name_meta.'_description')->where([['content_id','=',$temp->id],['lang_id','=','-1']])
->update(['meta_title'=>$data['meta_title'],'meta_description'=>$data['meta_description']]);
}
}
}

/**
* Check if null for insert udpate settings
*
* @param $description true or false
* @param $data varible with data
* @param null $langs if $description true need langs
*/
private function checkNULL($description,$data,$langs = null){
if($description){
foreach (MetaSettings::$columns as $col){
foreach ($langs as $lang){
if(is_null($data[$col][$lang])){
$data[$col][$lang] = "";
}
}
}
}else{
foreach (MetaSettings::$columns as $col){
if(is_null($data[$langs][$col])){
$data[$langs][$col] = "";
}
}
}
}

private function columns_create($table_name){
if(\Schema::hasTable($table_name)){
foreach (MetaSettings::$columns as $column){
Expand All @@ -136,18 +175,32 @@ private function columns_create($table_name){
}

private function columns_delete($table_name){
if(\Schema::hasTable($table_name)){
foreach (MetaSettings::$columns as $column){
if(\Schema::hasColumn($table_name, $column)){
\Schema::table($table_name, function (Blueprint $table,$column)
{
$table->dropColumn($column);
});
if(MetaSettings::is_description_table($table_name)){
if (\Schema::hasTable($table_name."_description")) {
foreach (MetaSettings::$columns as $column) {
if (\Schema::hasColumn($table_name."_description", $column)) {
\Schema::table($table_name."_description", function (Blueprint $table)use($column) {
$table->dropColumn($column);
});
}
}
return true;
} else {
throw new \Exception("not table " . $table_name."_description");
}
}else {
if (\Schema::hasTable($table_name)) {
foreach (MetaSettings::$columns as $column) {
if (\Schema::hasColumn($table_name, $column)) {
\Schema::table($table_name, function (Blueprint $table) use ($column){
$table->dropColumn($column);
});
}
}
return true;
} else {
throw new \Exception("not table " . $table_name);
}
return true;
}else{
throw new \Exception("not table ".$table_name);
}
}

Expand All @@ -161,40 +214,37 @@ private function columns_delete($table_name){
*/
public static function GenerateMeta($content,$all_data=null,$lang_id=null,$data_description=null){
if(!MetaSettings::NeedMeta($content->id)){
if(!is_null($data_description)){
return $data_description;
}else{
return $all_data;
}
}

$settings = MetaSettings::get_settings_meta($content->id);
if($lang_id){/// for description
if(!MetaSettings::is_description_table($content->table)){
// dd('return row in if');
if(!empty($data_description)){
return $data_description;
}else{
return $all_data;
}
$description_settings = $settings->getDescription();
}
// dump($lang_id);
$settings = MetaSettings::get_settings_meta($content->id);
if(MetaSettings::is_description_table($content->table)){
/// for multi language
if(is_null($lang_id)){
return $all_data;
}
$description_settings = $settings->getDescription($content->table);
$a = "meta_title";
foreach(MetaSettings::$columns as $col){
if(is_null($data_description[$col])) {
if(empty($data_description[$col])) {
$data_description[$col] = MetaSettings::replace($col,$description_settings[$lang_id]->$col,$data_description);
}
}
// dd('return all');
return $data_description;
}else{/// for data
if(MetaSettings::is_description_table($content->table)){
return $all_data;
}
// dd('for data');
$data_settings = $settings->getDescription();
foreach(MetaSettings::$columns as $col){
if(is_null($all_data[$col])) {
if(empty($all_data[$col])) {
$all_data[$col] = MetaSettings::replace($col,$data_settings[$col],$all_data);
// dump($col,$all_data[$col]);
}
}
// dd($all_data['meta_title']);
return $all_data;
}

Expand Down
41 changes: 29 additions & 12 deletions src/Models/Meta/Meta.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,25 +15,42 @@ class Meta extends \Eloquent
public $table = "crud_settings";

public function description(){
// return \DB::table('crud_settings_description')->where('content_id','=',$content_id);
return $this->hasMany('Wbe\Crud\Models\Meta\MetaDescription','content_id');
}

public function getDescription(){
public function getDescription($table = null){
$data = \DB::table('crud_settings_description')->where('content_id','=',$this->id)->get();
if(count($data)>1) {
$temp = [];
$lang = Languages::pluck('id');
foreach ($lang as $lang) {
$temp[$lang] = $data->where('lang_id', '=', $lang)->first();
if(MetaSettings::is_description_table($table)) {
$langs = Languages::pluck('id');
if($data->count()==0){
$temp = [];
foreach ($langs as $lang){
$temp[$lang] = [];
foreach (MetaSettings::$columns as $col){
$temp[$lang][$col]="";
}
}
return $temp;
}else{
$temp = [];
foreach ($langs as $lang) {
$temp[$lang] = $data->where('lang_id', '=', $lang)->first();
}
return $temp;
}
return $temp;
}
else{
$temp = (array)$data[0];
unset($temp['content_id']);
unset($temp['lang_id']);
return $temp;
if($data->count()==0){
$settings =[];
foreach (MetaSettings::$columns as $col){
$settings[$col] = "";
}
return $settings;
}else{
return $data[0];
}


}
}
}
2 changes: 2 additions & 0 deletions src/views/crud/fd_fields_row.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,12 @@
<input type="checkbox" {{--id="form_show"--}} class="form-control checkbox_autofill" {{ $f->form_show ? 'checked' : '' }}>
<input type="hidden" name="form_show[]">
</td>
@if($meta)
<td>
<input type="checkbox" id="{{$f->name}}" class="form-control checkbox_autofill in_meta" {{ $f->in_meta ? 'checked' : '' }}>
<input type="hidden" id="{{$f->name}}" name="in_meta[]">
</td>
@endif
{{--<td><input type="text" class="form-control" name="form_attributes[]" value="{{ $f->form_attributes }}"></td>--}}
{{--<td>--}}
{{--<input type="checkbox" --}}{{--id="show"--}}{{-- class="form-control checkbox_autofill" {{ $f->show ? 'checked' : '' }}>--}}
Expand Down
10 changes: 6 additions & 4 deletions src/views/crud/fieldsdescriptor.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -385,7 +385,9 @@ function rel_left_column_change(elem) {
<th>grid filter</th>
<th>grid custom display</th>
<th>form show</th>
@if($meta)
<th>in meta</th>
@endif
<th>Дії</th>
</tr>
</thead>
Expand Down Expand Up @@ -479,8 +481,8 @@ function rel_left_column_change(elem) {
<div class="tab-content">
@foreach($langs as $lang)
<div id="{{$lang->code}}" class="tab-pane fade @if($loop->index==0){{' in active'}}@endif">
@foreach($meta->getDescription($meta->id)[$lang->id] as $item_key =>$item_value)
@if(in_array($item_key,$meta_fild))
@foreach($meta->getDescription($content->table)[$lang->id] as $item_key=>$item_value)
@if(in_array($item_key,$meta_fild))
<label for="{{$item_key}}" class="col-md-12"> {{$item_key}}
<input type="text" class="form-control col-md-12" name="{{$item_key}}[{{$lang->id}}]" value="{{$item_value}}">
</label>
Expand All @@ -490,10 +492,10 @@ function rel_left_column_change(elem) {
@endforeach
</div>
@else
@foreach($meta->getDescription($meta->id) as $fild_name =>$value)
@foreach($meta->getDescription($content->table) as $fild_name =>$value)
@if(in_array($fild_name,$meta_fild))
<label for="{{$fild_name}}" class="col-md-12"> {{$fild_name}}
<input type="text" class="form-control col-md-12" name="{{$fild_name}}[]" value="{{$value}}">
<input type="text" class="form-control col-md-12" name="{{$fild_name}}" value="{{$value}}">
</label>
@endif
@endforeach
Expand Down

0 comments on commit 90fb2a0

Please sign in to comment.