Microweber\Providers\Content\ContentManagerHelpers::save_from_live_edit PHP Метод

save_from_live_edit() публичный Метод

public save_from_live_edit ( $post_data )
    public function save_from_live_edit($post_data)
    {
        $is_module = false;
        $is_admin = $this->app->user_manager->is_admin();
        if ($post_data) {
            if (isset($post_data['json_obj'])) {
                $obj = json_decode($post_data['json_obj'], true);
                $post_data = $obj;
            }
            if (isset($post_data['mw_preview_only'])) {
                $is_no_save = true;
                unset($post_data['mw_preview_only']);
            }
            $is_no_save = false;
            $is_draft = false;
            if (isset($post_data['is_draft'])) {
                unset($post_data['is_draft']);
                $is_draft = 1;
            }
            $the_field_data_all = $post_data;
        } else {
            return array('error' => 'no POST?');
        }
        $ustr2 = $this->app->url_manager->string(1, 1);
        if (isset($ustr2) and trim($ustr2) == 'favicon.ico') {
            return false;
        }
        $ref_page = $ref_page_url = false;
        if (isset($_SERVER['HTTP_REFERER'])) {
            $ref_page = $ref_page_url = $_SERVER['HTTP_REFERER'];
        }
        if (isset($post_data['id']) and intval($post_data['id']) > 0) {
            $page_id = intval($post_data['id']);
        } elseif ($ref_page != '') {
            //removing hash from url
            if (strpos($ref_page_url, '#')) {
                $ref_page = $ref_page_url = substr($ref_page_url, 0, strpos($ref_page_url, '#'));
            }
            $ref_page2 = $ref_page = $this->get_by_url($ref_page_url);
            if ($ref_page2 == false) {
                $ustr = $this->app->url_manager->string(1);
                if ($this->app->modules->is_installed($ustr)) {
                    $ref_page = false;
                }
            } else {
                $ref_page = $ref_page2;
            }
            if (isset($ustr) and trim($ustr) == 'favicon.ico') {
                return false;
            } elseif ($ustr2 == '' or $ustr2 == '/') {
                $ref_page = $this->app->content_manager->homepage();
                if ($ref_page_url) {
                    $page_url_ref = $this->app->url_manager->param('content_id', $ref_page_url);
                    if ($page_url_ref !== false) {
                        if ($page_url_ref == 0) {
                            return false;
                        }
                    }
                }
            }
            if ($ref_page == false) {
                $guess_page_data = new \Microweber\Controllers\DefaultController();
                // $guess_page_data =  new  $this->app->controller($this->app);
                $ref_page_url = strtok($ref_page_url, '?');
                $guess_page_data->page_url = $ref_page_url;
                $guess_page_data->return_data = true;
                $guess_page_data->create_new_page = true;
                $pd = $guess_page_data->index();
                $ustr = $this->app->url_manager->string(1);
                $is_module = false;
                $pd['url'] = $ustr;
                if (isset($pd['active_site_template']) and $pd['active_site_template'] == template_name()) {
                    $pd['active_site_template'] = '';
                }
                if ($this->app->modules->is_installed($ustr)) {
                    $is_module = true;
                    $save_page['layout_file'] = 'clean.php';
                    $save_page['subtype'] = 'module';
                    $hp_id = $this->app->content_manager->homepage();
                    if (isset($hp_id['id'])) {
                        $page_id = $hp_id['id'];
                    } else {
                        $page_id = 1;
                    }
                    $is_module = 1;
                    $save_page = false;
                }
                if ($is_admin == true and is_array($pd) and $is_module == false) {
                    $save_page = $pd;
                    if (!isset($_GET['mw_quick_edit'])) {
                        if (isset($ref_page_url) and $ref_page_url != false) {
                            $save_page['url'] = $ref_page_url;
                        } else {
                            $save_page['url'] = $this->app->url_manager->string(1);
                        }
                        $title = str_replace('%20', ' ', $this->app->url_manager->string(1));
                        if ($title == 'editor_tools/wysiwyg' or $title == 'api/module' or $title == 'admin/view:content') {
                            return false;
                        }
                        $save_page['title'] = $title;
                        if ($save_page['url'] == '' or $save_page['url'] == '/' or $save_page['url'] == $this->app->url_manager->site()) {
                            $save_page['url'] = 'home';
                            $home_exists = $this->app->content_manager->homepage();
                            if ($home_exists == false) {
                                $save_page['is_home'] = 1;
                            }
                        }
                    }
                    if ($save_page['title'] == '') {
                        $save_page['title'] = 'Home';
                    }
                    if (!isset($save_page['is_active'])) {
                        $save_page['is_active'] = 1;
                    }
                    if (isset($save_page['content_type']) and $save_page['content_type'] == 'page') {
                        if (!isset($save_page['subtype'])) {
                            $save_page['subtype'] = 'static';
                            if (!isset($save_page['layout_file']) or $save_page['layout_file'] == false) {
                                $save_page['layout_file'] = 'inherit';
                            }
                        }
                    }
                    if ($save_page != false) {
                        $page_id = $this->app->content_manager->save_content_admin($save_page);
                    }
                }
            } else {
                $page_id = $ref_page['id'];
                $ref_page['custom_fields'] = $this->app->content_manager->custom_fields($page_id, false);
            }
        }
        $author_id = user_id();
        if ($is_admin == false and $page_id != 0 and $author_id != 0) {
            $page_data_to_check_author = $this->get_by_id($page_id);
            if (!isset($page_data_to_check_author['created_by']) or $page_data_to_check_author['created_by'] != $author_id) {
                return array('error' => 'You dont have permission to edit this content');
            }
        } elseif ($is_admin == false) {
            return array('error' => 'Not logged in as admin to use ' . __FUNCTION__);
        }
        $save_as_draft = false;
        if (isset($post_data['save_draft'])) {
            $save_as_draft = true;
            unset($post_data['save_draft']);
        }
        $json_print = array();
        foreach ($the_field_data_all as $the_field_data) {
            $save_global = false;
            $save_layout = false;
            if (isset($page_id) and $page_id != 0 and !empty($the_field_data)) {
                $save_global = false;
                $content_id = $page_id;
                $url = $this->app->url_manager->string(true);
                $some_mods = array();
                if (isset($the_field_data) and is_array($the_field_data) and isset($the_field_data['attributes'])) {
                    if ($the_field_data['html'] != '') {
                        $field = false;
                        if (isset($the_field_data['attributes']['field'])) {
                            $field = trim($the_field_data['attributes']['field']);
                        }
                        if (isset($the_field_data['attributes']['data-field'])) {
                            $field = $the_field_data['attributes']['field'] = trim($the_field_data['attributes']['data-field']);
                        }
                        if ($field != false) {
                            $page_element_id = $field;
                        }
                        if (!isset($the_field_data['attributes']['rel'])) {
                            $the_field_data['attributes']['rel_type'] = 'content';
                        } else {
                            $the_field_data['attributes']['rel_type'] = $the_field_data['attributes']['rel'];
                        }
                        if (isset($the_field_data['attributes']['rel-id'])) {
                            $content_id = $the_field_data['attributes']['rel-id'];
                        } elseif (isset($the_field_data['attributes']['rel_id'])) {
                            $content_id = $the_field_data['attributes']['rel_id'];
                        } elseif (isset($the_field_data['attributes']['data-rel-id'])) {
                            $content_id = $the_field_data['attributes']['data-rel-id'];
                        } elseif (isset($the_field_data['attributes']['data-rel_id'])) {
                            $content_id = $the_field_data['attributes']['data-rel_id'];
                        }
                        $save_global = false;
                        if (isset($the_field_data['attributes']['rel_type']) and (trim($the_field_data['attributes']['rel_type']) == 'global' or trim($the_field_data['attributes']['rel_type'])) == 'module') {
                            $save_global = true;
                        } else {
                            $save_global = false;
                        }
                        if (isset($the_field_data['attributes']['rel_type']) and trim($the_field_data['attributes']['rel_type']) == 'layout') {
                            $save_global = false;
                            $save_layout = true;
                        } else {
                            $save_layout = false;
                        }
                        if (isset($the_field_data['attributes']['rel'])) {
                            $the_field_data['attributes']['rel_type'] = $the_field_data['attributes']['rel'];
                        }
                        if (!isset($the_field_data['attributes']['data-id'])) {
                            $the_field_data['attributes']['data-id'] = $content_id;
                        }
                        $save_global = 1;
                        if (isset($the_field_data['attributes']['rel_type']) and isset($the_field_data['attributes']['data-id'])) {
                            $rel_ch = trim($the_field_data['attributes']['rel_type']);
                            switch ($rel_ch) {
                                case 'content':
                                    $save_global = false;
                                    $save_layout = false;
                                    $content_id_for_con_field = $content_id = $the_field_data['attributes']['data-id'];
                                    break;
                                case 'page':
                                case 'post':
                                    $save_global = false;
                                    $save_layout = false;
                                    $content_id_for_con_field = $content_id = $page_id;
                                    break;
                                default:
                                    break;
                            }
                        }
                        $inh = false;
                        if (isset($the_field_data['attributes']['rel_type']) and $the_field_data['attributes']['rel_type'] == 'inherit') {
                            $save_global = false;
                            $save_layout = false;
                            $content_id = $page_id;
                            $inh = $this->app->content_manager->get_inherited_parent($page_id);
                            if ($inh != false) {
                                $content_id_for_con_field = $content_id = $inh;
                            }
                        } elseif (isset($the_field_data['attributes']['rel_type']) and $the_field_data['attributes']['rel_type'] == 'page') {
                            $save_global = false;
                            $save_layout = false;
                            $content_id = $page_id;
                            $check_if_page = $this->get_by_id($content_id);
                            if (is_array($check_if_page) and isset($check_if_page['content_type']) and isset($check_if_page['parent']) and $check_if_page['content_type'] != '' and intval($check_if_page['parent']) != 0 and $check_if_page['content_type'] != 'page') {
                                $inh = $check_if_page['parent'];
                                if ($inh != false) {
                                    $content_id_for_con_field = $content_id = $inh;
                                }
                            }
                        }
                        $save_layout = false;
                        if (isset($post_data['id'])) {
                            $content_id_for_con_field = $post_data['id'];
                        } elseif ($inh == false and !isset($content_id_for_con_field)) {
                            if (is_array($ref_page) and isset($ref_page['parent']) and isset($ref_page['content_type']) and $ref_page['content_type'] == 'post') {
                                $content_id_for_con_field = intval($ref_page['parent']);
                            } else {
                                $content_id_for_con_field = intval($ref_page['id']);
                            }
                        }
                        $html_to_save = $the_field_data['html'];
                        $html_to_save = $content = $this->app->parser->make_tags($html_to_save);
                        if ($save_global == false and $save_layout == false) {
                            if ($content_id) {
                                $for_histroy = $ref_page;
                                $old = false;
                                $field123 = str_ireplace('custom_field_', '', $field);
                                if (stristr($field, 'custom_field_')) {
                                    $old = $for_histroy['custom_fields'][$field123];
                                } else {
                                    if (isset($for_histroy['custom_fields'][$field123])) {
                                        $old = $for_histroy['custom_fields'][$field123];
                                    } elseif (isset($for_histroy[$field])) {
                                        $old = $for_histroy[$field];
                                    }
                                }
                                $history_to_save = array();
                                $history_to_save['table'] = 'content';
                                $history_to_save['id'] = $content_id;
                                $history_to_save['value'] = $old;
                                $history_to_save['field'] = $field;
                                $cont_field = array();
                                $cont_field['rel_type'] = 'content';
                                $cont_field['rel_id'] = $content_id_for_con_field;
                                $cont_field['value'] = $html_to_save;
                                $cont_field['field'] = $field;
                                if ($is_draft != false) {
                                    $cont_id = $content_id_for_con_field;
                                    $cont_field['is_draft'] = 1;
                                    $cont_field['rel_type'] = $rel_ch;
                                    $cont_field['url'] = $url;
                                    $to_save_draft = true;
                                    if (isset($cont_field['value'])) {
                                        $draftmd5 = md5($cont_field['value']);
                                        $draftmd5_last = $this->app->user_manager->session_get('content_draft_save_md5');
                                        if ($draftmd5_last == $draftmd5) {
                                            $to_save_draft = false;
                                        } else {
                                            $this->app->user_manager->session_set('content_draft_save_md5', $draftmd5);
                                        }
                                    }
                                    if ($to_save_draft) {
                                        $cont_field1 = $this->app->content_manager->save_content_field($cont_field);
                                    }
                                } else {
                                    if ($field != 'content') {
                                        $cont_field1 = $this->app->content_manager->save_content_field($cont_field);
                                    } else {
                                        $cont_table_save = array();
                                    }
                                }
                                $this->app->event_manager->trigger('mw.content.save_edit', $cont_field);
                                $to_save = array();
                                $to_save['id'] = $content_id;
                                $is_native_fld = $this->app->database_manager->get_fields('content');
                                if (in_array($field, $is_native_fld)) {
                                    $to_save[$field] = $html_to_save;
                                }
                                if ($is_no_save != true and $is_draft == false) {
                                    $json_print[] = $to_save;
                                    $saved = $this->app->content_manager->save_content_admin($to_save);
                                }
                            } elseif (isset($category_id)) {
                                echo __FILE__ . __LINE__ . ' category is not implemented ... not ready yet';
                            }
                        } else {
                            $cont_field = array();
                            $cont_field['rel_type'] = $the_field_data['attributes']['rel_type'];
                            $cont_field['rel_id'] = 0;
                            if (isset($the_field_data['attributes']['rel-id'])) {
                                $cont_field['rel_id'] = $the_field_data['attributes']['rel-id'];
                            } elseif (isset($the_field_data['attributes']['rel_id'])) {
                                $cont_field['rel_id'] = $the_field_data['attributes']['rel_id'];
                            } elseif (isset($the_field_data['attributes']['data-rel-id'])) {
                                $cont_field['rel_id'] = $the_field_data['attributes']['data-rel-id'];
                            } elseif ($cont_field['rel_type'] != 'global' and isset($the_field_data['attributes']['content-id'])) {
                                $cont_field['rel_id'] = $the_field_data['attributes']['content-id'];
                            } elseif ($cont_field['rel_type'] != 'global' and isset($the_field_data['attributes']['data-id'])) {
                                $cont_field['rel_id'] = $the_field_data['attributes']['data-id'];
                            } elseif (isset($the_field_data['attributes']['data-rel_id'])) {
                                $cont_field['rel_id'] = $the_field_data['attributes']['data-rel_id'];
                            }
                            $cont_field['value'] = $this->app->parser->make_tags($html_to_save);
                            if ((!isset($the_field_data['attributes']['field']) or $the_field_data['attributes']['field'] == '') and isset($the_field_data['attributes']['data-field'])) {
                                $the_field_data['attributes']['field'] = $the_field_data['attributes']['data-field'];
                            }
                            $cont_field['field'] = $the_field_data['attributes']['field'];
                            if ($is_draft != false) {
                                $cont_field['is_draft'] = 1;
                                $cont_field['url'] = $this->app->url_manager->string(true);
                                $cont_field_new = $this->app->content_manager->save_content_field($cont_field);
                            } else {
                                $cont_field_new = $this->app->content_manager->save_content_field($cont_field);
                            }
                            if ($save_global == true and $save_layout == false) {
                                $json_print[] = $cont_field;
                                $history_to_save = array();
                                $history_to_save['table'] = 'global';
                                $history_to_save['value'] = $cont_field['value'];
                                $history_to_save['field'] = $field;
                                $history_to_save['page_element_id'] = $page_element_id;
                            }
                        }
                    }
                }
            }
        }
        if (isset($opts_saved)) {
            $this->app->cache_manager->delete('options');
        }
        return $json_print;
    }