root/events/phpframework/piece_framework/trunk/libs/Phwittr/Self.php @ 14808

Revision 14808, 4.3 kB (checked in by kumatch, 5 years ago)

Added update()/delete() methods.

  • Property svn:keywords set to Id Rev
Line 
1<?php
2/* vim: set expandtab tabstop=4 shiftwidth=4: */
3
4/*
5 * PHP versions 5
6 *
7 * @package    Phwittr_PieceFramework
8 * @copyright  2008 KUMAKURA Yousuke All rights reserved.
9 * @version    SVN: $Id$
10 * @since      File available since Release 0.1.0
11 */
12
13require_once 'Phwittr/User.php';
14require_once 'Phwittr/Self/Exception.php';
15
16// {{{ Phwittr_Self
17
18/**
19 * Phwittr ログインユーザ
20 *
21 * @package    Phwittr_PieceFramework
22 * @copyright  2008 KUMAKURA Yousuke All rights reserved.
23 * @version    Release: @package_version@
24 * @since      Class available since Release 0.1.0
25 */
26class Phwittr_Self extends Phwittr_User
27{
28
29    // {{{ properties
30
31    /**#@+
32     * @access public
33     */
34
35    /**#@-*/
36
37    /**#@+
38     * @access protected
39     */
40
41    /**#@-*/
42
43    /**#@+
44     * @access private
45     */
46
47    /**#@-*/
48
49    /**#@+
50     * @access public
51     */
52
53    // }}}
54    // {{{ __construct
55
56    /**
57     * __construct
58     *
59     * @param integer $userId
60     * @return void
61     */
62    public function __construct($id)
63    {
64        Phwittr_Config::configurePieceORM();
65       
66        $mapper = Piece_ORM::getMapper('Users');
67        $user = $mapper->findById($id);
68        if (is_null($user)) {
69            throw new Phwittr_Self_Exception();
70        }
71
72        foreach ($user as $key => $value) {
73            $this->$key = $value;
74        }
75    }
76
77    // }}}
78    // {{{ listByFollowers
79
80    /**
81     * フォローしているユーザと自身の発言リストを取得する
82     *
83     * @param  integer $page
84     * @return mixed
85     */
86    public function listByFollowers($page = 1)
87    {
88        $criteria->userId = $this->id;
89
90        $mapper = $this->_createStatusListMapper($this->_limit * ($page - 1));
91        $statusList = $mapper->findAllByUserIdForListFollowersStatus($criteria);
92        if (count($statusList) == 0 && $page > 2) {
93            return $this->listByFollowers();
94        }
95
96        $this->_checkNextPage($mapper, $page);
97
98        return $statusList;
99    }
100
101    // }}}
102    // {{{ listReplies
103
104    /**
105     * 自身への返信リストを取得する
106     *
107     * @param  integer $page
108     * @return mixed
109     */
110    public function listReplies($page = 1)
111    {
112        $mapper = $this->_createStatusListMapper($this->_limit * ($page - 1));
113        $statusList = $mapper->findAllByReplyUserId($this->id);
114        if (count($statusList) == 0 && $page > 2) {
115            return $this->listReplies();
116        }
117
118        $this->_checkNextPage($mapper, $page);
119
120        return $statusList;
121    }
122
123    // }}}
124    // {{{ update
125
126    /**
127     * ステータスを更新する
128     *
129     * @param string $comment
130     * @return mixed
131     */
132    public function update($comment)
133    {
134        $status = Piece_ORM::createObject('Statuses');
135        $status->userId = $this->id;
136        $status->comment = $comment;
137        $status->replyUserId = $this->_findReplyUserId($comment);
138        $status->createdAt = date('Y-m-d H:i:s');
139
140        $mapper = Piece_ORM::getMapper('Statuses');
141        return $mapper->insert($status);
142    }
143
144    // }}}
145    // {{{ delete
146
147    /**
148     * ステータスを削除する
149     *
150     * @param integer $id
151     * @return mixed
152     */
153    public function delete($id)
154    {
155        $criteria->id = $id;
156        $criteria->userId = $this->id;
157
158        $mapper = Piece_ORM::getMapper('Statuses');
159        $result = $mapper->deleteByIdAndUserId($criteria);
160        if (!$result) {
161            throw new Phwittr_Self_Exception();
162        }
163
164        return $result;
165    }
166
167    /**#@-*/
168
169    /**#@+
170     * @access private
171     */
172
173    // }}}
174    // {{{ _findReplyUserIdByComment
175
176    /**
177     * コメントから返信先ユーザIdを取得する
178     *
179     * @param string $comment
180     * @return mixed
181     */
182    private function _findReplyUserId($comment)
183    {
184        $replyUserId = null;
185        if (preg_match('/^@([0-9a-zA-Z_]{1,20}+).*$/', $comment, $matches)) {
186            $replyUserName = $matches[1];
187
188            $mapper = Piece_ORM::getMapper('Users');
189            $replyUser = $mapper->findByUserName($replyUserName);
190            if ($replyUser) {
191                $replyUserId = $replyUser->id;
192            }
193        }
194
195        return $replyUserId;
196    }
197
198    /**#@-*/
199
200    // }}}
201}
202
203// }}}
204
205/*
206 * Local Variables:
207 * mode: php
208 * coding: utf-8
209 * tab-width: 4
210 * c-basic-offset: 4
211 * c-hanging-comment-ender-p: nil
212 * indent-tabs-mode: nil
213 * End:
214 */
215?>
Note: See TracBrowser for help on using the browser.