Yii HumHub Inject Event into Main App - php

I am trying to attach a event to a global scope of a Yii application called HumHub however, when I try to run a function through the "TopMenu" which is global, it doesn't appear to do anything. Possibly because I'm not using the $event variable scope?
I am trying to make a development mode module to prevent access to anyone but administrators from viewing the system when it's activated. The only global way I know to get my code across all pages (with my limited understanding of HumHub and Yii) is the TopMenu as it appears on all pages (besides login page which is disabled when guest mode is active)
autostart.php
Yii::app()->moduleManager->register(array(
'id' => 'devmode',
'class' => 'application.modules.devmode.DevModeModule',
'import' => array(
'application.modules.devmode.*',
),
// Events to Catch
'events' => array(
array('class' => 'AdminMenuWidget', 'event' => 'onInit', 'callback' => array('DevModeEvents', 'onAdminMenuInit')),
array('class' => 'TopMenuWidget', 'event' => 'onInit', 'callback' => array('DevModeEvents', 'devBlock')),
array('class' => 'DashboardSidebarWidget', 'event' => 'onInit', 'callback' => array('DevModeModule', 'onSidebarInit')),
),
));
DevModeEvents.php
<?php
/**
* Defines the module events
*
* #package humhub.modules.devmode.events
* #author Jordan Thompson
*/
class DevModeEvents {
public static function onAdminMenuInit($event)
{
$event->sender->addItem(array(
'label' => Yii::t('devmode.base', 'Development Mode'),
'url' => Yii::app()->createUrl('//devmode/config/config'),
'group' => 'settings',
'icon' => '<i class="fa fa-lock"></i>',
'isActive' => (Yii::app()->controller->module && Yii::app()->controller->module->id == 'devmode' && Yii::app()->controller->id == 'admin'),
'sortOrder' => 300,
));
}
public static function devBlock($event) {
$devMode = HSetting::Get('devMode', 'devmode');
if ($devMode == 1 ) {
if (!Yii::app()->user->isGuest) {
if (!Yii::app()->user->isAdmin()) {
throw new CHttpException('418', Yii::t('devmode.base', Yii::app()->name . ' is currently under maintenance, check back later.'));
}
} else {
throw new CHttpException('418', Yii::t('devmode.base', Yii::app()->name . ' is currently under maintenance, check back later.'));
}
}
}
}

change the sort order to 1, and access to it using css
li:nth-child(1) {
//do something;
}

Related

Why does my custom code show warnings in the logs?

I have a site on Drupal 8.6 and Bootstrap 3.3.7
I created a custom module for the customer to accept the terms and conditions of the store when placing an order.
This displays a link to a checkbox before payment to display the terms and conditions in a modal window.
When I place an order here are the warnings in the logs (sorry my code is too long to be published here):
https://pastebin.com/1p5m1Ved
https://pastebin.com/XYbqDJje
https://pastebin.com/P93bStKh
Here is the file that created the problem :
<?php
namespace Drupal\commerce_marketplace_terms_and_conditions\Plugin\Commerce\CheckoutPane;
use Drupal\Component\Serialization\Json;
use Drupal\Core\Form\FormStateInterface;
use Drupal\commerce_checkout\Plugin\Commerce\CheckoutPane\CheckoutPaneBase;
use Drupal\commerce_checkout\Plugin\Commerce\CheckoutPane\CheckoutPaneInterface;
use Drupal\Core\Link;
use Drupal\Core\Url;
/**
* Provides the completion message pane.
*
* #CommerceCheckoutPane(
* id = "marketplace_terms_and_conditions",
* label = #Translation("Marketplace Terms and Conditions"),
* default_step = "review",
* )
*/
class MarketplaceTermsAndConditions extends CheckoutPaneBase implements CheckoutPaneInterface {
/**
* {#inheritdoc}
*/
public function buildPaneForm(array $pane_form, FormStateInterface $form_state, array &$complete_form) {
$store_name = $this->order->getStore()->getName();
$store_id = $this->order->getStoreId();
$pane_form['#attached']['library'][] = 'core/drupal.dialog.ajax';
$attributes = [
'attributes' => [
'class' => 'use-ajax',
'data-dialog-type' => 'modal',
'data-dialog-options' => Json::encode([
'width' => auto
]),
],
];
$link = Link::fromTextAndUrl(
$this->t('terms and conditions of the store "#store_name"', ['#store_name' => $store_name]),
Url::fromUri("internal:/store/$store_id/cgv", $attributes)
)->toString();
$pane_form['marketplace_terms_and_conditions'] = [
'#type' => 'checkbox',
'#default_value' => FALSE,
'#title' => $this->t('I have read and accept #terms.', ['#terms' => $link]),
'#required' => TRUE,
'#weight' => $this->getWeight(),
];
return $pane_form;
}
}
What's wrong with my custom module and how to fix the problem ? Thank you
Change this to
'width' => auto
this
'width' => 'auto'
It is assuming it to be a constant, as you can see from the errors, it has to be either a variable or a string.

