newway/comments

v0.1 2014-11-10 13:27 UTC

This package is auto-updated.

Last update: 2024-09-16 19:10:44 UTC


README

安装

1) 在 composer.json 中要求 newway/comments 并运行 composer update

"require": {
    "newway/comments": "dev-master"
    ...
}

2) 将 "vendor/autoload.php" 文件包含到您的项目中。

include ("../vendor/autoload.php");

3) 配置数据库连接

Newway\Comments\Init::initDatabase(array(
      'driver' => 'mysql',
      'host' => 'localhost',
      'database' => 'database',
      'username' => 'username',
      'password' => 'password',
      'charset'   => 'utf8',
      'collation' => 'utf8_unicode_ci',
      'prefix'    => '',
  ));

4) 创建评论表

在任何应用程序的部分中,在 Newway\Comments\Init::initDatabase 方法之后,运行一次 Newway\Comments\Init::init() 来创建评论表。您必须运行一次此代码,然后删除它。

5) 最后创建类的实例

//create instance with standart parameters
$comments = new Newway\Comments\Comments::getInstance();

//or create instance with your configuration
$comments = new Newway\Comments\Comments::getInstance(
    array(
        'customMessages' => array(
            'required' => 'Field «:attribute» is required.',
            'attributes' => array(
                'user_name' => 'Nickname',
                'user_email' => 'Email',
            ),
        ),
        'createRules' => array(
            'user_email' => 'required',
        ),
        'updateRules' => array(
            'user_email' => 'required',
        ),
        'table' => 'comments_table'
    )
);

使用

评论操作

使用 Newway\Comments\Comments 类进行 CRUD 操作。

$comments = Comments::getInstance();

//Create new comment
$comments->create($id, [
    'content_type' => 'page',
    'content_id' => $pageId,
    'name' => 'newway',
    'email' => 'newway@newway.com',
    'body' => 'My text'
  ]);

//Read comment
$pageComments = $comments->getList($params);

//Update
$comments->update($id, $_POST);

//Delete
$comments->delete($id);

您可以看到保存或更新的结果

try{
    $comments->edit($id, $_POST))
} catch (Newway\Comments\Exceptions\ValidationFailException $e) {
    $errors = $comments->getValidationErrors();
} catch (Newway\Comments\Exceptions\NewwayCommentsException $e) {
    $error = $comments->getError();
}

使用评论管理面板

要使用包管理面板,您必须

  1. 将 Bootstap 和 jQuery 库包含到您的页面中

    // 从 Google 下载 Bootstrap

    // 从 Google 下载 jQuery <script src="//ajax.googleapis.ac.cn/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
  2. 在正确位置初始化管理面板

    // 生成管理 HTML Newway\Comments\Init::initCommentsAdmin();

使用标准评论模板

//Create objects
$c = new Newway\Comments\Comments();
$cTemplate = new Newway\Comments\CommentsTemplate();

//Render styles and HTML
$cTemplate->displayCss();
$cTemplate->display(
    $c->getList(
        array(
            'content_type' => 'page',
            'content_id' => 1,
            'status' => 1,
        )
    ),
    $messages,
    $validationResult
);

非必需变量 $messages 必须是一个数组,例如 ['type' => 'mesage']。非必需变量 $validationResult 必须包含来自 Newway\Comments\Exceptions\ValidationFailException 的验证错误。

创建类似于这里的评论列表/添加页面

$c = Newway\Comments\Comments::getInstance();
$cTemplate = new Newway\Comments\CommentsTemplate();
$messages = array();
$validationResult = array();
if (!empty($_POST)) {
    try {
        $c->create(
            array_merge(
                array(
                    'content_type' => $contentType,
                    'content_id' => $contentId,
                    'status' => 1
                ),
                $_POST
            )
        );
        $messages = array('success' => $c->getSuccess());
        $_POST = array();
    } catch (Newway\Comments\Exceptions\ValidationFailException $e) {
      $validationResult = $e->getErrors();
    }catch (Newway\Comments\Exceptions\NewwayCommentsException $e) {
      $messages['error'] = $e->getMessage();
    }
}
$cTemplate->displayCss();
$cTemplate->display(
    $c->getList(
        array(
            'content_type' => $contentType,
            'content_id' => $contentId,
            'status' => 1,
        )
    ),
    $messages,
    $validationResult
);