Microweber\Utils\Parser::_replace_editable_fields PHP Method

_replace_editable_fields() private method

private _replace_editable_fields ( $layout, $no_cache = false )
    private function _replace_editable_fields($layout, $no_cache = false)
    {
        if ($layout != '') {
            global $mw_replaced_edit_fields_vals;
            global $mw_parser_nest_counter_level;
            global $mw_replaced_edit_fields_vals_inner;
            ++$mw_parser_nest_counter_level;
            $replaced_code_tags = array();
            if ($this->_mw_parser_passed_replaces == null) {
                $this->_mw_parser_passed_replaces = array();
            }
            $mw_found_elems = '';
            $mw_found_elems_arr = array();
            $mw_elements_array = array('orig', $layout);
            $cached = false;
            if (!isset($parser_mem_crc)) {
                $parser_mem_crc = 'parser_' . crc32($layout) . content_id();
                $parser_modules_crc = 'parser_modules' . crc32($layout) . content_id();
            }
            if (isset($this->_mw_parser_passed_replaces[$parser_mem_crc])) {
                return $this->_mw_parser_passed_replaces[$parser_mem_crc];
            }
            if (isset($mw_replaced_edit_fields_vals[$parser_mem_crc])) {
                // return false;
                return $mw_replaced_edit_fields_vals[$parser_mem_crc];
            }
            $script_pattern = "/<pre[^>]*>(.*)<\\/pre>/Uis";
            preg_match_all($script_pattern, $layout, $mw_script_matches);
            if (!empty($mw_script_matches)) {
                foreach ($mw_script_matches[0] as $key => $value) {
                    if ($value != '') {
                        $v1 = crc32($value);
                        $v1 = '<!-- mw_replace_back_this_pre_' . $v1 . ' -->';
                        $layout = str_replace($value, $v1, $layout);
                        if (!isset($replaced_code_tags[$v1])) {
                            $replaced_code_tags[$v1] = $value;
                        }
                    }
                }
            }
            $script_pattern = "/<code[^>]*>(.*)<\\/code>/Uis";
            preg_match_all($script_pattern, $layout, $mw_script_matches);
            if (!empty($mw_script_matches)) {
                foreach ($mw_script_matches[0] as $key => $value) {
                    if ($value != '') {
                        $v1 = crc32($value);
                        $v1 = '<!-- mw_replace_back_this_pre_code_' . $v1 . ' -->';
                        $layout = str_replace($value, $v1, $layout);
                        if (!isset($replaced_code_tags[$v1])) {
                            $replaced_code_tags[$v1] = $value;
                        }
                    }
                }
            }
            $ch = mw_var($parser_mem_crc);
            if ($cached != false) {
                $mw_elements_array = $cached;
            } elseif ($ch != false) {
                $layout = $ch;
            } else {
                require_once __DIR__ . DIRECTORY_SEPARATOR . 'lib' . DIRECTORY_SEPARATOR . 'phpQuery.php';
                $pq = \phpQuery::newDocument($layout);
                $els = $pq['.edit'];
                $is_editable = true;
                foreach ($els as $elem) {
                    // iteration returns PLAIN dom nodes, NOT phpQuery objects
                    $tagName = $elem->tagName;
                    $name = pq($elem)->attr('field');
                    if (strval($name) == '') {
                        $name = pq($elem)->attr('data-field');
                    }
                    $rel = pq($elem)->attr('rel');
                    if ($rel == false) {
                        $rel = pq($elem)->attr('data-rel');
                        if ($rel == false) {
                            $rel = 'page';
                        }
                    }
                    $option_group = pq($elem)->attr('data-option_group');
                    if ($option_group == false) {
                        $option_group = 'editable_region';
                    }
                    $data_id = pq($elem)->attr('data-id');
                    if ($data_id == false) {
                        $data_id = pq($elem)->attr('rel-id');
                    }
                    if ($data_id == false) {
                        $data_id = pq($elem)->attr('rel_id');
                    }
                    if ($data_id == false) {
                        $data_id = pq($elem)->attr('data-rel-id');
                    }
                    $option_mod = pq($elem)->attr('data-module');
                    if ($option_mod == false) {
                        $option_mod = pq($elem)->attr('data-type');
                    }
                    if ($option_mod == false) {
                        $option_mod = pq($elem)->attr('type');
                    }
                    $name = trim($name);
                    $get_global = false;
                    $field = $name;
                    $use_id_as_field = $name;
                    if ($rel == 'global') {
                        $get_global = true;
                    } else {
                        $get_global = false;
                    }
                    $try_inherited = false;
                    if ($rel == 'content') {
                        if (!isset($data_id) or $data_id == false) {
                            $data_id = content_id();
                        }
                        $get_global = false;
                        $data_id = intval($data_id);
                        $data = $this->app->content_manager->get_by_id($data_id);
                    } elseif ($rel == 'page') {
                        if (!isset($data_id) or $data_id == false) {
                            $data_id = PAGE_ID;
                        }
                        if (!isset($data_id) or $data_id == false) {
                            $data_id = content_id();
                        }
                        $data = $this->app->content_manager->get_by_id($data_id);
                        $get_global = false;
                    } elseif ($rel == 'post') {
                        $get_global = false;
                        if (!isset($data_id) or $data_id == false) {
                            $data_id = POST_ID;
                        }
                        if (!isset($data_id) or $data_id == false) {
                            $data_id = PAGE_ID;
                        }
                        $data = $this->app->content_manager->get_by_id($data_id);
                    } elseif ($rel == 'inherit') {
                        $get_global = false;
                        if (!isset($data_id) or $data_id == false) {
                            $data_id = PAGE_ID;
                        }
                        $data_inh_check = $this->app->content_manager->get_by_id($data_id);
                        if (isset($data_inh_check['id']) and isset($data_inh_check['layout_file']) and trim($data_inh_check['layout_file']) != '' and $data_inh_check['layout_file'] != 'inherit') {
                            $inh = $data_inh_check['id'];
                        } else {
                            $inh = $this->app->content_manager->get_inherited_parent($data_id);
                        }
                        if ($inh != false and intval($inh) != 0) {
                            $try_inherited = true;
                            $data_id = $inh;
                            $rel = 'content';
                            $data = $this->app->content_manager->get_by_id($data_id);
                        } else {
                            $rel = 'content';
                            $data = $this->app->content_manager->get_page($data_id);
                        }
                    } elseif ($rel == 'global') {
                        $get_global = 1;
                        $cont_field = false;
                    } elseif (isset($attr['post'])) {
                        $get_global = false;
                        $data = $this->app->content_manager->get_by_id($attr['post']);
                        if ($data == false) {
                            $data = $this->app->content_manager->get_page($attr['post']);
                        }
                    } elseif (isset($attr['category'])) {
                        $get_global = false;
                        $data = $this->app->category_manager->get_by_id($attr['category']);
                    } elseif (isset($attr['global'])) {
                        $get_global = true;
                    }
                    $cf = false;
                    $field_content = false;
                    $orig_rel = $rel;
                    $this->_current_parser_rel = $rel;
                    if (!empty($this->filter)) {
                        foreach ($this->filter as $filter) {
                            if (isset($data)) {
                                $new_data = call_user_func($filter, $data, $elem);
                                if (is_array($new_data) and !empty($new_data)) {
                                    $data = array_merge($data, $new_data);
                                }
                            }
                        }
                    }
                    if (isset($data[$field])) {
                        if (isset($data[$field])) {
                            $field_content = $data[$field];
                        }
                    } else {
                        if ($rel == 'page') {
                            $rel = 'content';
                        }
                        if ($rel == 'post') {
                            $rel = 'content';
                        }
                        $cont_field = false;
                        if (isset($data_id) and $data_id != 0 and trim($data_id) != '' and trim($field) != '') {
                            $cont_field = $this->app->content_manager->edit_field("rel_type={$rel}&field={$field}&rel_id={$data_id}");
                            if ($cont_field == false and $try_inherited == true) {
                                $inh = $this->app->content_manager->get_inherited_parent($data_id);
                                if ($inh != false and intval($inh) != 0 and $inh != $data_id) {
                                    $data_id = $inh;
                                    $cont_field2 = $this->app->content_manager->edit_field("rel_type={$rel}&field={$field}&rel_id={$inh}");
                                    if ($cont_field2 != false) {
                                        $rel = 'content';
                                        $data = $this->app->content_manager->get_by_id($inh);
                                        $cont_field = $cont_field2;
                                    }
                                }
                            }
                        } else {
                            if (isset($data_id) and trim($data_id) != '' and $field_content == false and isset($rel) and isset($field) and trim($field) != '') {
                                $cont_field = $this->app->content_manager->edit_field("rel_type={$rel}&field={$field}&rel_id={$data_id}");
                                if ($cont_field != false) {
                                    $field_content = $cont_field;
                                }
                            } else {
                                $field_content = $cont_field = $this->app->content_manager->edit_field("rel_type={$rel}&field={$field}");
                            }
                        }
                        if ($cont_field != false) {
                            $field_content = $cont_field;
                        }
                        $mw_replaced_edit_fields_vals[$parser_mem_crc] = $field_content;
                    }
                    if ($rel == 'global') {
                        $field_content = false;
                        $get_global = 1;
                    }
                    //   $filter
                    $this->_current_parser_rel = $rel;
                    $no_edit = false;
                    if ($field_content == false) {
                        if ($get_global == true) {
                            if (isset($data_id)) {
                                $cont_field = $this->app->content_manager->edit_field("rel_type={$rel}&field={$field}&rel_id={$data_id}");
                            }
                            if (isset($cont_field) and !empty($cont_field)) {
                                $cont_field = $this->app->content_manager->edit_field("rel_type={$rel}&field={$field}");
                            }
                            if ($cont_field == false) {
                                if ($option_mod != false) {
                                    $field_content = $this->app->content_manager->edit_field("rel_type={$option_group}&field={$field}");
                                } else {
                                    $field_content = $this->app->content_manager->edit_field("rel_type={$option_group}&field={$field}");
                                }
                            } else {
                                $field_content = $cont_field;
                            }
                        } else {
                            if ($use_id_as_field != false) {
                                if (isset($data[$use_id_as_field])) {
                                    $field_content = $data[$use_id_as_field];
                                }
                            }
                            if ($field_content == false) {
                                if (isset($data_id) and $data_id != false) {
                                    $cont_field = $this->app->content_manager->edit_field("rel_type={$orig_rel}&field={$field}&rel_id={$data_id}");
                                } else {
                                    $cont_field = $this->app->content_manager->edit_field("rel_type={$orig_rel}&field={$field}&rel_id=" . PAGE_ID);
                                }
                            }
                            if (isset($data[$field])) {
                                $field_content = $data[$field];
                            } else {
                                if (isset($cont_field) and $cont_field != false) {
                                    $field_content = $cont_field;
                                }
                            }
                        }
                        if ($field == 'content' and template_var('content') != false) {
                            $field_content = template_var('content');
                            template_var('content', false);
                            $no_edit = template_var('no_edit');
                        }
                        if (isset($data_id) and trim($data_id) != '' and $field_content == false and isset($rel) and isset($field) and trim($field) != '') {
                            $cont_field = $this->app->content_manager->edit_field("rel_type={$rel}&field={$field}&rel_id={$data_id}");
                            if ($cont_field != false) {
                                $field_content = $cont_field;
                            }
                        } elseif ($field_content == false and isset($rel) and isset($field) and trim($field) != '') {
                            $cont_field = $this->app->content_manager->edit_field("rel_type={$rel}&field={$field}");
                            if ($cont_field != false) {
                                $field_content = $cont_field;
                            }
                        }
                    }
                    if ($field_content != false and $field_content != '' and is_string($field_content)) {
                        $parser_mem_crc2 = 'parser_field_content_' . $field . $rel . $data_id . crc32($field_content);
                        $ch2 = mw_var($parser_mem_crc);
                        if ($ch2 == false) {
                            $this->_mw_parser_passed_hashes[] = $parser_mem_crc2;
                            $this->_mw_parser_passed_hashes_rel[$rel][] = $parser_mem_crc2;
                            if (!isset($mw_replaced_edit_fields_vals[$parser_mem_crc2]) and $field_content != false and $field_content != '') {
                                $mw_replaced_edit_fields_vals[$parser_mem_crc2] = $ch2;
                                $parser_mem_crc3 = 'mw_replace_back_this_editable_' . $parser_mem_crc2 . '';
                                $mw_found_elems = ',' . $parser_mem_crc2;
                                $mw_found_elems_arr[$parser_mem_crc2] = $field_content;
                                // $rep = pq($elem)->html();
                                $rep = pq($elem)->html();
                                if ($no_edit != false or isset($data) and isset($data['no_edit']) and $data['no_edit'] != false) {
                                    $is_editable = false;
                                    if ($is_editable === false) {
                                        pq($elem)->removeClass('edit');
                                    } else {
                                    }
                                    $is_editable = 1;
                                }
                                $mw_replaced_edit_fields_vals_inner[$parser_mem_crc3] = array('s' => $rep, 'r' => $field_content, 'rel' => $rel);
                            }
                        }
                        mw_var($parser_mem_crc2, 1);
                    }
                }
                $layout = $pq->htmlOuter();
                $pq->__destruct();
                $pq = null;
                unset($pq);
                if (!empty($mw_replaced_edit_fields_vals_inner)) {
                    $reps_arr = array();
                    $reps_arr2 = array();
                    foreach ($mw_replaced_edit_fields_vals_inner as $k => $v) {
                        $repc = 1;
                        if (isset($v['s'])) {
                            $reps_arr[] = $v['s'];
                            $reps_arr2[] = $v['r'];
                            $layout = $this->_str_replace_first($v['s'], $v['r'], $layout, $repc);
                            unset($mw_replaced_edit_fields_vals_inner[$k]);
                        }
                    }
                    // $layout = str_replace($reps_arr, $reps_arr2, $layout,$repc);
                }
                mw_var($parser_mem_crc, $layout);
                if ($mw_found_elems != '') {
                    $mw_elements_array['new'] = $layout;
                    $mw_elements_array['to_replace'] = $mw_found_elems;
                    $mw_elements_array['elems'] = $mw_found_elems_arr;
                } else {
                    $mw_elements_array['new'] = $layout;
                }
            }
        }
        if (isset($mw_elements_array) and !empty($mw_elements_array)) {
            if (isset($mw_elements_array['elems']) and isset($mw_elements_array['to_replace']) and isset($mw_elements_array['new'])) {
                $modified_layout = $mw_elements_array['new'];
                $reps = $mw_elements_array['elems'];
                $c = 1;
                foreach ($reps as $elk => $value) {
                    $elk_crc = crc32($elk);
                    $global_holder_hash = 'replaced' . $elk_crc;
                    if (!isset($mw_replaced_edit_fields_vals[$global_holder_hash])) {
                        $this->_mw_parser_passed_replaces[] = $elk_crc;
                        $mw_replaced_edit_fields_vals[$global_holder_hash] = $modified_layout;
                        if ($value != '') {
                            $val_rep = $value;
                            $val_rep = $this->_replace_editable_fields($val_rep, true);
                            $rep = 'mw_replace_back_this_editable_' . $elk . '';
                            $ct = 1;
                            //   $modified_layout = str_replace($rep, $val_rep, $modified_layout,$ct);
                            $modified_layout = $this->_str_replace_first($rep, $val_rep, $modified_layout);
                        }
                    } else {
                        $rep = 'mw_replace_back_this_editable_' . $elk . '';
                        $modified_layout = $this->_str_replace_first($rep, $value, $modified_layout);
                        // $modified_layout = str_replace($rep, $value, $modified_layout);
                    }
                }
                $layout = $modified_layout;
                $mw_replaced_edit_fields_vals[$parser_mem_crc] = $layout;
            }
            if (!empty($replaced_code_tags)) {
                foreach ($replaced_code_tags as $key => $value) {
                    if ($value != '') {
                        $layout = str_replace($key, $value, $layout);
                    }
                    unset($replaced_code_tags[$key]);
                }
            }
            if ($no_cache == false) {
                //    $this->app->cache_manager->save($layout, $parser_mem_crc, 'content_fields/global/parser');
            }
        }
        $this->_mw_parser_passed_replaces[$parser_mem_crc] = $layout;
        $mw_replaced_edit_fields_vals[$parser_mem_crc] = $layout;
        return $layout;
    }