My shortcode doesn't appear in Visual Composer using vc_map

I've built a Wordpress plugin, and now I want to add support for Visual Composer but I can't get it to work. I've read a lot of articles about how to use vc_map, found pages on github with examples, and it seems like I do everything like described in these articles. However my shortcodes still don't show up in the VC editor.
My shortcode (it works perfectly as a shortcode, [mappy id="123"] will show the map with the ID of 123)
add_shortcode('mappy', 'mappy_shortcode');
function mappy_shortcode($atts) {
// Attribute defaults
extract(shortcode_atts(array(
'id' => null,
), $atts));
// Check if id is set, and the given post is a mappy_map
if(!isset($atts['id'])) {
return '';
}
$post = get_post(intval($atts['id']));
if(!isset($post)) {
return;
}
if($post->post_type != 'mappy_map') {
return;
}
return $post->post_content;
}
And the code I've tried:
/**
* Visual Composer Support
*/
if(defined('WPB_VC_VERSION')) {
add_action('vc_before_init', 'mappy_vc_support');
}
function mappy_vc_support() {
vc_map(
array(
'base' => 'mappy',
'description' => __('Display a Mappy map.', 'mappy'),
'category' => 'Mappy',
'icon' => plugin_dir_url(__FILE__) . 'img/vc_map_32.png',
'class' => '',
'weight' => 100,
'params' => array(
array(
'type' => 'dropdown',
'class' => '',
'heading' => __('Térkép', 'mappy'),
'param_name' => 'id',
'value' => mappy_map_list(),
'description' => __('Térkép', 'mappy'),
'holder' => 'div'
)
)
)
);
}
/**
* Helper function to get all mappy_map posts
*/
function mappy_map_list() {
$list = array();
$posts = get_posts(array('post_type' => 'mappy_map'));
foreach($posts as $post) {
$list[$post->post_title] = $post->ID;
}
return $list;
}
I've also tried to hook mappy_vc_support into init and admin_init, with no luck.
Also note that the function vc_map itself is being called, and returns 1.
What could be the problem here?

Yii cgridview last page pagination issue

