How bootstrap.widgets.TbGridView pagination can be changed as the image pasted below? - yii

My widget is:-
$this->widget('bootstrap.widgets.TbGridView',array(
'type'=>'striped bordered condensed',
'id'=>'accounts-grid',
'dataProvider'=>$model->search(),
'columns'=>array(
array(
'name'=>'id',
'header'=>'ID',
'type'=>'raw',
'value'=>'$data->id',
'htmlOptions'=>array('width'=>'40'),
'url'=>'Yii::app()->createUrl("/meetings/view", array("id"=>$data["id"]))',
),
'name',
'phone',
'mobile',
'type',
'account_manager',
'status',
array(
'class'=>'bootstrap.widgets.TbButtonColumn',
),
),
));
And I need this type of pagination for this widget.
Please help me if you have any ideas.

Create a php file CustomPager.php and put it into components folder
<?php
class CustomPager extends CLinkPager {
/**
* #var string the text shown before page buttons. Defaults to ''.
*/
public $header = '';
/**
* #var string the URL of the CSS file used by this pager.
* Defaults to false, meaning that no CSS will be included.
*/
public $cssFile = false;
/**
* #var boolean whether to display the first and last items.
*/
public $displayFirstAndLast = false;
/**
* Initializes the pager by setting some default property values.
*/
public function init() {
if ($this->nextPageLabel === null)
$this->nextPageLabel = Yii::t('bootstrap', 'Next') . ' →';
if ($this->prevPageLabel === null)
$this->prevPageLabel = '← ' . Yii::t('bootstrap', 'Previous');
if ($this->firstPageLabel === null)
$this->firstPageLabel = Yii::t('bootstrap', 'First');
if ($this->lastPageLabel === null)
$this->lastPageLabel = Yii::t('bootstrap', 'Last');
if (!isset($this->htmlOptions['class']))
$this->htmlOptions['class'] = ''; // would default to yiiPager
parent::init();
}
/**
* Creates the page buttons.
* #return array a list of page buttons (in HTML code).
*/
protected function createPageButtons() {
$totalpages = $this->getPageCount();
if (($pageCount = $this->getPageCount()) <= 1)
return array();
list ($beginPage, $endPage) = $this->getPageRange();
$currentPage = $this->getCurrentPage(false); // currentPage is calculated in getPageRange()
$buttons = array();
/**
* first page
*/
if ($this->displayFirstAndLast)
$buttons[] = $this->createPageButton($this->firstPageLabel, 0, 'first', $currentPage <= 0, false);
/**
* previous page
*/
if (($page = $currentPage - 1) < 0)
$page = 0;
$buttons[] = $this->createPageButton($this->prevPageLabel, $page, 'previous', $currentPage <= 0, false);
/**
* internal page
*/
$buttons[] = '<li class=""><a herf="">Current Page: '.($currentPage+1).' of ' . $totalpages .' </a><li>';
/**
* next page
*/
if (($page = $currentPage + 1) >= $pageCount - 1)
$page = $pageCount - 1;
$buttons[] = $this->createPageButton($this->nextPageLabel, $page, 'next', $currentPage >= ($pageCount - 1), false);
/**
* last page
*/
if ($this->displayFirstAndLast)
$buttons[] = $this->createPageButton($this->lastPageLabel, $pageCount - 1, 'last', $currentPage >= ($pageCount - 1), false);
return $buttons;
}
/**
* Creates a page button.
* You may override this method to customize the page buttons.
* #param string $label the text label for the button
* #param integer $page the page number
* #param string $class the CSS class for the page button. This could be 'page', 'first', 'last', 'next' or 'previous'.
* #param boolean $hidden whether this page button is visible
* #param boolean $selected whether this page button is selected
* #return string the generated button
*/
protected function createPageButton($label, $page, $class, $hidden, $selected) {
if ($hidden || $selected)
$class .= ' ' . ($hidden ? 'disabled' : 'active');
return CHtml::tag('li', array('class' => $class), CHtml::link($label, $this->createPageUrl($page)));
}
}
?>
and call class from widget grid view
'pager'=>array(
'class'=>'CustomPager',
),

Related

Behat ensure that select field has these options

