<?php
/**
 * CAI CP v.0.9
 * Applications class
 * 
 * build date  2017-07-05
 *
 * @package   : System
 * @author    : Alexander I. Chebykin <alex.chebykin@gmail.com>
 * @copyright : Copyright (c) 2017 Alexander I. Chebykin
 * @version   : 0.9
 * @license   : MIT
 * @link      : https://github.com/CAI79/CAI-CP
 ******************************************************************/

namespace CAI\CAICP;

class Applications
{
    protected $apps_avail   = array();
    protected $apps_enabled = array();
    protected $cp_root      = '';

    /**
     * Constructor
     * 
     * @param string $root_dir Control panel's root directory
     */
    function __construct($root_dir)
    {
        $this->cp_root = filter_input(INPUT_SERVER, 'DOCUMENT_ROOT') . $root_dir;
        $this->enum_enabled_apps();
        $this->enum_avail_apps();
    }

    /**
     * Read available applications info
     */
    private function enum_avail_apps()
    {
        $dir = opendir($this->cp_root . '/system/apps');
        while($file = readdir($dir)) {
            if (is_dir($this->cp_root . '/system/apps/' . $file) 
                    && $file != '.' && $file != '..' 
                    && file_exists($this->cp_root . '/system/apps/' . $file . '/app.json')) {
                $app_info = json_decode(file_get_contents($this->cp_root . 
                                                        '/system/apps/' .
                                                        $file .
                                                        '/app.json'),
                                        true);
                if (!array_key_exists('version', $app_info)) {
                    $app_info['version'] = '';
                }
                if (!array_key_exists('author', $app_info)) {
                    $app_info['author'] = '';
                }
                $this->apps_avail[$file] = array('caption' => $app_info['caption'],
                                                 'version' => $app_info['version'],
                                                 'author'  => $app_info['author'],
                                                 'enabled' => array_key_exists($app_info['caption'],
                                                                               $this->apps_enabled));
            }
        }
    }

    /**
     * Returns enabled applications list
     * 
     * @return array enabled applications list
     */
    public function enum_enabled_apps() {
        $this->apps_enabled = json_decode(file_get_contents($this->cp_root . 
                                                        '/system/apps/apps.json'),
                                        true);
    }

    /**
     * Returns available applications list
     * 
     * @return array available applications list
     */
    public function get_avail() {
        return $this->apps_avail;
    }

    /**
     * Returns available applications list in json format
     * 
     * @return string available applications list in json format
     */
    public function get_avail_json() {
        return html_entity_decode(json_encode($this->get_avail()));
    }

    /**
     * Returns enabled applications list
     * 
     * @return array enabled applications list
     */
    public function get_enabled() {
        return $this->apps_enabled;
    }

    /**
     * Returns enabled applications list in json format
     * 
     * @return string enabled applications list
     */
    public function get_enabled_json() {
        return html_entity_decode(json_encode($this->get_enabled()));
    }

    /**
     * Enable apps
     * 
     * @param array $values Array with apps
     */
    public function enable($values)
    {
        $this->apps_enabled = $values;
        
    }

    /**
     * Enable apps
     * 
     * @param string $json Apps in JSON format
     */
    public function enable_json($json)
    {
        $this->enable(json_decode($json, true));
    }

    /**
     * Set list of enabled applications
     * 
     * @param array $values array with enabled applications
     */
    public function set($values)
    {
        $this->apps_enabled = $values;
    }

    /**
     * Saves enabled applications list
     * 
     * @return bool
     */
    public function save()
    {
        return file_put_contents($this->cp_root . '/system/apps/apps.json',
                        html_entity_decode(json_encode($this->apps_enabled)));
    }
}
