RE: Creating a module
04-06-2015, 06:42 AM
(This post was last modified: 04-06-2015, 06:42 AM by rafaht.)
TGates,
controller.ext.php:
module.zpm:
This module is at initial development and piece of code isn't added... I'm trying to do work the basics do add the complex of this.
Thanks,
controller.ext.php:
Code:
<?php
/*
*
* Alerts module for Sentora 1.0.0
* Version : 1
* Author : Rafael (rafaht @ Sentora Forums)
* Email : rafael@rafatresbach.com
*/
class module_controller {
static $done;
static $error;
static $emptytitle;
static $emptymessage;
static $ok;
/* Load CSS and JS files */
static function getInit() {
global $controller;
$line = '<link rel="stylesheet" type="text/css" href="modules/' . $controller->GetControllerRequest('URL', 'module') . '/assets/alert.css">';
$line .= '<script type="text/javascript" src="modules/' . $controller->GetControllerRequest('URL', 'module') . '/assets/alert.js"></script>';
return $line;
}
/*
* The 'worker' methods.
*/
static function ListAlerts() {
global $zdbh;
$sql = "SELECT * FROM sentora_alerts";
$numrows = $zdbh->prepare($sql);
$numrows->execute();
if ($numrows->fetchColumn() <> 0) {
$sql = $zdbh->prepare($sql);
$res = array();
$sql->execute();
while ($rowalerts = $sql->fetch()) {
array_push($res, array('alert_id' => $rowalerts['alert_id'], 'alert_title' => runtime_xss::xssClean($rowalerts['alert_title']), 'alert_message' =>runtime_xss::xssClean($rowalerts['alert_message'])));
}
return $res;
} else {
return false;
}
}
/*
* End 'worker' methods.
*/
static function getAlertsList() {
global $zdbh;
return self::ListAlerts();
}
static function CheckAlertsForErrors($title, $message) {
global $zdbh;
if (strlen(trim($title)) < 3) {
self::$emptytitle = true;
return false;
}
if (strlen(trim($message)) < 25) {
self::$emptymessage = true;
return false;
}
return true;
}
static function getCSFR_Tag() {
return runtime_csfr::Token();
}
static function getModuleName() {
$module_name = ui_module::GetModuleName();
return $module_name;
}
static function getModuleIcon() {
global $controller;
$module_icon = "/modules/" . $controller->GetControllerRequest('URL', 'module') . "/assets/icon.png";
return $module_icon;
}
static function getModuleDesc() {
$message = ui_language::translate(ui_module::GetModuleDescription());
return $message;
}
static function getAlertTitle(){
global $controller;
if ($controller->GetControllerRequest('FORM', 'alert_title') && (fs_director::CheckForEmptyValue(self::$done))){
return $controller->GetControllerRequest('FORM', 'alert_title');
} else {
return "";
}
}
static function getAlertMessage(){
global $controller;
if ($controller->GetControllerRequest('FORM', 'alert_message') && (fs_director::CheckForEmptyValue(self::$done))){
return $controller->GetControllerRequest('FORM', 'alert_message');
} else {
return "";
}
}
static function doAddAlert(){
global $controller;
runtime_csfr::Protect();
$currentuser = ctrl_users::GetUserDetail();
$formvars = $controller->GetAllControllerRequests('FORM');
if (self::ExecuteAlerts($formvars)){
self::$ok = true;
return true;
} else {
return false;
}
return;
}
static function ExecuteAlerts($data) {
global $zdbh;
$retval = false;
if (!fs_director::CheckForEmptyValue(self::CheckAlertsForErrors($data['alert_title'], $data['alert_message']))){
$currentuser = ctrl_users::GetUserDetail();
$sql = $zdbh->prepare("INSERT INTO sentora_alerts (alert_title, alert_message, alert_date) VALUES (:title,:message,:date)");
$sql->bindParam(':title', $data['alert_title']);
$sql->bindParam(':message', $data['alert_message']);
$datetime = date("Y-m-d H:i");
$sql->bindParam(':date', $datetime);
$sql->execute();
self::$done = true;
$retval = true;
}
return $retval;
}
static function getResult() {
if (!fs_director::CheckForEmptyValue(self::$emptytitle)) {
return ui_sysmessage::shout(ui_language::translate("Please enter a valid alert title and try again."), "zannounceerror");
}
if (!fs_director::CheckForEmptyValue(self::$emptymessage)) {
return ui_sysmessage::shout(ui_language::translate("Alert message must be characters greater than 25."), "zannounceerror");
}
if (!fs_director::CheckForEmptyValue(self::$ok)) {
return ui_sysmessage::shout(ui_language::translate("Alert added successfully."), "zannounceok");
}
if (!fs_director::CheckForEmptyValue(self::$error)) {
return ui_sysmessage::shout(ui_language::translate("An error has occurred while executing your request, please check your input and try again."), "zannounceerror");
}
return;
}
/*
* Webinterface sudo methods.
*/
}
?>
module.zpm:
Code:
<@ Init @>
<div class="zmodule_content panel" id="zmodule_header_<@ ModuleName @>">
<div class="panel-heading">
<img src="<@ ModuleIcon @>" width="35" height="35" border="0" alt="<@ ModuleName @>">
<div class="zmodule_title">
<@ ModuleName @>
</div>
<div class="zmodule_desc" id="zmodule_desc_<@ ModuleName @>"><@ ModuleDesc @></div>
</div>
<div class="zgrid_wrapper">
<h3><: Alerts list :></h3>
<br><br>
<h3><: Add new alert :></h3>
<@ Result @>
<div id="alerts_msg" class="alert alert-danger" style="display: none;"></div>
<form action="./?module=alerts&action=AddAlert" method="post" id="frm_alerts">
<table class="table table-stripped">
<tr>
<th nowrap="nowrap"><: Title :>:</th>
<td><input name="alert_title" type="text" id="alert_title" size="30" value="<@ AlertTitle @>" /></td>
</tr>
<tr>
<th nowrap="nowrap"><: Alert Message :>:</th>
<td>
<textarea name="alert_message" id="alert_message" rows="3" cols="80" class="ckeditor" /><@ AlertMessage @></textarea>
</td>
</tr>
<tr>
<th></th>
<td>
<button class="btn btn-primary" type="button" name="AddAlert" value="1" id="btn_addalert">Add alert</button>
</td>
</tr>
</table>
<@ CSFR_Tag @>
</form>
</div>
</div>
<script>
var form_msgs = [];
form_msgs['empty_alert_title'] = "<: Alert title cannot be less than 3 characters :>!";
form_msgs['empty_alert_message'] = "<: Alert message cannot be less than 25 characters :>!";
</script>
This module is at initial development and piece of code isn't added... I'm trying to do work the basics do add the complex of this.
Thanks,