View source
<?php
namespace Drupal\dynamic_banner\application\Controller;
use Drupal\file\Entity\File;
use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\Link;
use Drupal\Core\Url;
use Drupal\Core\Database\Query\TableSortExtender;
class CdbController extends ControllerBase {
function adminListPage() {
$output = [];
$header = array(
array(
'data' => t('Url'),
'field' => 'd.path',
'sort' => 'asc',
),
array(
'data' => t('ImgUrl'),
),
array(
'data' => t('Text'),
'field' => 'd.text',
),
array(
'data' => t('Link'),
'field' => 'd.link',
),
array(
'data' => t('Mode'),
'field' => 'd.mode',
),
array(
'data' => t('Operations'),
'colspan' => '2',
),
);
$query = \Drupal::database()
->select('dynamic_banner', 'd')
->extend('\\Drupal\\Core\\Database\\Query\\PagerSelectExtender')
->extend('\\Drupal\\Core\\Database\\Query\\TableSortExtender');
$query = $query
->fields('d', array(
'dbid',
'path',
'imgurl',
'imgfid',
'text',
'link',
'mode',
));
$query = $query
->limit(10)
->orderByHeader($header);
$result = $query
->execute();
$rows = array();
foreach ($result as $data) {
$editUrl = Url::fromRoute('cdb.banneredit', array(
'bid' => $data->dbid,
));
$editLink = Link::fromTextAndUrl(t('Edit'), $editUrl);
$delUrl = Url::fromRoute('cdb.bannerdel', array(
'bid' => $data->dbid,
));
$delLink = Link::fromTextAndUrl(t('Delete'), $delUrl);
$image = $this
->dynamic_banner_image_handler($data->imgurl, $data->imgfid);
$rows[] = array(
'data' => array(
$data->path,
$image,
$data->text,
$data->link,
$data->mode,
$editLink,
$delLink,
),
);
}
$output['dynamic_banner_table'] = array(
'#theme' => 'table',
'#header' => $header,
'#rows' => $rows,
'#empty' => t('No Banners Found.'),
);
$output['dynamic_banner_pager'] = array(
'#type' => 'pager',
);
return $output;
}
function dynamic_banner_load_banner($dbid) {
$query = \Drupal::database()
->select('dynamic_banner', 'd');
$query
->condition('d.dbid', $dbid, '=')
->fields('d');
$result = $query
->execute()
->fetchObject();
if ($result) {
return $result;
}
return NULL;
}
function dynamic_banner_find_load_default() {
$query = \Drupal::database()
->select('dynamic_banner', 'd');
$query
->condition('d.path', 'DEFAULT', '=')
->fields('d');
$result = $query
->execute()
->fetchObject();
if ($result) {
return $result;
}
$blank_banner = new stdClass();
$blank_banner->dbid = 0;
$blank_banner->path = 'DEFAULT';
$blank_banner->imgurl = '';
$blank_banner->mode = 'normal';
$blank_banner->text = '';
$blank_banner->link = '';
$blank_banner->imgfid = '';
return $blank_banner;
}
public function dynamic_banner_image_handler($imgurl, $imgfid) {
if ($imgurl && $imgurl != '') {
return $imgurl;
}
else {
if (strrpos($imgfid, ',')) {
$all_fids = explode(",", $imgfid);
$all_files = File::loadMultiple((int) $all_fids);
$retval = "";
if (isset($all_files)) {
foreach ($all_files as $file) {
if ($retval != "") {
$retval .= ",";
}
if (isset($file)) {
$fileUrl = $file
->getFileUri();
}
else {
$fileUrl = '';
}
$retval .= str_replace('public://', '/', $fileUrl);
}
}
return $retval;
}
else {
$file = File::load((int) $imgfid);
if (isset($file)) {
$fileUrl = $file
->getFileUri();
}
else {
$fileUrl = '';
}
$file_path = str_replace('public://', '/', $fileUrl);
return $file_path;
}
}
}
}