219 lines
4.8 KiB
PHP
Executable File
219 lines
4.8 KiB
PHP
Executable File
<?php
|
||
|
||
declare(strict_types=1);
|
||
|
||
namespace IR\App\Controllers;
|
||
|
||
# core
|
||
use IR\Core\Application as Application;
|
||
|
||
# mvc
|
||
use IR\Mvc\Controller as Controller;
|
||
|
||
# helpers
|
||
use IR\App\Helpers\Authentication as Authentication;
|
||
use IR\App\Helpers\DataTable;
|
||
use IR\App\Helpers\Page as Page;
|
||
use IR\App\Helpers\Permissions as Permissions;
|
||
use IR\App\Models\Admin\Domain;
|
||
use IR\App\Models\Admin\MtaServer;
|
||
use IR\App\Models\Admin\ShortLink;
|
||
# exceptions
|
||
use IR\Exceptions\Types\PageException as PageException;
|
||
use IR\Http\Request;
|
||
use IR\Logs\Logger;
|
||
|
||
if (!defined('IR_START')) exit('<pre>No direct script access allowed</pre>');
|
||
|
||
class ShortLinks extends Controller
|
||
{
|
||
/**
|
||
* @app
|
||
* @readwrite
|
||
*/
|
||
protected $app;
|
||
|
||
/**
|
||
* @app
|
||
* @readwrite
|
||
*/
|
||
protected $authenticatedUser;
|
||
|
||
/**
|
||
* @name init
|
||
* @description initializing process before the action method executed
|
||
* @once
|
||
* @protected
|
||
*/
|
||
public function init()
|
||
{
|
||
# set the current application to a local variable
|
||
$this->app = Application::getCurrent();
|
||
|
||
# connect to the database
|
||
$this->app->database('system')->connect();
|
||
|
||
# check for authentication
|
||
if (!Authentication::isUserAuthenticated()) {
|
||
Page::redirect($this->app->http->request->getBaseURL() . RDS . 'auth' . RDS . 'login.' . DEFAULT_EXTENSION);
|
||
}
|
||
|
||
# check users roles
|
||
Authentication::checkUserRoles();
|
||
|
||
# get the authenticated user
|
||
$this->authenticatedUser = Authentication::getAuthenticatedUser();
|
||
}
|
||
|
||
/**
|
||
* @name add
|
||
* @description the add action
|
||
* @before init
|
||
* @after closeConnections,checkForMessage
|
||
*/
|
||
public function add()
|
||
{
|
||
# check for permissions
|
||
$access = Permissions::customcheckForAuthorization($this->authenticatedUser, __CLASS__, "add");
|
||
|
||
if ($access == false) {
|
||
throw new PageException('Access Denied !', 403);
|
||
}
|
||
|
||
# set menu status
|
||
$this->masterView->set([
|
||
'tools' => 'true',
|
||
'short_links' => 'true',
|
||
'add_short_link' => 'true'
|
||
]);
|
||
|
||
# set data to the page view
|
||
$this->pageView->set([
|
||
'domains' => Domain::all(Domain::FETCH_ARRAY,['status = ?',['Activated']],['id','value'],'id','DESC'),
|
||
]);
|
||
}
|
||
|
||
/**
|
||
* @name main
|
||
* @description list short links
|
||
* @before init
|
||
* @after closeConnections,checkForMessage
|
||
*/
|
||
public function main()
|
||
{
|
||
# check for permissions
|
||
$access = Permissions::customcheckForAuthorization($this->authenticatedUser, __CLASS__, "main");
|
||
|
||
if ($access == false) {
|
||
throw new PageException('Access Denied !', 403);
|
||
}
|
||
|
||
# preparing the columns array to create the list
|
||
$columnsArray = [
|
||
'id',
|
||
'url',
|
||
'value',
|
||
'domain_name',
|
||
'created_by',
|
||
'created_date',
|
||
];
|
||
|
||
# creating the html part of the list
|
||
$columns = Page::createTableHeader($columnsArray);
|
||
$filters = Page::createTableFilters($columnsArray);
|
||
|
||
# set menu status
|
||
$this->masterView->set([
|
||
'tools' => 'true',
|
||
'short_links' => 'true',
|
||
'list_short_links' => 'true'
|
||
]);
|
||
|
||
# set data to the page view
|
||
$this->pageView->set([
|
||
'columns' => $columns,
|
||
'filters' => $filters
|
||
]);
|
||
}
|
||
|
||
|
||
/**
|
||
* @name get
|
||
* @description the get action
|
||
* @before init
|
||
* @after closeConnections
|
||
*/
|
||
public function get()
|
||
{
|
||
# check for permissions
|
||
$access = Permissions::customcheckForAuthorization($this->authenticatedUser, __CLASS__, 'get');
|
||
|
||
if ($access == false) {
|
||
throw new PageException('Access Denied !', 403);
|
||
}
|
||
|
||
# get post data
|
||
$data = $this->app->http->request->retrieve(Request::ALL, Request::POST);
|
||
|
||
if (count($data)) {
|
||
$url = $this->app->http->request->getBaseURL();
|
||
|
||
# preparing the columns array to create the list
|
||
$columns = [
|
||
'id',
|
||
'url',
|
||
'value',
|
||
'domain_name',
|
||
'created_by',
|
||
'created_date'
|
||
];
|
||
|
||
# fetching the results to create the ajax list
|
||
die(json_encode(DataTable::init($data, 'admin.short_links s', $columns, new ShortLink(), 'short-links', 'DESC', null, false)));
|
||
}
|
||
}
|
||
|
||
/**
|
||
* @name delete
|
||
* @description creates a query object, only if the primary key property value is not empty, and executes the query’s delete() method.
|
||
* @access public
|
||
* @return integer
|
||
*/
|
||
public function delete() : int
|
||
{
|
||
# check for permissions
|
||
$access = Permissions::customcheckForAuthorization($this->authenticatedUser, __CLASS__, 'delete');
|
||
|
||
if ($access == false) {
|
||
throw new PageException('Access Denied !', 403);
|
||
}
|
||
|
||
return parent::delete();
|
||
}
|
||
|
||
|
||
/**
|
||
* @name closeConnections
|
||
* @description close all connections
|
||
* @once
|
||
* @protected
|
||
*/
|
||
public function closeConnections()
|
||
{
|
||
# connect to the database
|
||
$this->app->database('system')->disconnect();
|
||
$this->app->database('clients')->disconnect();
|
||
}
|
||
|
||
/**
|
||
* @name checkForMessage
|
||
* @description checks for session messages
|
||
* @once
|
||
* @protected
|
||
*/
|
||
public function checkForMessage()
|
||
{
|
||
# check for message
|
||
Page::checkForMessage($this);
|
||
}
|
||
} |