dev-master
9999999-dev
MIT
The Requires
by Zimm
v0.1
0.1.0.0
MIT
The Requires
by Zimm
Wallogit.com
2017 © Pedro Peláez
1) Require newway/comments in composer.json and run composer update., (*1)
"require": {
"newway/comments": "dev-master"
...
}
2) Include "vendor/autoload.php" file to your project., (*2)
include ("../vendor/autoload.php");
3) Configurate database conection:, (*3)
Newway\Comments\Init::initDatabase(array(
'driver' => 'mysql',
'host' => 'localhost',
'database' => 'database',
'username' => 'username',
'password' => 'password',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
));
4) Create comments table:, (*4)
Run Newway\Comments\Init::init() once in any part of application, after Newway\Comments\Init::initDatabase method, to create comments table. You must run once this code, and delete it., (*5)
5) Finaly create an instance of the class, (*6)
//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'
)
);
Operations with comments, (*7)
Use Newway\Comments\Comments class for CRUD operations., (*8)
$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);
You can see saving or updating results:, (*9)
try{
$comments->edit($id, $_POST))
} catch (Newway\Comments\Exceptions\ValidationFailException $e) {
$errors = $comments->getValidationErrors();
} catch (Newway\Comments\Exceptions\NewwayCommentsException $e) {
$error = $comments->getError();
}
Using comments admin panel, (*10)
To use package admin panel you must:, (*11)
1) Include Bootstap and jQuery libraries to your page:, (*12)
//Bootstrap from Google <link href="dist/css/bootstrap.min.css" rel="stylesheet"> <link href="dist/css/bootstrap-theme.min.css" rel="stylesheet"> <link href="dist/js/bootstrap.min.js" rel="stylesheet"> //jQuery from Google <script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
2) Init admin panel in the right place:, (*13)
//Generate admin HTML Newway\Comments\Init::initCommentsAdmin();
Using standard comments template, (*14)
//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
);
Not required variable $messages must be an array like ['type' => 'mesage']. Not required variable $validationResult must contain validation errors from Newway\Comments\Exceptions\ValidationFailException., (*15)
Create comments list/add page like here:, (*16)
$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
);
MIT
MIT