With Behat how can I make sure that a select field contains a given set of options?
I can't see any core methods for checking this.
Add the following to your FeatureContext.php:
/**
* #Then /^the select field "([^"]*)" should have a list containing:$/
*
* #param $locator
* string $locator input id, name or label
* #param \Behat\Gherkin\Node\PyStringNode $list
* A list of options that should be present.
*/
public function shouldHaveAListContaining($locator, PyStringNode $list): void {
$session = $this->getSession();
$page = $session->getPage();
$element = $page->findField($locator);
if ($element === NULL) {
throw new \InvalidArgumentException(sprintf('Could find element "%s".', $locator));
}
$options = [];
foreach ($element->findAll('css', 'option') as $option) {
$options[] = $option->getText();
}
$missing = array_diff($list->getStrings(), $options);
if (count($missing) > 0) {
$context = [$locator, implode(', ', $missing)];
throw new \RuntimeException(vsprintf('Element "%s" is missing these options "%s"', $context));
}
}
And the call it like this:
And the select field "YOUR_LABEL" should have a list containing:
"""
Option 1
Option 2
"""

Prestashop 1.7 - City field as dropdown or autocomplete

I am using Prestashop 1.7 and I need to change the city input field.
I have a billing software that only allows me to use predefined cities. So I have created a table ps_cities with the entries (id an city name).
I know how to write a dropdown or a autocomplete script, but I do not know where to change the input type in the Prestashop files.
On the 1.6 version you have the input field in a theme file, but somehow I fail to find in the new version.
In PrestaShop 1.7.7.X I've created a module that includes some new (and cool!) hooks like showing below. I consider this one a good option because it will be easier to maintain in the next PrestaShop releases.
Some assumptions here: I created a relationship model CityAddress with two fields id_city and id_address and a City model with fields like name, id_state, id_country, also I continued using Address::city string name for compatibility.
/**
* #see /classes/form/CustomerAddressFormatter.php#L156
* #param array $param [
* #var array $fields
* ]
*/
public function hookAdditionalCustomerAddressFields($params)
{
($params['fields']['city'])->setType('hidden');
// New field
$formField = $params['fields'];
$formField = (new FormField())
->setName('id_city')
->setLabel($this->l('City'))
->setRequired(true)
->setType('select')
;
// If an address already exits, select the default city
if (Tools::getIsset('id_address')) {
$address = new Address(Tools::getValue('id_address'));
if (!empty($address->id_state)) {
$cities = City::getCitiesByIdState((int) $address->id_state);
if (!empty($cities)) {
foreach ($cities as $city) {
$formField->addAvailableValue(
$city['id_city'],
$city['name']
);
}
$id_city = CityAddress::getIdCityByIdAddress((int) $address->id);
$formField->setValue($id_city);
}
}
}
// Add the id_city field in the position of the city field
$keys = array_keys($params['fields']);
$search = 'city';
foreach ($keys as $key => $value) {
if ($value == $search) {
break;
}
}
$part1 = array_slice($params['fields'], 0, $key + 1);
$part2 = array_slice($params['fields'], $key + 1);
$part1['id_city'] = $formField;
$params['fields'] = array_merge($part1, $part2);
}
This one to validate the field:
/**
* #see /classes/form/CustomerAddressForm.php#L123
* #param array $param [
* #var CustomerAddressForm $form
* ]
*/
public function hookActionValidateCustomerAddressForm($params)
{
if (empty(Tools::getValue('id_city'))
|| empty(Tools::getValue('city'))) {
return false;
}
$form = $params['form'];
$idCityField = $form->getField('id_city');
$idCity = (int) Tools::getValue('id_city');
$cityObj = new City($idCity);
$city = pSQL(Tools::getValue('city'));
if ($cityObj->name !== $city) {
$idCityField->addError(sprintf(
$this->l('Invalid name in field id_city %s and city %s'),
$cityObj->name,
$city
));
return false;
}
return true;
}
And the submitted field:
/**
* #see /classes/form/CustomerAddressForm.php#L153
* #param array $param [
* #var Address $address
* ]
*/
public function hookActionSubmitCustomerAddressForm($params)
{
/** #var Address */
$address = $params['address'];
$address->save();
if (!Validate::isLoadedObject($address)) {
throw new PrestaShopException($this->l('Address object error while trying to save city'));
}
// If address has a previous value then update it
$cityAddress = CityAddress::getCityAddressByIdAddress((int) $address->id);
$city = City::getCityByNameAndIdState($address->city, $address->id_state);
$cityAddress->id_city = $city->id;
$cityAddress->id_address = $address->id;
$cityAddress->save();
}
It is possible if you have this line in the additionalCustomerAddressFields hook:
https://github.com/PrestaShop/PrestaShop/blob/develop/classes/form/CustomerAddressFormatter.php#L150
For previous version I included ['fields' => &$format] as a parameter.
You can find all form fields of the Front Office in your theme's /templates/_partials/form-fields.tpl file

