public function indexAction()
 {
     $cache = Zend_Registry::get('cache');
     $id = md5("readrankControllerIndex");
     if (($result = $cache->load($id)) === false) {
         $article = new Application_Model_DbTable_Article();
         $db = $article->getAdapter();
         $select = $db->select();
         $select->from('article', array('id', 'title', 'view_counter'));
         $select->where('account_id = ?', BLOGGER_ID);
         $select->order('view_counter desc');
         $select->limit(10);
         $result = $db->fetchAll($select);
         $cache->save($result, $id, array('readrankbox'));
     }
     //print_r($result);
     $this->view->data = $result;
 }
 private function build_calendar($month, $year, $today)
 {
     // 日历表头,星期天开始一直到星期六
     $daysOfWeek = array('日', '一', '二', '三', '四', '五', '六');
     // 本月第一天的位置
     $firstDayOfMonth = mktime(0, 0, 0, $month, 1, $year);
     // 获取本月天数
     $numberDays = date('t', $firstDayOfMonth);
     // 获取本月第一天
     $dateComponents = getdate($firstDayOfMonth);
     // 获取月份的英文单词
     $monthName = $dateComponents['month'];
     //获取这个月发表的文章
     $mdlArt = new Application_Model_DbTable_Article();
     $db = $mdlArt->getAdapter();
     $select = $db->select();
     $select->from('article', array('title', 'day(add_time) as day'));
     $select->where('account_id = ?', BLOGGER_ID);
     $select->where('month(add_time)= ?', $month);
     $select->where('year(add_time)= ?', $year);
     $result = $db->fetchAll($select);
     //echo "<pre>";
     //echo $select->__toString();
     //print_r($result);
     //echo "</pre>";
     $dayOfWeek = $dateComponents['wday'];
     // 月历表头
     $calendar = "<table>";
     $calendar .= "<caption>{$year}-{$month}-{$today}</caption>";
     $calendar .= "<tr>";
     // 星期表头
     foreach ($daysOfWeek as $day) {
         $calendar .= "<th class='header'>{$day}</th>";
     }
     // 开始输出日历
     // 初始化天数计数器,从1号开始
     $currentDay = 1;
     $calendar .= "</tr><tr>";
     // 使用变量 $dayOfWeek 可以保证一周七天精确输出
     if ($dayOfWeek > 0) {
         $calendar .= "<td colspan='{$dayOfWeek}'>&nbsp;</td>";
     }
     $month = str_pad($month, 2, "0", STR_PAD_LEFT);
     while ($currentDay <= $numberDays) {
         // 7天一行,7天一到新增一行
         if ($dayOfWeek == 7) {
             $dayOfWeek = 0;
             $calendar .= "</tr><tr>";
         }
         $currentDayRel = str_pad($currentDay, 2, "0", STR_PAD_LEFT);
         $date = "{$year}-{$month}-{$currentDayRel}";
         $len = count($result);
         $title = "";
         for ($i = 0; $i < $len; $i++) {
             if ($result[$i]['day'] == $currentDay) {
                 $title .= '『' . $result[$i]['title'] . '』';
             }
         }
         if (empty($title)) {
             $str = $currentDay;
         } else {
             $str = "<a class='post_day' href='/" . BLOGGER_NAME . "/article/index/date/" . $year . "-" . $month . "-" . $currentDay . "' title='发表了: " . $title . "'>" . $currentDay . "</a>";
         }
         if ($currentDay != $today) {
             $calendar .= "<td class='day' rel='{$date}'>{$str}</td>";
         } else {
             $calendar .= "<td class='day today' rel='{$date}'>{$str}</td>";
         }
         // 计数器
         $currentDay++;
         $dayOfWeek++;
     }
     // 最后一行表格的处理,往往最后一行不可能全部填满,需要要空格填充。
     if ($dayOfWeek != 7) {
         $remainingDays = 7 - $dayOfWeek;
         $calendar .= "<td colspan='{$remainingDays}'>&nbsp;</td>";
     }
     $calendar .= "</tr>";
     $calendar .= "</table>";
     //echo sprintf("%s - %s - %s",$year, $month, $today);
     //print_r($year.'-'.$monthNum.'-'.$today);
     return $calendar;
 }
 public function searchAction()
 {
     if ($this->getRequest()->isPost()) {
         $keywords = trim($this->_request->getParam('keywords'));
         $result = preg_split("/\\s+/", $keywords);
         $article = new Application_Model_DbTable_Article();
         $db = $article->getAdapter();
         $len = count($result);
         $this->view->keywords = join('--', $result);
         /* $select->from('article', array('id','title','summary' => 'LEFT(article.content,200)','view_counter','add_time'));
         			$select->join('atype', 'article.atype_id = atype.id', 'atype');
         			$select->where('article.account_id = ?', BLOGGER_ID);
         			$select->Where('title LIKE ?' ,'%'.$result[0].'%');
         			$select->orWhere('content LIKE ?','%'.$result[0].'%'); */
         $select_arr = array();
         for ($i = 0; $i < $len; $i++) {
             if ($i == 0) {
                 $temp = 'select' . $i;
                 ${$temp} = $db->select();
                 ${$temp}->from('article', array('id', 'title', 'summary' => 'LEFT(article.content,200)', 'view_counter', 'add_time'));
                 ${$temp}->join('atype', 'article.atype_id = atype.id', 'atype');
                 ${$temp}->where('article.account_id = ?', BLOGGER_ID);
                 ${$temp}->Where('title LIKE ?', '%' . $result[$i] . '%');
                 ${$temp}->orWhere('content LIKE ?', '%' . $result[$i] . '%');
                 $select_arr[] = ${$temp};
             } else {
                 $temp = 'select' . $i;
                 ${$temp} = $db->select();
                 ${$temp}->from('article', array('id', 'title', 'summary' => 'LEFT(article.content,200)', 'view_counter', 'add_time'));
                 ${$temp}->join('atype', 'article.atype_id = atype.id', 'atype');
                 ${$temp}->where('article.account_id = ?', BLOGGER_ID);
                 ${$temp}->Where('title LIKE ?', '%' . $result[$i] . '%');
                 ${$temp}->orWhere('content LIKE ?', '%' . $result[$i] . '%');
                 $select_arr[] = ${$temp};
             }
             $select = $db->select();
             $select->union($select_arr);
             $select->group('article.id');
         }
         $data = $db->fetchAll($select);
         //分页
         $numPerPage = $this->_numPerPage;
         $pageRange = $this->_pageRange;
         $page = $this->_request->getParam('page', 1);
         $offset = $numPerPage * $page;
         $paginator = Zend_Paginator::factory($data);
         $paginator->setCurrentPageNumber($page)->setItemCountPerPage($numPerPage)->setPageRange($pageRange);
         $this->view->paginator = $paginator;
         $comment = new Application_Model_DbTable_Comment();
         $db = $comment->getAdapter();
         $select = $db->select();
         $select->from('comment', array('article_id', 'total' => 'count(*)'));
         $select->group('article_id');
         $comment = $db->fetchAll($select);
         $ca = array();
         foreach ($comment as $arr) {
             $ca[$arr['article_id']] = $arr['total'];
         }
         $this->view->comment = $ca;
         $user = new Zend_Session_Namespace('user');
         if (isset($user->nickname) && $user->nickname == BLOGGER_NAME) {
             $this->view->show = true;
         } else {
             $this->view->show = false;
         }
     }
 }
 public function saveAction()
 {
     if ($this->getRequest()->isPost()) {
         $article = new Application_Model_DbTable_Article();
         $db = $article->getAdapter();
         $row = array('id' => null, 'title' => $this->_request->getPost('title'), 'content' => $this->_request->getPost('blog_content'), 'account_id' => BLOGGER_ID, 'atype_id' => $this->_request->getPost('atype'));
         $rows_affected = $db->insert('article', $row);
         //获取插入id
         $last_insert_id = $db->lastInsertId();
         $cache = Zend_Registry::get('cache');
         $cache->clean(Zend_Cache::CLEANING_MODE_MATCHING_TAG, array('artcatebox'));
         //print_r($last_insert_id);
         $this->_helper->viewRenderer->setNoRender();
         //$this->_forward('detail','article',null,array('id'=>$last_insert_id));
         $this->_redirect('/article/detail/id/' . $last_insert_id);
     }
 }
 public function updateAction()
 {
     if ($this->getRequest()->isPost()) {
         $article = new Application_Model_DbTable_Article();
         $db = $article->getAdapter();
         $set = array('content' => $this->_request->getPost('content'));
         $article_id = $this->_request->getPost('article_id');
         $section_id = $this->_request->getPost('section_id');
         $where = $db->quoteInto('id = ?', $section_id);
         $rows_affected = $db->update('ui_article', $set, $where);
         $cache = Zend_Registry::get('cache');
         $cache->clean(Zend_Cache::CLEANING_MODE_MATCHING_TAG, array('article_index', 'article_list'));
         $this->_helper->viewRenderer->setNoRender();
         $this->_redirect('/article/detail/id/' . $article_id);
     }
 }