<?php

/**
 * Set the ajax action and call for wordpress
 */
class CTLPPU_Classes_Action extends CTLPPU_Classes_FrontController {

    /** @var array with all form and ajax actions  */
    var $actions = array();

    /** @var array from core config */
    private static $config;


    /**
     * The hookAjax is loaded as custom hook in hookController class
     *
     * @return void
     */
    function hookInit() {
        /* Only if ajax */
        if (CTLPPU_Classes_Tools::isAjax()) {
            $this->getActions();
        }
    }

    /**
     * The hookHead is loaded as admin hook in hookController class for script load
     * Is needed for security check as nonce
     *
     * @return void
     */
    public function hookHead() {

        echo '<script type="text/javascript">
                  var ctlppuQuery = {
                    "ajaxurl": "' . admin_url('admin-ajax.php') . '",
                    "nonce": "' . wp_create_nonce(_CTLPPU_NONCE_ID_) . '"
                  }
              </script>';
    }

    /**
     * The hookSubmit is loaded when action si posted
     *
     * @return void
     */
    function hookMenu() {

        /* Only if post */
        if (!CTLPPU_Classes_Tools::isAjax()) {
            $this->getActions();
        }
    }

    function hookFrontinit() {
        /* Only if post */
        if (!CTLPPU_Classes_Tools::isAjax()) {
            $this->getActions();
        }
    }


    /**
     * Get all actions from config.xml in core directory and add them in the WP
     *
     * @return void
     */
    public function getActions() {
        $this->actions = array();
        $cur_action = CTLPPU_Classes_Tools::getValue('action', false);

        /* if config allready in cache */
        if (!isset(self::$config)) {
            $config_file = _CTLPPU_ROOT_DIR_ . 'config.json';
            if (!file_exists($config_file))
                return;

            /* load configuration blocks data from core config files */
            self::$config = json_decode(file_get_contents($config_file), 1);

        }

        if (is_array(self::$config))
            foreach (self::$config['blocks']['block'] as $block) {
                if (isset($block['active']) && $block['active'] == 1)
                    if (isset($block['admin']) &&
                            (($block['admin'] == 1 && (is_admin() || is_network_admin())) ||
                            $block['admin'] == 0)
                    ) {
                        /* if there is a single action */
                        if (isset($block['actions']['action']))

                        /* if there are more actions for the current block */
                            if (!is_array($block['actions']['action'])) {
                                /* add the action in the actions array */
                                if ($block['actions']['action'] == $cur_action)
                                    $this->actions[] = array('class' => $block['name']);
                            }else {
                                /* if there are more actions for the current block */
                                foreach ($block['actions']['action'] as $action) {
                                    /* add the actions in the actions array */
                                    if ($action == $cur_action)
                                        $this->actions[] = array('class' => $block['name']);
                                }
                            }
                    }
            }


        /* add the actions in WP */
        foreach ($this->actions as $actions) {
            CTLPPU_Classes_ObjController::getClass($actions['class'])->action();
        }
    }

}