TYPO3 9 get resources and categories from pages, queryBuilder join problems

This solution was working til TYPO3 8.7. But breaks in TYPO3 9.
I've some submenu showing images from page-resources and using categories.descriptions for css.
I use custom viewhelpers for that. But now the JOIN-sql is broken... Don't know why, because it worked in 8.7 and previous.
<?php
namespace EnzephaloN\ThemePsoa\ViewHelper;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Core\Database\ConnectionPool;
class SysCategoryViewHelper extends \TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper {
/**
* #var integer $uid (CE)
*/
public function initializeArguments() {
$this->registerArgument('uid', 'integer', 'enthaelt die UID des CE', TRUE);
}
/**
* #var integer $uid
*/
public function render($uid = null) {
$queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('sys_category');
$queryBuilder->getRestrictions()
->removeAll();
$query = $queryBuilder
->select('*')
->from('sys_category')
->join('sys_category','sys_category_record_mm','MM', $queryBuilder->expr()->andX($queryBuilder->expr()->eq('MM.uid_foreign', $uid),$queryBuilder->expr()->eq('MM.uid_local', 'uid')))
->setMaxResults(1);
$result = $query->execute();
$res = [];
while ($row = $result->fetch()) {
$res[] = $row;
}
$this->templateVariableContainer->add('sysCategoryDetailArray', $res);
}
}
ERRORMESSAGE
(1/2) Doctrine\DBAL\Exception\SyntaxErrorException
An exception occurred while executing
SELECT * FROM sys_category INNER JOIN sys_category_record_mm MM ON
(`MM`.`uid_foreign` = ) AND (MM.uid_local = uid)
LIMIT 1: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ') AND (MM.uid_local = uid) LIMIT 1' at line 1
and for fal:
<?php
namespace EnzephaloN\ThemePsoa\ViewHelper;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Core\Database\ConnectionPool;
class FalResourceViewHelper extends \TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper {
/**
* #author INGENIUMDESIGN
*/
public function initializeArguments() {
$this->registerArgument('uid', 'integer', 'enthaelt die UID des CE', TRUE);
}
/**
* #var mixed $uid
*/
public function render($uid = null) {
$queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('sys_file');
if($uid) {
$query = $queryBuilder
->select('*')
->from('sys_file')
->join('sys_file', 'sys_file_reference', 'MM',
$queryBuilder->expr()->andX(
$queryBuilder->expr()->andX(
$queryBuilder->expr()->eq('MM.uid_foreign', $uid),
$queryBuilder->expr()->eq('MM.tablenames', '"pages"')
),
$queryBuilder->expr()->eq('MM.uid_local', 'sys_file.uid')
)
)
->setMaxResults(1);
//die($query->getSQL());
$result = $query->execute();
$res = [];
while ($row = $result->fetch()) {
$res[] = $row;
}
$this->templateVariableContainer->add('sysFileArray', $res);
}
}
}
Can anybody check those queryBuilder-statements?
Thanx in advance
EnzephaloN
Here's the solution:
FalResourceViewhelper
Just use the TYPO3-core-method $fileRepository->findByRelation(); This returns the page-related resources.
Use like this:
<e:falResource data="{page.data}" table="pages" field="media" as="sysFileArray">
<f:if condition="{sysFileArray.0.uid}!=0">
<f:image src="{sysFileArray.0.uid}" treatIdAsReference="1" class="img-responsive"/>
</f:if>
</e:falResouce>
<?php
namespace EnzephaloN\ThemePsoa\ViewHelper;
use TYPO3\CMS\Core\Resource\FileRepository;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;
use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractViewHelper;
use TYPO3Fluid\Fluid\Core\ViewHelper\Traits\CompileWithRenderStatic;
/**
* FalViewHelper
*/
class FalResourceViewHelper extends AbstractViewHelper{
use CompileWithRenderStatic;
/**
* #var bool
*/
protected $escapeOutput = false;
/**
* Initialize arguments.
*
* #throws \TYPO3Fluid\Fluid\Core\ViewHelper\Exception
*/
public function initializeArguments()
{
parent::initializeArguments();
$this->registerArgument('data', 'array', 'Data of current record', true);
$this->registerArgument('table', 'string', 'table', false, 'tt_content');
$this->registerArgument('field', 'string', 'field', false, 'image');
$this->registerArgument('as', 'string', 'Name of variable to create', false, 'items');
}
/**
* #param array $arguments
* #param \Closure $renderChildrenClosure
* #param RenderingContextInterface $renderingContext
* #return string
*/
public static function renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext) {
$variableProvider = $renderingContext->getVariableProvider();
if (is_array($arguments['data']) && $arguments['data']['uid'] && $arguments['data'][$arguments['field']]) {
$fileRepository = GeneralUtility::makeInstance(FileRepository::class);
$items = $fileRepository->findByRelation(
$arguments['table'],
$arguments['field'],
$arguments['data']['uid']
);
$localizedId = null;
if (isset($arguments['data']['_LOCALIZED_UID'])) {
$localizedId = $arguments['data']['_LOCALIZED_UID'];
} elseif (isset($arguments['data']['_PAGES_OVERLAY_UID'])) {
$localizedId = $arguments['data']['_PAGES_OVERLAY_UID'];
}
$isTableLocalizable = (
!empty($GLOBALS['TCA'][$arguments['table']]['ctrl']['languageField'])
&& !empty($GLOBALS['TCA'][$arguments['table']]['ctrl']['transOrigPointerField'])
);
if ($isTableLocalizable && $localizedId !== null) {
$items = $fileRepository->findByRelation($arguments['table'], $arguments['field'], $localizedId);
}
} else {
$items = null;
}
$variableProvider->add($arguments['as'], $items);
$content = $renderChildrenClosure();
$variableProvider->remove($arguments['as']);
return $content;
}
}
It's easy.
--
SysCategoryViewHelper works like this:
<e:sysCategory data="{page.data}" as="sysCategoryDetailArray">
<f:if condition="{sysCategoryDetailArray.0}">
<i class="fa fa-4x {sysCategoryDetailArray.0.description}"></i>
</f:if>
</e:sysCategory>
And here is the code:
<?php
namespace EnzephaloN\ThemePsoa\ViewHelper;
use TYPO3\CMS\Core\Resource\FileRepository;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;
use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractViewHelper;
use TYPO3Fluid\Fluid\Core\ViewHelper\Traits\CompileWithRenderStatic;
use TYPO3\CMS\Core\Database\ConnectionPool;
class SysCategoryViewHelper extends \TYPO3Fluid\Fluid\Core\ViewHelper\AbstractViewHelper {
use CompileWithRenderStatic;
/**
* #var bool
*/
protected $escapeOutput = false;
/**
* Initialize arguments.
*
* #throws \TYPO3Fluid\Fluid\Core\ViewHelper\Exception
*/
public function initializeArguments()
{
parent::initializeArguments();
$this->registerArgument('data', 'array', 'Data of current record', true);
$this->registerArgument('as', 'string', 'Name of variable to create', false, 'items');
}
/**
* #param array $arguments
* #param \Closure $renderChildrenClosure
* #param RenderingContextInterface $renderingContext
* #return string
*/
public static function renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext) {
$variableProvider = $renderingContext->getVariableProvider();
if (is_array($arguments['data']) && $arguments['data']['uid']) {
$queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('sys_category');
$queryBuilder->getRestrictions()
->removeAll();
$query = $queryBuilder
->select('*')
->from('sys_category')
->join('sys_category', 'sys_category_record_mm', 'MM', $queryBuilder->expr()->andX(
$queryBuilder->expr()->eq('MM.uid_foreign', $arguments['data']['uid']),
$queryBuilder->expr()->eq('MM.uid_local','sys_category.uid')));
$result = $query->execute();
$items = [];
while ($row = $result->fetch()) {
$items[] = $row;
}
} else {
$items = null;
}
$variableProvider->add($arguments['as'], $items);
$content = $renderChildrenClosure();
$variableProvider->remove($arguments['as']);
return $content;
}
}

