<?php

/**
 * Obsługuje akcje dynamiczne widoku użytkowników
 * 
 * @package article
 * @subpackage Controller
 * @version 1.0 2013-05-08 00:46 
 * @author Adrian Frączak <biuro@provisio.com.pl>
 */
class ArticledynamicController extends Zend_Controller_Action {

    public function preDispatch() {

       $inst = Zend_Auth::getInstance()->getIdentity();
        
        $notin = false;        
        if( ! $inst )
            $notin = true;
        elseif( $inst->type == 1 || $inst->type == 2 )
            $notin = true;
                
        if ($notin) {
            
            Core_Session::set('loginRedirect', 'http://' . $_SERVER['HTTP_HOST'] . Zend_Controller_Front::getInstance()->getRequest()->getRequestUri());
            
            Zend_Auth::getInstance()->clearIdentity();            
            $this->_helper->redirector->gotoSimple('login', 'index');
            
        }
    }

    public function init() {
        /* Initialize action controller here */
    }

    public function setthumbAction() {
        $tmp_name = $_FILES['uploadfile']['tmp_name'];
        $dir = Core_Path::publicFolder() . 'uploads/';
        $name = basename($_FILES['uploadfile']['name']);

        $oname = md5(date("YmdHis")) . $name;
        $dir_image = $dir . $oname;

        move_uploaded_file($tmp_name, $dir_image);

        $style = Core_Image::getStyle($dir_image, 200, 150);

        $id = $this->_request->getParam('id');

        if ($id != null) {
            $obj = ArticleApi::get($id);
            $obj->thumb()->update($oname);
        }

        $url = Core_Path::web() . 'files/article/' . $oname;
        $thumb = Core_Path::web() . 'files/article/' . $oname;

        $data = array();

        $data['name'] = $name;
        $data['objecturl'] = $oname;
        $data['url'] = $url;
        $data['thumburl'] = $thumb;
        $data['style'] = $style;
        $data['id'] = -1;

        $this->_helper->json($data);
    }

    public function uploadoneAction() {

        $tmp_name = $_FILES['uploadfile']['tmp_name'];
        $dir = Core_Path::publicFolder() . '/uploads/';
        $name = basename($_FILES['uploadfile']['name']);

        $oname = md5(date("YmdHis")) . $name;
        $dir_image = $dir . $oname;
        $dir_thumb = $dir . 'thumb_' . $oname;

        move_uploaded_file($tmp_name, $dir_image);
        Core_Image::minimizeAndMove($dir_image, $dir_thumb, 200, 200);

        $id = $this->_request->getParam('id');
        $style = Core_Image::getStyle($dir_thumb, 200, 150);
        $url = Core_Path::web() . '/uploads/' . $oname;
        $thumb = Core_Path::web() . '/uploads/thumb_' . $oname;
        $image_id = -1;

        if ($id != null) {
            $obj = ArticleApi::get($id);
            $image = $obj->addImage($oname, 0);
            $image_id = $image->id;

            $url = $image->url();
            $thumb = $image->thumb();
        }

        $data = array();
        $data['name'] = $name;
        $data['objecturl'] = $oname;
        $data['url'] = $url;
        $data['thumburl'] = $thumb;
        $data['style'] = $style;
        $data['id'] = $image_id;

        $this->_helper->json($data);
    }

    public function imagelistAction() {
        $images_id = $this->_request->getParam('imageList', array());

        $number = 0;
        $obj = NULL;

        foreach ($images_id as $img_id) {
            $image = ArticleApi::getImage($img_id);
            $image->number = $number++;
            $image->save();

            if ($obj == NULL)
                $obj = $image->article();
        }

        $this->_helper->json(array());
    }

    public function deleteimageAction() {
        $id = $this->_request->getParam('id', -1);

        if ($id == -1) {

            $name = $this->_request->getParam('objectname');

            if ($name) {
                unlink(Core_Path::publicFolder() . '/uploads/' . $name);
                unlink(Core_Path::publicFolder() . '/uploads/thumb_' . $name);
            }
        } else {

            $image = ArticleApi::getImage($id);
            $article = $image->article();
            $image->delete();

        }

        $this->_helper->json(array());
    }

    public function setdataAction() {

        $id = $this->_request->getParam('id', NULL);
        $type = $this->_request->getParam('type', 1);
        $value = $this->_request->getParam('data', '');

        $obj = ArticleApi::get($id);
        $obj->__set($type, $value);
        $obj->updateMod();
        $obj->save();


        $this->_helper->json($obj->retArray());
    }

    public function setcategoryAction() {
        $id = $this->_request->getParam('id', NULL);
        $value = $this->_request->getParam('data', '');

        $obj = ArticleApi::get($id);
        $obj->__set('id_main_category', $value);

        $obj->updateCategory();
        $obj->updateMod();
        $obj->save();

        $this->_helper->json($obj->retArray());
    }

    public function getchildAction() {
        $parent = $this->_request->getParam('parent', NULL);

        $obj = ArticleApi::getCategory($parent);
        $data = array();
        $data['options'] = $obj->kids()->toArray();
        $data['size'] = $obj->size();

        $this->_helper->json($data);
    }

    public function addcategoryAction() {

        $arr = array();
        
        $arr['id_parent'] = $this->_request->getParam('id_parent', NULL);
        $arr['is_show'] = $this->_request->getParam('is_show', NULL);
        $arr['name'] = $this->_request->getParam('name', NULL);
        $arr['title'] = $this->_request->getParam('title', NULL);
        $arr['keywords'] = $this->_request->getParam('keywords', NULL);
        $arr['description'] = $this->_request->getParam('description', NULL);
        
        $cat = ArticleApi::addCategory($arr);


        $this->_helper->json($cat->toArray());
    }

    public function deletecategoryAction() {

        $id = $this->_request->getParam('id', NULL);

        $cat = ArticleApi::getCategory($id);
        $cat->delete();
        $this->_helper->json(array());
    }

    public function setlocationAction() {

        $arr = $this->_request->getParam('list', array());

        $i = 0;
        foreach ($arr as $el => $k) {

            if ($k == 'null') {

                $blog = ArticleApi::getCategory($el);
                $blog->number = $i++;
                $blog->save();
            }
        }

        $this->_helper->json(array());
    }

    public function setdatacategoryAction() {

        $id = $this->_request->getParam('id', NULL);
        $type = $this->_request->getParam('type', 1);
        $value = $this->_request->getParam('data', '');

        $obj = ArticleApi::getCategory($id);
        $obj->__set($type, $value);
        $obj->updateMod();
        $obj->save();


        $this->_helper->json($obj);
    }

    public function setdataconfigAction() {

        $type = $this->_request->getParam('type', 1);
        $value = $this->_request->getParam('value', '');

        $obj = ArticleApi::getConfig();
        $obj->__set($type, $value);
        $obj->updateMod();
        $obj->save();


        $this->_helper->json($obj);
    }

}

