Esempio n. 1
0
 public static function parseParams($setting)
 {
     if (@$setting->field_parameters) {
         //OLD CODE: Uses supplied parameters, but must include ALL paramaters
         //$params = $setting->field_parameters;
         //NEW CODE: Merges supplied paramaters, and defaults on parameters that were not supplied
         $default_settings = json_decode(self::getDefaultParams($setting), true);
         $provided_settings = json_decode($setting->field_parameters, true);
         $params = json_encode(\Bootleg\Cms\Utils::array_merge_recursive_distinct($default_settings, $provided_settings));
     } else {
         if (@$setting->default_setting->field_parameters) {
             $params = @$setting->default_setting->field_parameters;
         } else {
             $params = self::getDefaultParams($setting);
         }
     }
     return json_decode($params);
 }
 /**
  * Update the specified resource in storage.
  *
  * @param  int  $id
  * @return Response
  */
 public function anyUpdate($id = false)
 {
     if (!@$id) {
         $input = array_except(\Input::all(), '_method');
         $id = $input['id'];
     }
     if ($id !== false) {
         $input = array_except(\Input::all(), '_method');
         $validation = \Validator::make($input, $this->content->rules);
         if ($validation->passes()) {
             //we need to update the settings too:
             $content = $this->content->find($id);
             //TODO: care with Template settings.
             \Event::fire('content.edit', array($content));
             \Event::fire('content.update', array($content));
             if (@$input['parent_id'] == '#') {
                 $input['parent_id'] = $this->content->getMainRoot();
             }
             $oldPosition = $content->position;
             $content->update($input);
             //position needs looking at too..
             if (isset($input['position']) && $oldPosition != $input['position']) {
                 $siblings = $content->getSiblingsAndSelf();
                 foreach ($siblings as $key => $sibling) {
                     if ($sibling->id == $content->id) {
                         if ($oldPosition > $content->position) {
                             $siblings[$key]->position = $siblings[$key]->position - 0.5;
                         } else {
                             $siblings[$key]->position = $siblings[$key]->position + 0.5;
                         }
                     }
                 }
                 $ordered = $siblings->sortBy(function ($sibling) {
                     return $sibling->position;
                 });
                 $ordered->values();
                 //this will leave us with 2 that are the same position.
                 //we need to loop through and detect which ones to swap.
                 foreach ($ordered as $key => $sibling) {
                     $sibling->position = $key;
                     $sibling->save();
                 }
             }
             //TODO: take another look at a better way of doing this vv ..also VALIDATION!
             //add any settings:
             if (isset($input['setting'])) {
                 foreach ($input['setting'] as $name => $settingGroup) {
                     foreach ($settingGroup as $type => $setGrp) {
                         foreach ($setGrp as $key => $setting) {
                             //we want to delete this setting.
                             $toDel = \Bootleg\Cms\Utils::recursive_array_search('deleted', $setGrp);
                             if (is_array($setGrp) && @$toDel) {
                                 $contentSetting = \Contentsetting::destroy($toDel);
                             } else {
                                 if (is_array($setting) && @$setting['deleted']) {
                                     //THIS IS AN UPLOAD CONTENT ITEM.
                                     //we need to count if there are others.. if so we need to remove this item.
                                     //otherwise we need to set it to blank.
                                     $thisSetting = \ContentSetting::find($key);
                                     $otherSettings = \Contentsetting::where('name', $thisSetting->name)->where('content_id', $content->id)->get();
                                     if (count($otherSettings) > 1) {
                                         $contentSetting = \Contentsetting::destroy($key);
                                     } else {
                                         //we jsut want to set it to blank.
                                         $thisSetting->value = '';
                                         $thisSetting->save();
                                     }
                                 } else {
                                     if ($type != 'Templatesetting') {
                                         //dd($type);
                                         //dd($name . $content->id . $key);
                                         $contentSetting = \Contentsetting::withTrashed()->where('name', '=', $name)->where('content_id', '=', $content->id)->where('id', '=', $key)->first();
                                     }
                                     //if it's not found (even in trashed) then we need to make a new field.
                                     //if it's contentdefault, we need to create it too since it doesn't exist!
                                     if ($type == 'Imagetag') {
                                         if (is_null($contentSetting)) {
                                             $contentSetting = new \Contentsetting();
                                             $contentSetting->name = $name;
                                             $contentSetting->content_id = $content->id;
                                             $contentSetting->field_parameters = '{"image_content_id":"$key"}';
                                             $contentSetting->field_type = "image_tag";
                                             $contentSetting->section = '';
                                         }
                                         $contentSetting->value = $setting;
                                     } else {
                                         if ($type == 'Templatesetting' || is_null($contentSetting)) {
                                             //TODO: Do we want protection in there so there has to be a
                                             //template setting in her for this?
                                             //if we can't find the field, we need to create it from the default:
                                             //dd($name);
                                             $defaultContentSetting = \Templatesetting::find($key);
                                             if (!$defaultContentSetting) {
                                                 $defaultContentSetting = \Templatesetting::where('name', '=', $name)->where('template_id', '=', $content->template_id)->first();
                                             }
                                             $contentSetting = new \Contentsetting();
                                             $contentSetting->name = @$defaultContentSetting->name ? @$defaultContentSetting->name : $name;
                                             $contentSetting->value = $setting;
                                             $contentSetting->content_id = $content->id;
                                             $contentSetting->field_parameters = @$defaultContentSetting->field_parameters;
                                             $contentSetting->field_type = @$defaultContentSetting->field_type;
                                             $contentSetting->section = @$defaultContentSetting->section;
                                         } else {
                                             //otherwise this field exists.. we can overwrite it' settings.
                                             $contentSetting->name = $name;
                                             $contentSetting->value = $setting;
                                             $contentSetting->content_id = $content->id;
                                             $contentSetting->field_type = @$contentSetting->field_type ? $contentSetting->field_type : 'text';
                                         }
                                     }
                                     $contentSetting->save();
                                     $contentSetting->restore();
                                     //TODO: do we always want to restore the deleted field here?
                                 }
                             }
                         }
                     }
                 }
             }
             //TODO: care with Template settings.
             \Event::fire('content.edited', array($content));
             \Event::fire('content.updated', array($content));
             if ($this->content_mode == 'template') {
                 return redirect()->action('\\Bootleg\\Cms\\TemplateController@anyEdit', $id)->with('success', 'Success, saved correctly');
             } else {
                 return redirect()->action('\\Bootleg\\Cms\\ContentsController@anyEdit', $id)->with('success', 'Success, saved correctly');
             }
         }
     } else {
         //TODO:
         $validation = 'no id';
     }
     return redirect()->action('\\Bootleg\\Cms\\ContentsController@anyEdit', $id)->withInput()->withErrors($validation)->with('danger', 'There were validation errors.');
 }