<?php
if (!defined('IN_CONTEXT')) die('access violation error!');

class ModDownloadCategory extends Module {
	
	protected $_filters = array(
        'check_admin' => ''
    );
	
    public function admin_list() {
        $this->_layout = 'content';
        
        $curr_locale = trim(SessionHolder::get('_LOCALE'));
        $mod_locale = trim(SessionHolder::get('mod_download_category/_LOCALE', $curr_locale));
        $lang_sw = trim(ParamHolder::get('lang_sw', $mod_locale));
        SessionHolder::set('mod_download_category/_LOCALE', $lang_sw);
        
        $download_category_data =& 
            Pager::pageByObject('DownloadCategory', "s_locale=? and id<>?", array($lang_sw, 1), 
                "ORDER BY `id` DESC");
        
        $this->assign('download_categories', $download_category_data['data']);
        $this->assign('pager', $download_category_data['pager']);
        
        $this->assign('lang_sw', $lang_sw);
        $this->assign('langs', Toolkit::loadAllLangs());
    }
    
    public function admin_add() {
        $this->_layout = 'content';
        
        $curr_locale = trim(SessionHolder::get('_LOCALE'));
        $mod_locale = trim(SessionHolder::get('mod_download_category/_LOCALE', $curr_locale));
        
        $this->assign('download_category_title', __('New Category'));
        $this->assign('next_action', 'admin_create');

        $this->assign('mod_locale', $mod_locale);
        $this->assign('langs', Toolkit::loadAllLangs());
        $this->assign('roles', Toolkit::loadAllRoles());
        
        return '_form';
    }
    
    public function admin_create() {
        
        $d_c_info =& ParamHolder::get('dc', array());
        if (sizeof($d_c_info) <= 0) {
            $this->assign('json', Toolkit::jsonERR(__('Missing category information!')));
            return '_result';
        }
        $is_member_only = ParamHolder::get('ismemonly', '0');
        try {
        	$d_c_info['alias'] = 'cad_'.Toolkit::randomStr(8);
            // Re-arrange publish status
            if ($d_c_info['published'] == '1') {
                $d_c_info['published'] = '1';
            } else {
                $d_c_info['published'] = '0';
            }
            $d_c_info['for_roles'] = ACL::explainAccess(intval($is_member_only));
            $d_c_info['i_order'] = 0;
            
            // Data operation
            $o_download_category = new DownloadCategory();
            $o_download_category->set($d_c_info);
            $o_download_category->save();
        } catch (Exception $ex) {
            $this->assign('json', Toolkit::jsonERR($ex->getMessage()));
            return '_result';
        }
        
        $this->assign('json', Toolkit::jsonOK(array('forward' => Html::uriquery('mod_download_category', 'admin_list'))));
        return '_result';
    }
    
    public function admin_delete() {
        
        $download_category_id = trim(ParamHolder::get('download_category_id', '0'));
        if (intval($download_category_id) == 0) {
            $this->assign('json', Toolkit::jsonERR(__('Invalid ID!')));
            return '_result';
        }
        try {
			$tmp_arr = explode('_', $download_category_id);
			$len = sizeof($tmp_arr);
			for ($i = 0; $i< $len; $i++){
				$curr_download_category = new DownloadCategory($tmp_arr[$i]);
				$curr_download = new Download();
				$downloads = $curr_download->findAll("download_category_id=?", array($tmp_arr[$i]));
				$curr_download->set_d($downloads);
				$curr_download_category->delete();
			}
        } catch (Exception $ex) {
            $this->assign('json', Toolkit::jsonERR($ex->getMessage()));
            return ('_result');
        }
        
        $this->assign('json', Toolkit::jsonOK());
        return '_result';
    }
    
    public function admin_edit() {
        $this->_layout = 'content';
        
        $download_category_id = ParamHolder::get('download_category_id', '0');
        if (intval($download_category_id) == 0) {
            $this->assign('json', Toolkit::jsonERR(__('Invalid ID!')));
            return '_error';
        }
        
        try {
            $curr_download_category = new DownloadCategory($download_category_id);
            $this->assign('curr_download_category', $curr_download_category);

        } catch (Exception $ex) {
            $this->assign('json', Toolkit::jsonERR($ex->getMessage()));
            return '_error';
        }
        
        $this->assign('download_category_title', __('Edit Category'));
        $this->assign('next_action', 'admin_update');
        
        $this->assign('langs', Toolkit::loadAllLangs());
        $this->assign('roles', Toolkit::loadAllRoles());
        
        return '_form';
    }
    
    public function admin_update() {
        $flag = false;
        $dc_info =& ParamHolder::get('dc', array());
        if (sizeof($dc_info) <= 0) {
            $this->assign('json', Toolkit::jsonERR(__('Missing category information!')));
            return '_result';
        }
        $is_member_only = ParamHolder::get('ismemonly', '0');
        try {
            // Re-arrange publish status
            if ($dc_info['published'] == '1') {
                $dc_info['published'] = '1';
                $flag = true;
            } else {
                $dc_info['published'] = '0';
                $flag = false;
            }
            $dc_info['for_roles'] = ACL::explainAccess(intval($is_member_only));
            
            // Data operation
            $o_dc = new DownloadCategory($dc_info['id']);
            $o_dc->set($dc_info);
            $o_dc->save();
            
            $o_download = new Download();
            $downloads = $o_download->findAll("download_category_id=?",array($dc_info['id']));
         	if(!empty($downloads))
            {
            	foreach($downloads as $k => $v)
            	{
            		$downloads_info['published'] = ($flag) ? '1' : '0';
            		$downloads_info['for_roles'] = $dc_info['for_roles'];
            		$obj_download = new Download($v->id);
            		$obj_download->set($downloads_info);
            		$obj_download->save();
            	}
            }
        } catch (Exception $ex) {
            $this->assign('json', Toolkit::jsonERR($ex->getMessage()));
            return '_result';
        }
        
        $this->assign('json', Toolkit::jsonOK(array('forward' => Html::uriquery('mod_download_category', 'admin_list'))));
        return '_result';
    }
    
	public function admin_pic()
    {
    	$download_category_info = array();
    	$download_category_id = trim(ParamHolder::get('_id', ''));
    	if(!empty($download_category_id))
    	{
    		$o_downloadcategory = new DownloadCategory($download_category_id);
            if($o_downloadcategory->published == 1)
            {
            	$download_category_info['published'] = '0';
            	$o_downloadcategory->set($download_category_info);
            	$o_downloadcategory->save();
				die('0');
            }
            elseif($o_downloadcategory->published == 0)
            {
            	$download_category_info['published'] = '1';
            	$o_downloadcategory->set($download_category_info);
            	$o_downloadcategory->save();
				die('1');
            }
    	}
    }
}
?>