<?php

/*
 * Vinnorokom Facebook Quiz App V.2.1.3
 *
 * All table connections
 * Fetching data
 * Only DB class using for fetching data from default database
 * Check config file to see default database.
 * Dont change anything if you dont know about these :)
 * Extent Eloquent
 *  */

class Common extends Eloquent
{
    public function __construct()
    {
    }

    // Get All media files
    public function getAllMediaFiles()
    {
        $data = DB::table('media_files')->take(60)->orderBy('file_id', 'desc')->get();

        return $data;
    }

    // Get all media files for page with pagination number per page 15
    public function getAllMediaFilesPagination()
    {
        $data = DB::table('media_files')->orderBy('file_id', 'desc')->paginate(15);

        return $data;
    }

    // Delete media files by file name
    public function deleteFileByFileName($file_name)
    {
        $file_name = trim($file_name);
        DB::table('media_files')->where('file_name', $file_name)->delete();

        return true;
    }

    // Add footer image link
    public function addFooterImageLink($data)
    {
        $dataArray = array();
        $dataArray['footer_image_link'] = $data['image_link'];
        $chk = DB::table('user_page_setting')->where('page_id', $data['link_id'])->first();
        if (!$chk->footer_image) {
            return 2;
        } else {
            DB::table('user_page_setting')->where('page_id', $data['link_id'])->update($dataArray);

            return true;
        }
    }

    // Update slider image
    public function updateSlider($sliderData)
    {
        $max_id = DB::table('user_page_setting')->max('page_id');
        DB::table('user_page_setting')->where('page_id', $max_id)->update($sliderData);

        return true;
    }

    // Get user list
    public function getUsersByGroupId($group_id)
    {
        $data = array();
        if (isset($group_id) && $group_id > 0) {
            $group_id = DB::table('user_quiz_result')->distinct()->select('fid')->where('group_id', $group_id)->get();

            $user_ids = array();
            foreach ($group_id as $g) {
                $user_ids[] = $g->fid;
            }
            if (!empty($group_id)) {
                $qry = DB::table('users')->where('user_level', 0)->whereIn('fbid', $user_ids)->orderBy('id', 'desc');
                $data['data'] = $qry->paginate(10);
                $data['count_user'] = $qry->count();
            } else {
                //$data = DB::table('users')->where('user_level', 0)->whereIn('fbid', array(0))->orderBy('id', 'desc')->paginate(10);

                $qry = DB::table('users')->where('user_level', 0)->whereIn('fbid', array(0))->orderBy('id', 'desc');
                $data['data'] = $qry->paginate(10);
                $data['count_user'] = $qry->count();
            }
        } else {
            //$data = DB::table('users')->where('user_level', 0)->orderBy('id', 'desc')->paginate(10);
            $qry = DB::table('users')->where('user_level', 0)->orderBy('id', 'desc');
            $data['data'] = $qry->paginate(10);
            $data['count_user'] = $qry->count();
        }

        return $data;
    }

    /*
     * Export user data information to csv file
     * @return data array
     *
     */

    public function userExport($group_id)
    {
        if (isset($group_id) && $group_id > 0) {
            $group_id = DB::table('user_quiz_result')->distinct()->select('fid')->where('group_id', $group_id)->get();
            $user_ids = array();
            foreach ($group_id as $g) {
                $user_ids[] = $g->fid;
            }
            $data = DB::table('users')->where('user_level', 0)->whereIn('fbid', $user_ids)->orderBy('id', 'desc')->paginate(10);
        } else {
            $data = DB::table('users')->where('user_level', 0)->orderBy('id', 'desc')->paginate(10);
        }

        return $data;
    }

    // Menu generate
    public function loadParentPages()
    {
        $data = DB::table('user_pages')->where('added_to', 1)->get();

        return $data;
    }

    // Get page deails by page id
    public function loadPageById($page_id)
    {
        $data = DB::table('user_pages')->where('page_id', $page_id)->first();

        return $data;
    }

    // Get App Page list
    public function allPageList()
    {
        $data = DB::table('user_pages')->orderBy('page_id', 'desc')->paginate(20);

        return $data;
    }

    // All published pages
    public function allActivePages()
    {
        $data = DB::table('user_pages')->where('publish', 1)->orderBy('page_id', 'desc')->get();

        return $data;
    }

