sutho100
2/27/2019 - 12:14 AM

Adore Beauty get json helper

<?php

namespace Templater\Adorebeauty;

use \App;
use \DB;
use \Longtail\Contracts\TemplateRepositoryInterface;

class Helper
{

    protected $template_repo;

    public function __construct(TemplateRepositoryInterface $template)
    {
        $this->template_repo = $template;
    }

    public function getExcerptDataByKeyword($keyword)
    {
        $best_keyword = '';

        $keyword_rule = 'best';

        preg_match_all("#" . $keyword_rule . "#i", $keyword, $matches);
        if (isset($matches[0][0]) && strlen($matches[0][0]) > 0) {
            $best_keyword = 'y';
        }

        return $best_keyword;
    }

    public function extendBaseTemplate($data, $section)
    {
        $data['webroot_uri'] = 'https://www.adorebeauty.com.au';
        if ('production' !== app()->environment()) {

            $data['iserp_root_uri'] = 'https://adore.ecstaging.com/buy';
            $data['webroot_uri'] = 'https://adore.ecstaging.com';
        }

        $html_link_attributes = '';
        if ($data['client_type'] == 'SEM') {
            $html_link_attributes = ' rel="nofollow" ';
        }
        $data['html_link_attributes'] = $html_link_attributes;

        // Default titles
        $data['meta_title'] = 'Adore Beauty | Australia\'s Online Beauty Store';
        $data['meta_keywords'] = '';
        $data['meta_description'] = 'Over 3,000 skincare, haircare &amp; cosmetic products by brands include Dermalogica, Clarins, SK-II, Aesop &amp; more. Shop online with FREE delivery!';

        $data['results_to_display'] = 0;
        $first_description = "";

        // $community_menu_json = 'https://forum.adorebeauty.com.au/top.json';
        // $raw_json = Cache::remember(
        //   'templater_resource_raw_json', 720,
        //   function() use($community_menu_json) {
        //     // use curl to download the uri
        //     $ch = curl_init();
        //     curl_setopt($ch, CURLOPT_URL, $community_menu_json);
        //     curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        //     curl_setopt($ch, CURLOPT_TIMEOUT, 3); // 3 second timeout
        //     $output = curl_exec($ch);

        //     if ($output === false) {
        //         \Log::warning("Failed to retrieve the Community option from Adorebeauty mega menu, {$community_menu_json}, error = " . curl_error($ch));
        //     }

        //     curl_close($ch);
        //     return ($output);
        //   }
        // );
        // $menu_community_option = json_decode($raw_json);
        // $data['community_mega_menu_option'] = $menu_community_option;

        if (isset($data['iserp']['id']) && (int) $data['iserp']['id'] > 0) {
            $data['add_noindex_to_templates'] = $this->template_repo->shouldAddMetaTagForNoIndex($data);
            // Meta data
            if (isset($data['iserp']['full_keyword_match_score']) && $data['iserp']['full_keyword_match_score'] > 5) {
                $data['meta_title'] = ucwords($data['iserp']['iserp_name']) . ' - ' . $data['iserp']['full_keyword_match_score'] . ' products | Adore Beauty';
            } else {
                $data['meta_title'] = ucwords($data['iserp']['iserp_name']) . ' | Adore Beauty';
            }

            $data['meta_keywords'] = $data['meta_keywords'] . ',' . $data['iserp']['iserp_name'];
            if (isset($data['first_result_description']) && !empty($data['first_result_description'])) {
                $data['meta_description'] = stripslashes($data['iserp']['iserp_name']) . ',' . $data['first_result_description'];
            }

            if (isset($data['iserp']['iserps_category_meta_data'])) {
                if (isset($data['iserp']['iserps_category_meta_data']['attributes']) &&
                    strlen($data['iserp']['iserps_category_meta_data']['attributes']) > 0) {
                    $category_meta_data = json_decode($data['iserp']['iserps_category_meta_data']['attributes']);

                    if (isset($category_meta_data->sitewide_banner) && strlen($category_meta_data->sitewide_banner) > 0) {
                        $data['sitewide_banner'] = $category_meta_data->sitewide_banner;
                    }
                }

            }
            //get the subcategories
            $child_query = DB::table('client_18_categories')->select('category_name', 'category_url', 'category_id')->where('parent_cat', $data['iserp']['category_id'])->get();

            if (count($child_query) > 0) {
                $data['iserp']['child_categories'] = $child_query;

                if (isset($data['client_data']) && isset($data['client_data']->client_id)) {
                    foreach ($child_query as $child_cat) {
                        $second_child = DB::table("client_" . $data['client_data']->client_id . "_categories")
                            ->select('category_name', 'category_url', 'category_id')
                            ->where('parent_cat', $child_cat->category_id)->get();

                        if (!empty($second_child)) {
                            $data['iserp']['second_child_categories'][$child_cat->category_id] = $second_child;
                        }
                    }
                }
            }

            $category_details = DB::table('client_18_categories')->select('category_name', 'category_url', 'category_level', 'parent_cat')->where('category_reference_id', $data['iserp']['category_reference_id'])->first();

            if ($category_details->category_level == 2) {
                $category_level_one = DB::table('client_18_categories')->select('category_name', 'category_slug', 'category_url', 'category_level', 'parent_cat')->where('category_id', $category_details->parent_cat)->first();
                $data['iserp']['level_one_category_info'] = $category_level_one;

                if (isset($category_level_one) && $category_level_one->category_level == 1) {
                    $category_level_zero = DB::table('client_18_categories')->select('category_name', 'category_slug', 'category_url', 'category_level', 'parent_cat')->where('category_id', $category_level_one->parent_cat)->first();
                    $data['iserp']['level_zero_category_info'] = $category_level_zero;
                }
            }
            if ($category_details->category_level == 1) {
                $category_level_zero = DB::table('client_18_categories')->select('category_name', 'category_slug', 'category_url', 'category_level', 'parent_cat')->where('category_id', $category_details->parent_cat)->first();
                $data['iserp']['level_one_category_info'] = $category_level_zero;
            }

            $data['best_keyword'] = $this->getExcerptDataByKeyword($data['iserp']['iserp_name']);

            $brand_query = DB::table('client_18_categories_meta')->select('category_meta_value')->where('category_meta_key', 'all_brands')->first();
            if (count(get_object_vars($brand_query)) > 0) {
                $data['all_brands'] = json_decode($brand_query->category_meta_value);
            }
        }
        return $data;

    }
}