How can i load model in joomla?

This is my Controller
// No direct access
defined( '_JEXEC' ) or die( 'Restricted access' );
jimport('joomla.application.component.controller');
/**
* Hello World Component Controller
*
* #package Joomla.Tutorials
* #subpackage Components
*/
class HelloController extends JController
{
/**
* Method to display the view
*
* #access public
*/
function __construct($default = array())
{
parent::__construct($default);
// Register Extra tasks
$this->registerTask( 'detail' , 'display' );
}
function display()
{
switch($this->getTask())
{
case 'detail' :
{
JRequest::setVar( 'view' , 'new');
// Checkout the weblink
$model = $this->getModel('hello');
} break;
}
parent::display();
}
}
this is my view.html.php
class HelloViewNew extends JView
{
function display($tpl = null)
{
global $mainframe;
$db =& JFactory::getDBO();
$model =& $this->getModel('hello');
$items = & $model->getdetail();
$this->assignRef( 'items', $items );
parent::display($tpl);
}
}
and this is my model
defined( '_JEXEC' ) or die( 'Restricted access' );
jimport( 'joomla.application.component.model' );
/**
* Hello Model
*
* #package Joomla.Tutorials
* #subpackage Components
*/
class HelloModelHello extends JModel
{
/**
* Gets the greeting
* #return string The greeting to be displayed to the user
*/
var $_data;
/**
* Returns the query
* #return string The query to be used to retrieve the rows from the database
*/
function _buildQuery()
{
$query = ' SELECT * '
. ' FROM #__hello WHERE published = 1'
;
return $query;
}
/**
* Retrieves the hello data
* #return array Array of objects containing the data from the database
*/
function getData()
{
// Lets load the data if it doesn't already exist
if (empty( $this->_data ))
{
$query = $this->_buildQuery();
$this->_data = $this->_getList( $query );
}
//echo "<pre>"; print_r($this->_data); exit;
return $this->_data;
}
function detail()
{
echo "this is test"; exit;
}
}
My question is how can i fetch that detail function from database its not working for me?
on your model, you've the function : function detail() ,
But you've tried to call the function on view with : $items = & $model->getdetail();
Remember your function is detail() NOT getdetail() . So, call with :
$items = & $model->detail();
That's your only mistake I guess so, good luck
you should use this in contoller
$view = $this->getView();
$view->setModel($this->getModel());
then you can use $this->getModel() in view.