    // All published pages
    public static function allActiveChildPages($page_id)
    {
        $data = DB::table('user_pages')->select('page_name', 'slug', 'page_id')->where('parent_id', $page_id)->where('page_type', 1)->where('published', 1)->orderBy('page_id', 'desc')->get();

        return $data;
        Util::printArr($data);
        exit;
    }

    //  Get All navbar pages
    public static function allActiveNavbarPages()
    {
        $data = DB::table('user_pages')->select('page_name', 'slug', 'page_id')->where('added_to', 1)->where('published', 1)->orderBy('page_id', 'desc')->get();

        return $data;
    }

    // Change page pulish / unpublish
    public function chagePageStatus($page_id)
    {
        $check = DB::table('user_pages')->where('page_id', $page_id)->first();
        if ($check->published == 1) {
            DB::table('user_pages')->where('page_id', $page_id)->update(array('published' => 0));
        }
        if ($check->published == 0) {
            DB::table('user_pages')->where('page_id', $page_id)->update(array('published' => 1));
        }

        return true;
    }

    public function deletePage($page_id)
    {
        DB::table('user_pages')->where('page_id', $page_id)->delete();

        return true;
    }

    // Get All blog type page
    public function getHomePageBlog()
    {
        $data = DB::table('user_pages')->where('published', 1)->where('page_type', 2)->where('is_featured', 1)->take(4)->orderBy('page_id', 'desc')->get();

        return $data;
    }

    // Get All blog type page
    public function getAllBlog()
    {
        $data = DB::table('user_pages')->where('published', 1)->where('page_type', 2)->orderBy('page_id', 'desc')->paginate(10);

        return $data;
    }

    // Get last 20 blog for menu
    public function getMenuBlogList()
    {
        $data = DB::table('user_pages')->take(20)->where('published', 1)->where('page_type', 2)->orderBy('page_id', 'desc')->get();

        return $data;
    }

    // Custome text save
    public function saveCustomText($inputs)
    {
        $get_max_id = DB::table('user_page_setting')->max('page_id');  // Get max id
        $data = DB::table('user_page_setting')->where('page_id', $get_max_id)->update($inputs);

        return true;
    }

    // Get custom text
    public function getCustomText()
    {
        $get_max_id = DB::table('user_page_setting')->max('page_id');  // Get max id
        $data = DB::table('user_page_setting')->where('page_id', $get_max_id)->first();

        return $data;
    }

    // Get result by result ID
    public function getResultById($result_id)
    {
        $data = DB::table('user_quiz_result')->where('id', $result_id)->first();

        return $data;
    }

    // Update Analytics code
    public function updateAnalyticsCode($data)
    {
        DB::table('user_page_setting')->where('page_id', $data['page_id'])->update(array('analytics_code' => $data['analytics_code']));

        return true;
    }

    // Update Guess number setting

    public function updateGuessNumber($number)
    {
        $get_max_id = DB::table('user_page_setting')->max('page_id');  // Get max id
      $data = DB::table('user_page_setting')->where('page_id', $get_max_id)->first();
        $data = DB::table('user_page_setting')->where('page_id', $get_max_id)->update($number);

        return true;
    }

    // Load email configuration information
    public function loadEmailConfiguration()
    {
        $check_data = DB::table('email_settings')->max('id');
        $data = DB::table('email_settings')->where('id', $check_data)->first();

        return $data;
    }

    // Update email config

    public function updateEmail($data)
    {
        $check_data = DB::table('email_settings')->max('id');
      # if already settings found , new information will be updated
      if ($check_data > 0) {
          DB::table('email_settings')->where('id', $check_data)->update($data);

          return true;
      } else {
          # if no record found , new settings will be inserted
        DB::table('email_settings')->where('id', $check_data)->insert($data);

          return true;
      }

        return false;
    }

    // Guess number status change
    public function guessNumberStatus()
    {
        // Get setting data
      $check_status = DB::table('user_page_setting')->where('page_id', DB::table('user_page_setting')->max('page_id'))->first();
        if ($check_status->guess_number_status == 1) {
            DB::table('user_page_setting')->where('page_id', DB::table('user_page_setting')->max('page_id'))->update(array('guess_number_status' => 0));

            return 0;
        } else {
            DB::table('user_page_setting')->where('page_id', DB::table('user_page_setting')->max('page_id'))->update(array('guess_number_status' => 1));

            return 1;
        }

        return 2;
    }
}