I have simple Yii cgridview code with pagination. Pagination is working fine but in the last page I have faced one issue.
For example, If I have 13 records in DB table, and set pagination for 10 pages per page then for first page It will show "1 - 10 of 13 results" but when I clicked on 2nd page link then It will show "4 - 13 of 13" instead of "11 - 13 of 13".
Here is my code.
1) Controller :
function actiontransactionHistory(){
$creditTransactionObj = new CreditTransaction();
$this->render('history',array(
'creditTransactionObj'=>$creditTransactionObj,
));
}
2) Model :
public function search()
{
// Warning: Please modify the following code to remove attributes that
// should not be searched.
$criteria=new CDbCriteria;
$criteria->compare('id',$this->id,true);
$criteria->compare('fkasmtGroupId',$this->fkasmtGroupId);
$criteria->compare('fkgroupSurveyUserId',$this->fkgroupSurveyUserId,true);
$criteria->compare('fkasmtId',$this->fkasmtId);
$criteria->compare('transaction_type',$this->transaction_type);
$criteria->compare('credit_type',$this->credit_type,true);
$criteria->compare('credit_qty',$this->credit_qty);
$criteria->compare('transaction_date',$this->transaction_date,true);
$criteria->compare('isDelete',$this->isDelete);
$criteria->compare('status',$this->status);
$criteria->compare('created_at',$this->created_at,true);
$criteria->compare('modified_at',$this->modified_at,true);
$sort = array(
"defaultOrder" => "transaction_date DESC",
);
return new CActiveDataProvider($this, array(
"criteria"=>$criteria,
"sort" => $sort,
));
}
3) View:
<?php
$this->widget('zii.widgets.grid.CGridView', array(
'id' => 'history-grid',
'dataProvider' => $creditTransactionObj->search(),
'loadingCssClass' => '',
'enableSorting' => true,
'itemsCssClass' => 'my-teams',
'summaryText' => "Displaying {start} - {end} of {count} results.",
"emptyText" => "There is no transaction history available.",
'columns' => array(
array('name' => 'transaction_date', 'header' => 'Date', 'type' => 'raw', 'value' => 'date("d-M-Y",strtotime($data->transaction_date))', 'htmlOptions' => array('class' => '')),
array('name' => 'credit_qty', 'header' => '# of Credits', 'sortable'=>false, 'type' => 'raw', 'value' => '($data->transaction_type == 1) ? - $data->credit_qty : $data->credit_qty', 'htmlOptions' => array('class' => '')),
array('name' => 'credit_type', 'header' => 'Type', 'type' => 'raw', 'value' => '$data->credit_type', 'htmlOptions' => array('class' => '')),
array('name' => 'transaction_type', 'header' => 'Activity', 'type' => 'raw', 'value' => '($data->transaction_type == 0) ? "Purchased" : (($data->transaction_type == 1) ? "Spent" : "Refunded")', 'htmlOptions' => array('class' => '')),
array('name' => 'fkasmtGroupId', 'header' => 'Group Name', 'type' => 'raw', 'value' => array($this,'getGroupName'), 'htmlOptions' => array('width' => '35%')),
)
));
?>
I have also attached both pages screenshot.
Any help will be appreciate. Thanks in advance !
After spending lots of hours finally I found the solution for this issue.
Actually the problem was in fetchData() function which is used in
yii/framework/web/CActiveDataProvider.php framework class file.
In fetchData() method, limit was not calculated properly for the last page pagination. so I have made changes to calculate correct limit.
Old Code:
protected function fetchData()
{
$criteria=clone $this->getCriteria();
if(($pagination=$this->getPagination())!==false)
{
$pagination->setItemCount($this->getTotalItemCount());
$pagination->applyLimit($criteria);
}
$baseCriteria=$this->model->getDbCriteria(false);
if(($sort=$this->getSort())!==false)
{
// set model criteria so that CSort can use its table alias setting
if($baseCriteria!==null)
{
$c=clone $baseCriteria;
$c->mergeWith($criteria);
$this->model->setDbCriteria($c);
}
else
$this->model->setDbCriteria($criteria);
$sort->applyOrder($criteria);
}
$this->model->setDbCriteria($baseCriteria!==null ? clone $baseCriteria : null);
$data=$this->model->findAll($criteria);
$this->model->setDbCriteria($baseCriteria); // restore original criteria
return $data;
}
New Code:
protected function fetchData()
{
$criteria=clone $this->getCriteria();
if(($pagination=$this->getPagination())!==false)
{
$pagination->setItemCount($this->getTotalItemCount());
$pagination->applyLimit($criteria);
// update limit to the correct value for the last page
$limit=$pagination->getLimit();
$offset=$pagination->getOffset();
if ( $offset+$limit > $pagination->getItemCount() )
$criteria->limit = $pagination->getItemCount() - $offset;
}
$baseCriteria=$this->model->getDbCriteria(false);
if(($sort=$this->getSort())!==false)
{
// set model criteria so that CSort can use its table alias setting
if($baseCriteria!==null)
{
$c=clone $baseCriteria;
$c->mergeWith($criteria);
$this->model->setDbCriteria($c);
}
else
$this->model->setDbCriteria($criteria);
$sort->applyOrder($criteria);
}
$this->model->setDbCriteria($baseCriteria!==null ? clone $baseCriteria : null);
$data=$this->model->findAll($criteria);
$this->model->setDbCriteria($baseCriteria); // restore original criteria
return $data;
}
But remember, never update a core class file of framework. so I just extend this method in my Model file and write below code.
Final Code In My Model file without change in framework file:
class CustomActiveDataProvider extends CActiveDataProvider
{
/**
* Fetches the data from the persistent data storage.
* #return array list of data items
*/
protected function fetchData()
{
$criteria=clone $this->getCriteria();
if(($pagination=$this->getPagination())!==false)
{
$pagination->setItemCount($this->getTotalItemCount());
$pagination->applyLimit($criteria);
// update limit to the correct value for the last page
$limit=$pagination->getLimit();
$offset=$pagination->getOffset();
if ( $offset+$limit > $pagination->getItemCount() )
$criteria->limit = $pagination->getItemCount() - $offset;
}
$baseCriteria=$this->model->getDbCriteria(false);
if(($sort=$this->getSort())!==false)
{
// set model criteria so that CSort can use its table alias setting
if($baseCriteria!==null)
{
$c=clone $baseCriteria;
$c->mergeWith($criteria);
$this->model->setDbCriteria($c);
}
else
$this->model->setDbCriteria($criteria);
$sort->applyOrder($criteria);
}
$this->model->setDbCriteria($baseCriteria!==null ? clone $baseCriteria : null);
$data=$this->model->findAll($criteria);
$this->model->setDbCriteria($baseCriteria); // restore original criteria
return $data;
}
}
// Used this custome active data provider as shown in below.
public function search()
{
$criteria=new CDbCriteria;
$criteria->compare('id',$this->id,true);
$criteria->compare('isDelete',$this->isDelete);
$criteria->compare('status',$this->status);
$criteria->compare('created_at',$this->created_at,true);
$criteria->compare('modified_at',$this->modified_at,true);
$sort = array(
"defaultOrder" => "transaction_date DESC",
);
return new CustomActiveDataProvider($this, array(
"criteria"=>$criteria,
"sort" => $sort,
"pagination" => array('pageSize' => (isset($_REQUEST['pageSize'])?$_REQUEST['pageSize']:10))
));
}
After this change, Last page pagination works completely fine.
Thanks!