How do I only show fields that have values, and hide fields that do not have values?

I am trying to get this code to show only fields that have values, any fields that don't have values are not meant to be displayed. It doesnt seem to be working
Any idea what I am doing wrong?
My simple test form is here http://www.healthybrighton.co.uk/wse/node/1844
/**
* Build a table of submitted values
*
* #param $form_vals array Submitted form data
* #param $select_mapping array Map select components to their value|label chocies
* #return HTML of the themed table
*/
function _format_form_state($form_vals = array(), $select_mapping) {
$output = '';
$header = array();
$rows = array();
if (!empty($form_vals)) {
foreach ($form_vals as $component_name => $component_value) {
$rows = array_merge(
$rows,
_add_component_row(
$component_name,
$component_value,
0,
$select_mapping
)
);
}
}
$output .= theme('table', $header, $rows);
return $output;
}
/**
* Build a table of submitted values
*
* #param $select_mapping array Map select components to their value|label chocies
* #param $values array Submitted form data
* #return HTML of the themed table
*/
function _format_form_state($select_mapping, $values = array()) {
$header = array(t('First'), t('Second'), t('Third'), t('Fourth'));
$rows = array();
foreach ($values as $cname => $cval) {
$rows[] = array($cname, $cval, 0, $select_mapping);
}
return theme_table($header, $rows);
}
$select_mapping should be first argument in function.
Argument with default value should not be preceded by argument without a default value.