<?php

defined('PHPFOX') or exit('NO DICE!');

class Photo_Component_Controller_Admincp_Add extends Phpfox_Component
{
    public function process(){
        $bIsEdit = false;
        $iDeleteId = $this->request()->getInt('delete');
        if ($iDeleteId && Phpfox::getUserParam('photo.can_edit_photo_categories', true)) {
            if (Photo_Service_Category_Process::instance()->delete($iDeleteId)) {
                $this->url()->send('admincp.photo', null, _p('Category successfully deleted'));
            }
        }
        $aLanguages = Language_Service_Language::instance()->getAll(true);
        if (($iEditId = $this->request()->getInt('id'))) {
            $aRow = Photo_Service_Category_Category::instance()->getCategory($iEditId);
            $bIsEdit = true;
            $this->template()->assign(array(
                    'aForms' => $aRow,
                    'iEditId' => $iEditId
                )
            );
        }

        if ($aVals = $this->request()->getArray('val')) {
            $aVals['parent_id'] = (int) $aVals['parent_id'];
            if ($aVals['parent_id'] > 0){
                $aRedirectParam = ['parent' => $aVals['parent_id']];
            } else {
                $aRedirectParam = [];
            }
            if ($aVals = $this->_validate($aVals)) {
                if ($bIsEdit) {
                    Phpfox::getUserParam('photo.can_edit_photo_categories', true);
                    if (Photo_Service_Category_Process::instance()->update($aVals)) {
                        $this->url()->send('admincp.photo', $aRedirectParam,
                            _p('photo_category_successfully_updated'));
                    }
                } else {
                    Phpfox::getUserParam('photo.can_add_public_categories', true);
                    if (Photo_Service_Category_Process::instance()->add($aVals)) {
                        $this->url()->send('admincp.photo', $aRedirectParam,
                            _p('photo_category_successfully_added'));
                    }
                }
            }
        }

        $this->template()->setTitle(_p('Add photo categories'))
            ->setBreadCrumb(_p("Apps"), $this->url()->makeUrl('admincp.apps'))
            ->setBreadCrumb(_p("Photo"), $this->url()->makeUrl('admincp.photo'))
            ->setBreadCrumb(_p('Add photo categories'), $this->url()->makeUrl('admincp.photo.add'))
            ->assign([
                'aLanguages' => $aLanguages,
                'bIsEdit' => $bIsEdit
            ]);
        return null;
    }

    /**
     * validate input value
     * @param $aVals
     *
     * @return bool
     */
    private function _validate($aVals)
    {
        return Language_Service_Language::instance()->validateInput($aVals, 'name', false);
    }
    
    public function clean(){
        (($sPlugin = Phpfox_Plugin::get('photo.component_controller_admincp_add_clean')) ? eval($sPlugin) : false);
    }
}