Location: PHPKode > projects > blog > blog/blogs_controller.php
<?php
/**
 *
 * @filesource
 * @copyright		Copyright 2008-2009, lesorb, Inc.
 * @link			http://www.lesorb.cn Blog Project
 * @package			app
 * @subpackage		app.controllers
 * @version			$Revision: 1.1 $
 * @author			WangLiJun
 * @lastmodified	$2009-4-20$
 */
/**
 * file used to handle the blog of commusers by the user himself
 *
 * @name		blogs_controller.php
 *
 * @copyright	lesorb
 */

class BlogsController extends AppController {

	var $name = 'Blogs';
	var $uses = array('Blog','User','BlogClass');
	var $helpers = array('Html', 'Form','Ajax','Javascript','tinymce');
	var $layout = 'default_commer';

	/*function myblog(){
		$sessUId = $this->Session->read('Users.id');

	}*/
	function lists($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('非法的请求!', true));
			//$this->redirect(array('action'=>'index'));
		}
	
		$users = $this->getUsers($id);
		
		$blogs = $this->paginate('Blog',array('`Blog`.`user_id`'=>$id));

		$this->set(compact('blogs','users'));
	}
	//view this bolg about user's 
	function view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('非法的请求!', true));
			$this->redirect(array('action'=>'lists'));
		}
	
		$users = $this->getUsers($id);

		$blog = $this->Blog->read(null, $id);

		$this->set(compact('users','blog'));
	}
	//view this bolg about commuser's classes
	function classes($id = null,$class_id) {
		if (!$id) {
			$this->Session->setFlash(__('非法的请求!', true));
			$this->redirect(array('action'=>'lists'));
		}	
		$users = $this->getUsers($id);

		$blogs = $this->paginate('Blog', array('`Blog`.`user_id`'=>$users['User']['id'],'`Blog`.`blogclass_id`'=>$class_id));

		$this->set(compact('users','blogs'));
	}
	//
    function manage_lists($post_status='publish') {

		$this->paginate = array(
			'conditions'=>array('`Blog`.`user_id`' => $this->Session->read('Users.id'),
										'post_status'=>$post_status),
			'limit' => 10,
			'page' => 1,
		);

		$this->Blog->recursive = 0;

		$this->set('blogs', $this->paginate());
	}

	function manage_view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('非法的请求!', true));
			$this->redirect(array('action'=>'index'));
		}
		$this->set('blog', $this->Blog->read(null, $id));
	}
	//
	function manage_add() {

			if (!empty($this->data)) {
					
					$this->Blog->create();
					$this->loadModels('Tag');
					$this->data['Blog']['tag'] = $this->Tag->parserToArrOrStr($this->data['Blog']['tag'],5);

					if ($this->Blog->save($this->data)) {

						$blogLastInsertID = $this->Blog->getLastInsertID();

						$strTags = $this->Tag->parserBeforeSaveTags($this->data['Blog']['tag']);
						
						$this->Tag->saveTagsToModel($strTags,$blogLastInsertID,50);

						$this->Session->setFlash(__('日志存储成功', true));
						$this->redirect(array('action'=>'lists',$this->Session->read('Commer.cid')));

					} else {
						$this->Session->setFlash(__('日志存储失败. 请您再试!', true));
					}
				}

				$blogclasses = $this->Blog->Blogclass->find('list',array('conditions' => 'commuser_id = '.$this->Session->read('Commer.cid'),
															'fields' => array('id','name'),
															'recursive' => 0));
				$this->set(compact('blogclasses'));
	}		

	function manage_edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('非法的请求!', true));
			$this->redirect(array('action'=>'index'));
		}

			if (!empty($this->data)) {

				$this->loadModels('Tag');					
				$this->data['Blog']['tag'] = $this->Tag->parserToArrOrStr($this->data['Blog']['tag'],5);

				if ($this->Blog->save($this->data)) {

					$strTags = $this->Tag->parserBeforeSaveTags($this->data['Blog']['tag']);
					$this->Tag->saveTagsToModel($strTags,$this->data['Blog']['id'],50);

					$this->Session->setFlash(__('日志编辑成功', true));
					$this->redirect(array('action'=>'index'));
				} else {
					$this->Session->setFlash(__('日志编辑不成功. 请您再试.', true));
				}
			}
			if (empty($this->data)) {
				$this->data = $this->Blog->read(null, $id);
			}

			$blogclasses = $this->Blog->Blogclass->find('list',array('conditions' => 'user_id = '.$this->Session->read('Users.id'),
															'fields' => array('id','name'),
															'recursive' => 0));
			$this->set(compact('blogclasses'));

	}
	function manage_classAdd(){
		
		if(!empty($this->data)){
			$this->Blog->Blogclass->create();
			$this->data['BlogClass']['user_id'] = $this->Session->read('Users.id');
		
			if($this->Blog->Blogclass->save($this->data)){
				$this->Session->setFlash(__('新的日志类别添加成功', true));
				$this->redirect(array('action'=>'classes','manage'=>true));
			}else{
				$this->Session->setFlash(__('日志类别添加不成功. 请您再试!', true));
			}
		}
	}
	function manage_classEdit($id=null){
		if(!$id){
			exit('非法的请求!');
		}

		if(!empty($this->data)){

			if($this->Blog->Blogclass->save($this->data)){
				$this->Session->setFlash(__('日志类别修改成功', true));
				$this->redirect(array('action'=>'classes','manage'=>true));
			}else{
				$this->Session->setFlash(__('日志类别修改不成功. 请您再试!', true));
			}
		}

		if(empty($this->data))
			$this->data = $this->Blog->Blogclass->read(null,$id);
	}
	function manage_classes(){

		$blogclasses = $this->Blog->Blogclass->find('list',array('conditions' => 'user_id = '.$this->Session->read('Users.id'),
															'fields' => array('id','name'),
															'recursive' => 0));
		$this->set(compact('blogclasses'));
	}
	function manage_classDel(){}
