<?php

class optionsModel
    extends baseModel
{
	static public $boolean_fields = [
		'enable_proposals',
		'enable_fundraising',
        'enable_deals',
		'enable_questions',
		'enable_blog',
		'enable_events',
		'enable_products',
		'enable_menu',
	];

	function get($name, $default = null)
	{
		$this->db->where('name = ?', $name);
		$value = $this->db->select_one('website_options', ['value'])['value'];

		return null == $value ? $default : $value;
	}

	function getList($cond = [])
	{
		$opts = $this->db->select("website_options");

		$options = [];
		foreach ($opts as $opt)
			$options[$opt['name']] = $opt;

		return $options;
	}

    function delete($name)
    {
        $this->db->where("name = ?", $name);
        $this->db->delete("website_options");
    }

	function update($name, $value)
	{
		$this->db->where("name = ?", $name);
		$row = $this->db->select_one("website_options");

		if (in_array($name, self::$boolean_fields))
			$value = empty($value) ? "0" : "1";

		if (empty($row))
			$this->db->insert("website_options", [
				'name'  => $name,
				'value' => $value]);
		else {
			$this->db->where("name = ?", $name);
			$this->db->update("website_options", ['value' => $value]);
		}
		return true;
	}

    function updateLogo($file, $type = "website")
    {
        // validate error, size and extension.
        if (!empty($file['error']) && is_array($file['error']))
            return false;

        if ($file['size'] > 10*1024*1024)
            return false;

        $ext = strtolower(pathinfo($file['name'], PATHINFO_EXTENSION));
        if (! in_array(strtolower($ext), ['jpg', 'jpeg', 'png', 'gif', 'bmp']))
            return false;

        // upload the file
        $filename    = md5_file($file['tmp_name']);
        $destination = "static/uploads/custom/$filename.$ext";
        if (!file_exists($destination))
            move_uploaded_file($file['tmp_name'], $destination);

        // save to uploads db table
        $uploadsModel = new uploadsModel();
        $id = $uploadsModel->insert($destination, "image", $file['name']);

        // save website_setting
        if (!in_array($type, ['website', 'footer', 'mobile']))
            $type = "website";

        $this->update($type . "_logo", $destination);

        return true;
    }

	function updateImageOption($file, $option)
	{
		// validate error, size and extension.
		if (!empty($file['error']) && is_array($file['error']))
			return false;

        if ($file['size'] > 10*1024*1024)
            return false;

        $ext = strtolower(pathinfo($file['name'], PATHINFO_EXTENSION));
        if (! in_array(strtolower($ext), ['jpg', 'jpeg', 'png', 'gif', 'bmp']))
            return false;

        // upload the file
        $filename    = md5_file($file['tmp_name']);
        $destination = "static/uploads/custom/$filename.$ext";
        if (!file_exists($destination))
            move_uploaded_file($file['tmp_name'], $destination);

        // save to uploads db table
        $uploadsModel = new uploadsModel();
        $id = $uploadsModel->insert($destination, "image", $file['name']);

        $this->update($option, $destination);

        return true;
	}
}
