<?php
/**
 * This file is part of the {@link http://ontowiki.net OntoWiki} project.
 *
 * @copyright Copyright (c) 2011-2016, {@link http://aksw.org AKSW}
 * @license   http://opensource.org/licenses/gpl-license.php GNU General Public License (GPL)
 */

/**
 * The main ckan controller provides the register and (later) the browser action
 *
 * @category   OntoWiki
 * @package    Extensions_Ckan
 */
class CkanController extends OntoWiki_Controller_Component
{
    /** @var CKAN registration uri base */
    protected $_registerBaseUrl = "http://thedatahub.org/package/new";

    /**
     * Constructor
     */
    public function init()
    {
        // this provides many std controller vars and other stuff ...
        parent::init();
        // init controller variables
        $this->store    = $this->_owApp->erfurt->getStore();
        $this->_config  = $this->_owApp->config;
        $this->response = Zend_Controller_Front::getInstance()->getResponse();
        $this->request  = Zend_Controller_Front::getInstance()->getRequest();
    }

    /**
     * search GUI to import CKAN packages into ontowiki
     * TODO: finish :)
     */
    public function browserAction()
    {
        $t = $this->_owApp->translate;
        $this->view->placeholder('main.window.title')->set($t->_('CKAN Package Browser'));
        $this->addModuleContext('main.window.ckan.browser');
        OntoWiki::getInstance()->getNavigation()->disableNavigation();
        echo "not finished yet";

        return;
    }

    /**
     * forwards to the CKAN registration page with some prefilled values
     */
    public function registerAction()
    {
        // this action needs no view
        $this->_helper->viewRenderer->setNoRender();
        // disable layout
        $this->_helper->layout()->disableLayout();

        // m (model) is automatically used and selected
        if ((!isset($this->request->m)) && (!$this->_owApp->selectedModel)) {
            throw new OntoWiki_Exception('No model pre-selected model and missing parameter m (model)!');
        } else {
            $model = $this->_owApp->selectedModel;
        }

        // get model URI / resource and load description
        $resourceUri = (string)$model;
        $resource    = new Erfurt_Rdf_Resource($resourceUri, $model);
        $description = $resource->getDescription();
        $description = $description[$resourceUri];

        // fill CKAN parameter
        $parameter          = array();
        $parameter['title'] = $model->getTitle();
        $parameter['url']   = $resourceUri;

        // go through the model info properties and use the first value as
        // notes value for ckan
        $infoProperties = $this->_config->descriptionHelper->properties;
        foreach ($infoProperties as $infoProperty) {
            if (!isset($parameter['description'])) {
                if (isset($description[$infoProperty][0]['value'])) {
                    $parameter['notes'] = $description[$infoProperty][0]['value'];
                }
            }
        }

        // build GET URI from the parameter array to redirect
        $getparams = '?';
        foreach ($parameter as $key => $value) {
            $getparams .= '&' . $key . '=' . urlencode($value);
        }
        $redirectUrl = $this->registerBaseUrl . $getparams;

        // redirect to CKANs dataset registration page
        $this->_response->setRedirect($redirectUrl, $code = 302);
    }
}