/*
	function manage_delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for Blog', true));
			$this->redirect(array('action'=>'index'));
		}
		if ($this->Blog->del($id)) {
			$this->Session->setFlash(__('Blog deleted', true));
			$this->redirect(array('action'=>'index'));
		}
	}
*/

	function admin_index() {
		$this->Blog->recursive = 0;
		$this->set('blogs', $this->paginate());
	}

	function admin_view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('非法的请求!', true));
			$this->redirect(array('action'=>'index'));
		}
		$this->set('blog', $this->Blog->read(null, $id));
	}

	function admin_add() {
		if (!empty($this->data)) {
			$this->Blog->create();
			if ($this->Blog->save($this->data)) {
				$this->Session->setFlash(__('The Blog has been saved', true));
				$this->redirect(array('action'=>'index'));
			} else {
				$this->Session->setFlash(__('The Blog could not be saved. Please, try again.', true));
			}
		}

		$blogclasses = $this->Blog->Blogclass->find('list');
		$this->set(compact('blogclasses'));
	}

	function admin_edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('非法的请求!', true));
			$this->redirect(array('action'=>'index'));
		}
		if (!empty($this->data)) {
			if ($this->Blog->save($this->data)) {
				$this->Session->setFlash(__('The Blog has been saved', true));
				$this->redirect(array('action'=>'index'));
			} else {
				$this->Session->setFlash(__('The Blog could not be saved. Please, try again.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->Blog->read(null, $id);
		}

		$blogclasses = $this->Blog->Blogclass->find('list');
		$this->set(compact('blogclasses'));
	}

	function admin_delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('非法的ID!', true));
			$this->redirect(array('action'=>'index'));
		}
		if ($this->Blog->del($id)) {
			$this->Session->setFlash(__('Blog deleted', true));
			$this->redirect(array('action'=>'index'));
		}
	}

	private function getUsers($id){
		$this->loadModels('User');
		$user = $this->User->find(array('id'=>$id),array('id','username','face','province','city','descriptions','aliasname','subdistrict'),null,0);

		return $user;
	}

}
?>
Return current item: blog