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

class ModBulletin 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_bulletin/_LOCALE', $curr_locale));
        $lang_sw = trim(ParamHolder::get('lang_sw', $mod_locale));
        SessionHolder::set('mod_bulletin/_LOCALE', $lang_sw);

        $where = "s_locale=?";
        $params = array($lang_sw);
        
        $bulletin_data =&
            Pager::pageByObject('Bulletin', $where, $params,
                "ORDER BY `create_time` DESC");
        $bulletin_id = $bulletin_data['data'][0]->id;
        if (!empty($bulletin_id)) {
        	$next_action = 'admin_update';
        } else {
        	$next_action = 'admin_create';
        }

        $this->assign('curr_bulletin', $bulletin_data['data'][0]);
        $this->assign('next_action',$next_action);
        $this->assign('mod_locale', $mod_locale);
        $this->assign('langs', Toolkit::loadAllLangs());

        return '_form';
    }
        
    public function admin_create() {

        $bulletin_info =& ParamHolder::get('bulletin', array());
        if (sizeof($bulletin_info) <= 0) {
            $this->assign('json', Toolkit::jsonERR(__('Missing bulletin information!')));
            return '_result';
        }

        $is_member_only = ParamHolder::get('ismemonly', '0');
        try {
            $bulletin_info['pub_start_time'] = -1;
            $bulletin_info['pub_end_time'] = -1;

			$bulletin_info['published'] = '1';
            $bulletin_info['for_roles'] = ACL::explainAccess(intval($is_member_only));
            $bulletin_info['title'] = '';
            $bulletin_info['content'] = $bulletin_info['content'];
            $bulletin_info['create_time'] = time();

            // Data operation
            $o_bulletin = new Bulletin();
            $o_bulletin->set($bulletin_info);
            $o_bulletin->save();
        } catch (Exception $ex) {
            $this->assign('json', Toolkit::jsonERR($ex->getMessage()));
            return '_result';
        }

        $this->assign('json', Toolkit::jsonOK(array('forward' => Html::uriquery('mod_bulletin', 'admin_list'), 
            'id' => $o_bulletin->id, 'title' => $o_bulletin->title)));
        return '_result';
    }
        
    public function admin_update() {

        $bulletin_info =& ParamHolder::get('bulletin', array());
        if (sizeof($bulletin_info) <= 0) {
            $this->assign('json', Toolkit::jsonERR(__('Missing bulletin information!')));
            return '_result';
        }
        $is_member_only = ParamHolder::get('ismemonly', '0');
        try {
            $bulletin_info['pub_start_time'] = -1;
			$bulletin_info['pub_end_time'] = -1;

            $bulletin_info['for_roles'] = ACL::explainAccess(intval($is_member_only));
            $bulletin_info['title'] = '';
            $bulletin_info['content'] = $bulletin_info['content'];
			$bulletin_info['create_time'] = time();

            // Data operation
            $o_bulletin = new Bulletin($bulletin_info['id']);
            $o_bulletin->set($bulletin_info);
            $o_bulletin->save();
        } catch (Exception $ex) {
            $this->assign('json', Toolkit::jsonERR($ex->getMessage()));
            return '_result';
        }

        $this->assign('json', Toolkit::jsonOK(array('forward' => Html::uriquery('mod_bulletin', 'admin_list'))));
        return '_result';
    }
    
}
?>