From 45b0fe42a979d444d547a5248eb2e9e915aaf16a Mon Sep 17 00:00:00 2001 From: wei <> Date: Sun, 14 Jan 2007 02:10:24 +0000 Subject: Add "block-content" to allow user comments on block level elements in quickstart docs. --- .../protected/controls/Comments/CommentBlock.php | 107 +++++++++++++++++++++ 1 file changed, 107 insertions(+) create mode 100644 demos/quickstart/protected/controls/Comments/CommentBlock.php (limited to 'demos/quickstart/protected/controls/Comments/CommentBlock.php') diff --git a/demos/quickstart/protected/controls/Comments/CommentBlock.php b/demos/quickstart/protected/controls/Comments/CommentBlock.php new file mode 100644 index 00000000..63b25c72 --- /dev/null +++ b/demos/quickstart/protected/controls/Comments/CommentBlock.php @@ -0,0 +1,107 @@ +setDbConnection($db); + +class CommentRecord extends TActiveRecord +{ + public $id; + public $username; + public $date_added; + public $page; + public $block_id; + public $content; + + public static $_tablename='qs_comments'; + + public static function finder() + { + return self::getRecordFinder('CommentRecord'); + } +} + +class CommentBlock extends TTemplateControl +{ + private $_page; + + function onLoad($param) + { + if(!$this->Page->IsCallBack) + { + $count = array(); + $data = $this->getCommentData(); + foreach($data as $r) + { + if(!isset($count[$r->block_id])) + $count[$r->block_id]=0; + $count[$r->block_id]++; + } + $js = "var comment_count = ".TJavascript::encode($count).";\n"; + $this->Page->ClientScript->registerBeginScript('count',$js); + $this->comments->dataSource = $data; + $this->comments->dataBind(); + } + } + + function getCommentData() + { + return CommentRecord::finder()->findAllByPage($this->getCurrentPagePath()); + } + + function add_comment($sender, $param) + { + if(!$this->Page->IsValid) + return; + $record = new CommentRecord; + $record->username = $this->username->Text; + $record->date_added = date('Y-m-d h:i:s'); + $record->page = $this->getCurrentPagePath(); + $record->block_id = $this->block_id->Value; + $record->content = $this->content->Text; + $record->save(); + + $this->content->Text = ''; + $this->password->Text = ''; + $cc = $this->Page->CallbackClient; + $cc->appendContent('comment-list', $this->format_message($record)); + $cc->callClientFunction('hide_add_comment'); + $cc->callClientFunction('increment_count_tag', $record->block_id); + if(!$this->Page->IsCallBack) + { + $this->comments->dataSource = $this->getCommentData(); + $this->comments->dataBind(); + } + } + + protected function getCurrentPagePath() + { + if(is_null($this->_page)) + { + $page = str_replace($this->Service->BasePath, '', $this->Page->Template->TemplateFile); + $this->_page = str_replace('\\', '/', $page); + } + return $this->_page; + } + + function validate_credential($sender, $param) + { + $param->IsValid = $this->password->Text == 'Prado'; + } + + protected function format_message($record) + { + $username=htmlspecialchars($record->username); + $content=nl2br(htmlspecialchars($record->content)); + return << + {$username} on {$record->date_added}. +
{$content}
+ +EOD; + } +} \ No newline at end of file -- cgit v1.2.3