| 1 |
lars |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
Prado::using('System.Data.*');
|
|
|
4 |
Prado::using('System.Web.UI.ActiveControls.*');
|
|
|
5 |
Prado::using('System.Data.ActiveRecord.TActiveRecordManager');
|
|
|
6 |
|
|
|
7 |
$db = new TDbConnection('mysql:host=localhost;dbname=xxxx', 'yyyy', 'zzzz');
|
|
|
8 |
$manager = TActiveRecordManager::getInstance();
|
|
|
9 |
$manager->setDbConnection($db);
|
|
|
10 |
|
|
|
11 |
class CommentRecord extends TActiveRecord
|
|
|
12 |
{
|
|
|
13 |
const TABLE='qs_comments';
|
|
|
14 |
|
|
|
15 |
public $id;
|
|
|
16 |
public $username;
|
|
|
17 |
public $date_added;
|
|
|
18 |
public $page;
|
|
|
19 |
public $block_id;
|
|
|
20 |
public $content;
|
|
|
21 |
|
|
|
22 |
public static function finder($className=__CLASS__)
|
|
|
23 |
{
|
|
|
24 |
return parent::finder($className);
|
|
|
25 |
}
|
|
|
26 |
}
|
|
|
27 |
|
|
|
28 |
class CommentBlock extends TTemplateControl
|
|
|
29 |
{
|
|
|
30 |
private $_page;
|
|
|
31 |
|
|
|
32 |
function onLoad($param)
|
|
|
33 |
{
|
|
|
34 |
if(!$this->Page->IsCallBack)
|
|
|
35 |
{
|
|
|
36 |
$count = array();
|
|
|
37 |
$data = $this->getCommentData();
|
|
|
38 |
foreach($data as $r)
|
|
|
39 |
{
|
|
|
40 |
if(!isset($count[$r->block_id]))
|
|
|
41 |
$count[$r->block_id]=0;
|
|
|
42 |
$count[$r->block_id]++;
|
|
|
43 |
}
|
|
|
44 |
$js = "var comment_count = ".TJavascript::encode($count).";\n";
|
|
|
45 |
$this->Page->ClientScript->registerBeginScript('count',$js);
|
|
|
46 |
$this->comments->dataSource = $data;
|
|
|
47 |
$this->comments->dataBind();
|
|
|
48 |
}
|
|
|
49 |
}
|
|
|
50 |
|
|
|
51 |
function getCommentData()
|
|
|
52 |
{
|
|
|
53 |
return CommentRecord::finder()->findAllByPage($this->getCurrentPagePath());
|
|
|
54 |
}
|
|
|
55 |
|
|
|
56 |
function add_comment($sender, $param)
|
|
|
57 |
{
|
|
|
58 |
if(!$this->Page->IsValid)
|
|
|
59 |
return;
|
|
|
60 |
$record = new CommentRecord;
|
|
|
61 |
$record->username = $this->username->Text;
|
|
|
62 |
$record->date_added = date('Y-m-d h:i:s');
|
|
|
63 |
$record->page = $this->getCurrentPagePath();
|
|
|
64 |
$record->block_id = $this->block_id->Value;
|
|
|
65 |
$record->content = $this->content->Text;
|
|
|
66 |
$record->save();
|
|
|
67 |
|
|
|
68 |
$this->content->Text = '';
|
|
|
69 |
$this->password->Text = '';
|
|
|
70 |
$cc = $this->Page->CallbackClient;
|
|
|
71 |
$cc->appendContent('comment-list', $this->format_message($record));
|
|
|
72 |
$cc->callClientFunction('hide_add_comment');
|
|
|
73 |
$cc->callClientFunction('increment_count_tag', $record->block_id);
|
|
|
74 |
if(!$this->Page->IsCallBack)
|
|
|
75 |
{
|
|
|
76 |
$this->comments->dataSource = $this->getCommentData();
|
|
|
77 |
$this->comments->dataBind();
|
|
|
78 |
}
|
|
|
79 |
}
|
|
|
80 |
|
|
|
81 |
protected function getCurrentPagePath()
|
|
|
82 |
{
|
|
|
83 |
if(is_null($this->_page))
|
|
|
84 |
{
|
|
|
85 |
$page = str_replace($this->Service->BasePath, '', $this->Page->Template->TemplateFile);
|
|
|
86 |
$this->_page = str_replace('\\', '/', $page);
|
|
|
87 |
}
|
|
|
88 |
return $this->_page;
|
|
|
89 |
}
|
|
|
90 |
|
|
|
91 |
function validate_credential($sender, $param)
|
|
|
92 |
{
|
|
|
93 |
$param->IsValid = $this->password->Text == 'Prado';
|
|
|
94 |
}
|
|
|
95 |
|
|
|
96 |
protected function format_message($record)
|
|
|
97 |
{
|
|
|
98 |
$username=htmlspecialchars($record->username);
|
|
|
99 |
$content=nl2br(htmlspecialchars($record->content));
|
|
|
100 |
return <<<EOD
|
|
|
101 |
<div class="comment c-{$record->block_id}">
|
|
|
102 |
<span><strong>{$username}</strong> on {$record->date_added}.</span>
|
|
|
103 |
<div>{$content}</div>
|
|
|
104 |
</div>
|
|
|
105 |
EOD;
|
|
|
106 |
}
|
|
|
107 |
}
|