Show subroutines.php syntax highlighted
<?php
class CommentsModule extends Module {
const NEWEST_FIRST = 1;
const OLDEST_FIRST = 2;
const APPROVED = 1;
const NOT_APPROVED = 0;
public function addComment($id, $base, $instance, $comment, $name) {
$TA = new TA();
$TA->addQuery(TA :: SELECT, "xcs_module_comments_settings");
$TA->addParam("value");
$TA->WHERE("name='approve'");
$TA->LIMIT(1);
$TA->Execute();
$object = $TA->Result()->FetchObject();
$TA->removeQuery();
$approve = $object->value;
$ip = $_SERVER['REMOTE_ADDR'];
$host = gethostbyaddr($ip);
$date = Date :: NOW();
$TA->addQuery(TA :: INSERT, "xcs_module_comments");
$TA->addParam("text", TA :: STRING, $comment);
$TA->addParam("user_id", TA :: NUMBER, User::$profile->id);
$TA->addParam("name", TA :: STRING, $name);
$TA->addParam("email", TA :: STRING, '');
$TA->addParam("website", TA :: STRING, '');
$TA->addParam("time", TA :: STRING, $date);
$TA->addParam("host", TA :: STRING, $host);
$TA->addParam("ip", TA :: STRING, $ip);
$TA->addParam("base", TA :: STRING, $base);
$TA->addParam("instance", TA :: STRING, $instance);
$TA->addParam("object", TA :: NUMBER, $id);
if (String::isRude($comment))
$TA->addParam("approved", TA :: NUMBER, 0);
else {
if ($approve == 0 || $base == 'shoutbox')
$approved = 1;
else
$approved = 0;
$TA->addParam("approved", TA :: NUMBER, $approved);
}
$TA->Execute();
$TA->End();
return $date;
}
public function getComment($id) {
$TA = new TA();
$TA->addQuery(TA :: SELECT, "xcs_module_comments");
$TA->ORDER("id", TA :: DESCENDING);
$TA->WHERE("`approved`='1' AND `id`='" . $id . "'");
$TA->Execute();
$object = $TA->Result()->FetchObject();
if (!$object)
return false;
$object->email = String :: parseEmail($object->email);
return $object;
}
public function readComments($object_id, $base, $instance = false, $order = self :: OLDEST_FIRST, $limit = 0) {
if ($instance === false && is_module($base)) {
$instance = $base->getInstance();
$base = $base->getBase();
}
$TA = new TA();
$TA->addQuery(TA :: SELECT, "xcs_module_comments");
$TA->ORDER("id", TA :: DESCENDING);
if (Session :: isValid() && User :: isAuthor()) {
$TA->WHERE("`object`='" . $object_id . "' AND `base`='" . $base . "' AND `instance`='" . $instance . "'");
} else {
$TA->WHERE("`approved`='1' AND `object`='" . $object_id . "' AND `base`='" . $base . "' AND `instance`='" . $instance . "'");
}
$TA->LIMIT($limit);
$TA->Execute();
$retval = array ();
while ($object = $TA->Result()->FetchObject()) {
$object->email = String :: parseEmail($object->email);
$retval[] = $object;
}
$TA->End();
if ($order == self :: OLDEST_FIRST) {
$retval = array_reverse($retval);
}
return $retval;
}
public function numComments($id, $base, $instance, $approved = self :: APPROVED) {
$TA = new TA();
$TA->addQuery(TA :: SELECT, "xcs_module_comments");
$TA->addParam("id");
$TA->WHERE("`approved`='" . (int) $approved . "' AND `object`='" . $id . "' AND `base`='" . $base . "' AND `instance`='" . $instance . "'");
$TA->Execute();
$num = $TA->Result()->NumRows();
$TA->End();
return $num;
}
public function getNotApprovedComments() {
global $XCS;
$TA = new TA();
$TA->addQuery(TA :: SELECT, "xcs_module_comments");
$TA->WHERE("`approved`='" . (int) self :: NOT_APPROVED . "'");
$TA->Execute();
$comments = array ();
while ($object = $TA->Result()->FetchObject()) {
$object->module = $XCS->getModuleNick($object->instance);
$comments[] = $object;
}
$TA->End();
return $comments;
}
public function getNotApprovedCommentsNum() {
$TA = new TA();
$TA->addQuery(TA :: SELECT, "xcs_module_comments");
$TA->addParam("id");
$TA->WHERE("`approved`='" . (int) self :: NOT_APPROVED . "'");
$TA->Execute();
$num = $TA->Result()->NumRows();
$TA->End();
return $num;
}
public function showAllComments($instance, $article) {
Engine :: VerifyUser(); // Verify User (spambot protection)
$tab = new TinyAjaxBehavior(true);
$comments = $this->readComments($article, 'articles', $instance);
foreach ($comments as $comment) {
$tab->add(TabInnerHtml :: getBehavior('comment_id_' . $comment->id, $comment->text));
}
return $tab->getString();
}
public function showComment($id) {
Engine :: VerifyUser(); // Verify User (spambot protection)
$comment = $this->getComment($id);
if ($comment) {
Ajax :: InnerHTMLe('comment_id_' . $id, $comment->text);
}
return Ajax :: ReturnValue(true);
}
public function onSend() {
Engine :: VerifyUser(); // Verify User (spambot protection)
$formData = Ajax :: getPostData();
$date = $this->addComment($formData['id'], $formData['base'], $formData['instance'], $formData['text'], $formData['name']);
try {
Ajax :: Action(TabCallFunction :: getBehavior('commentAdded', Date :: format($date)));
Renderer::FlushAjax();
} catch (NoAjaxException $e) {
}
}
public function showForm($id, $currentModule) {
global $ECP;
$cssid = "_default";
require ("modules/comments/templates/default/form.php");
}
public function showAdmin($comment) {
if (Session :: isValid() && User :: isAuthor()) {
Renderer :: setFlag('_currentModule', Renderer :: getFlag('currentModule'));
Renderer :: setFlag('currentModule', $this);
require ("modules/comments/templates/default/admin.php");
Renderer :: setFlag('currentModule', Renderer :: getFlag('_currentModule'));
}
}
function onAdminDelete() {
$TA = new TA();
$TA->addQuery(TA :: DELETE, "xcs_module_comments");
$TA->WHERE("`id`='" . $_GET['id'] . "'");
$TA->LIMIT(1);
$TA->Execute();
$TA->End();
try {
// AJAX PART
// Set empty content to comment = delete it from the site.
Ajax :: innerHTML('comment_id_' . $_GET['id'], '');
// Send content to user.
Renderer::FlushAJAX();
} catch (NoAjaxException $e) {
// DEGRADED PART
Location :: Redirect("index.php");
}
}
function onAdminChangeApprove() {
if ($_GET['approved'] == 1) {
$approved = 0;
} else {
$approved = 1;
}
$TA = new TA();
$TA->addQuery(TA :: UPDATE, "ecp_module_comments");
$TA->addParam("approved", TA :: NUMBER, $approved);
$TA->WHERE("`id`='" . $_GET['id'] . "'");
$TA->LIMIT(1);
$TA->Execute();
$TA->End();
try {
// AJAX PART
// We need to set a parentModule, so we'll know in which module to search for comments.
Engine :: setFlag('parentModule', ModulesManager :: loadModule($_GET['object']));
// Renders onDefault() method of $this with parameter $_GET['objectid'].
Renderer :: renderDefaultState($this, $_GET['objectid']);
// Send only the new content to user.
Renderer::FlushAJAX();
} catch (NoAjaxException $a) {
// DEGRADED PART
Location :: Redirect("index.php");
}
}
function onAdmin() {
$comments = $this->getNotApprovedComments();
$currentModule->addContent("content_admin.php");
$currentModule->registerVariable($comments, "comments");
}
function onDefault($id = false) {
$parentModule = Engine :: getFlag('parentModule');
if (!$id || !$parentModule)
return false;
$comments = $this->readComments($id, $parentModule);
$this->addContent('content.php');
$this->registerVariable($comments, 'comments');
$this->registerVariable($id, 'id');
$this->registerVariable($parentModule, 'parentModule');
}
}
?>
See more files for this project here