Alias "custom.controllers.ExampleController.php" is invalid

This is the error:
Alias "custom.controllers.ExampleController.php" is invalid. Make sure
it points to an existing PHP file and the file is readable.
My code is given below
main.php=>
return
array(
'controllerMap' => array(
'product' => array(
'class' => 'custom.controllers.Product.php',
),
),
'import' => array(
'custom.mycompany.*',
),
'components' =>
array(
'widgetHandler' => array(
//Load a component
'class' => 'custom.mycompany.mywidget.mywidget',
),
)
);
Product.php=>
<?php
class Product extends Controller
{
public function actionIndex()
{
echo "this is the default index function";
}
public function actionTest()
{
echo "This is the test function";
}
}
I am using lightspeed cms.
the notation for Yii2 / php class is not dot based but slash based
'class' => 'custom\controllers\Product.php',
(And in your code there is not the ExampleController..)
see p https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-0.md
and https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-4-autoloader.md
https://github.com/yiisoft/yii2/blob/master/docs/internals/core-code-style.md
for PHP an Yii2 coding standard suggestion

Adding a custom filter to views in Drupal 7

im using Drupal 7 and I want to add a new filter in views.
I have a custom table "clicks" with two fields; nid and clicks_left.
The filter should just contain a checkbox "Only display nodes with clicks left". So the filter should join node and clicks on nid..
I have read like thousands of pages of custom filters but can't get it to work =)
Please, could someone show me a working example so I understand?
I have come so far that the filter is displayed under filters but what do I need to add to do the join and get the checkbox? The relevant code below:
FILE clicks_views.inc:
function clicks_views_data() {
$data = array();
$data['clicks']['clicks_filter'] = array(
'group' => t('Clicks'),
'title' => t('Clicks left'),
'help' => t('Filter any Views based on clicks left'),
'filter' => array(
'field' => 'clicks_left',
'handler' => 'clicks_handler_filter',
),
);
return $data;
}
FILE clicks_handler_filter.inc:
<?php
class clicks_handler_filter extends views_handler_filter {
???
};
I know both functions are wrong ;)
Ok, I've found a solution. For anyone who needs it:
In clicks.module
function clicks_views_api() {
return array(
'api' => 2,
'path' => drupal_get_path('module', 'clicks') . '/includes'
);
}
In clicks.views.inc
function clicks_views_handlers() {
return array(
'info' => array(
'path' => drupal_get_path('module', 'clicks') . '/includes', // path to view files
),
'handlers' => array(
// register our custom filter, with the class/file name and parent class
'clicks_handler_filter' => array(
'parent' => 'views_handler_filter',
)
),
);
}
function clicks_views_data() {
$data = array();
if(module_exists('clicks')) {
$data['node']['clicks'] = array(
'group' => t('Clicks'),
'title' => t('Clicks left'),
'help' => t('Filter any Views based on clicks left'),
'filter' => array(
'handler' => 'clicks_handler_filter',
),
);
}
return $data;
}
In clicks_handler_filter.inc
class clicks_handler_filter extends views_handler_filter {
function admin_summary() { }
function operator_form() { }
function query() {
$table = $this->ensure_my_table();
$join = new views_join();
$join->construct('clicks', $this->table_alias, 'nid', 'nid');
$this->query->ensure_table('clicks', $this->relationship, $join);
$this->query->add_where($this->options['group'], "clicks.clicks_left", 0, ">");
}
}
This gives me a possibility to add a filter "clicks" that if enabled hides all results that doesn't have clicks left (clicks_left > 0)
Actually, if your values in your tables clicks are numeric you don't need to create your own handler, you can use the default from Views views_handler_filter_numeric.
You can see all handlers that already exists in the Views handlers.

Categories