Create a {if} statement in WHMCS - if-statement

Im trying to create a specific IF statement on the clientareaproductdetails.tpl file in WHMCS - bottom line i'm trying to display some text on a page depending on the product the customer is looking at.
So this is what I tried (which does not work)
{if $id == '17'} something {else} nothing {/if}
So if the product ID = 17 then display 'something' otherwise display 'nothing.
Any ideas if/how this is possible?
Thanks in advance.
H

If by product id, you mean the package id, then it explains why your code didn't work. $id variable is for service id.
To achieve what you want, Add a hook file (say: custom_product_message.php) to includes/hooks/ folder.
Then add the following code:
<?php
add_hook('ClientAreaProductDetailsOutput', 1, function($service) {
if (!is_null($service)) {
if ($service['service']->packageId == 17) {
return "something";
} else {
return 'nothing';
}
}
return '';
});
The idea is to use ClientAreaProductDetailsOutput hook to display a text in the clientarea productdetails page.

Related

How to add form alter hook in drupal 8

I am very much new to drupal 8, I am trying to alter a form under custom content type "Question". The form has an id "node-question-form".
I have setup a module and trying to add hook_FORM_ID_alter() but its never getting called. Even the simplest hook is not working. eg:
function constellator_form_alter(&$form, &$form_state, $form_id){
echo "alter the form"; exit;
}
where 'constellator' is the module name.
I have been stuck with since morning and nothing is working for me, Any help will be greatly appriciated.
Cheers
hook_form_alter() and hook_FORM_ID_alter() are called while a form is being created and before displaying it on the screen.
These functions are written in the .module file.
Always clear the cache after writing any hook function. This makes Drupal understand that such a function has been declared.
Use drush cr if using drush version 8 else click on Manage->Drupal 8 logo->Flush all Caches to clear the caches.
Now you can check if the function is being called or not.
The best way to check that is to install the Devel module, enable it. Along with Devel, Kint is installed. Enable Kint too from the Admin UI.
After doing that,you can check whether the hook is being called or not in the following way:
function constellator_form_alter(&$form, &$form_state, $form_id){
kint($form);
}
This will print all the form variables for all forms in the page.
If you want to target a particular form in the page, for eg. you form, node-question-form, type:
function node_question_form_form_alter(&$form, &$form_state, $form_id){
kint($form);
}
Using echo, the way you did, you can confirm whether the function is being called or not, without any hassle, by viewing the Source Code for the page and then searching for the text that you have echoed, using some search option of browser, like, Ctrl+f in case of Google Chrome.
If you want to change sorting options and/or direction (ASC / DESC), you can use this example (tested with Drupal 9).
Here I force the sorting direction according to the "sort by option" set by the user in an exposed filter. (if the user want to sort by relevance, we set the order to ASC, if the user want to sort by date, we set the order to DESC to have the latest content first).
/**
* Force sorting direction for search by date
*
*/
function MYTHEME_form_alter(&$form, &$form_state, $form_id)
{
if (!$form_id == 'views_exposed_form"' || !$form['#id'] == 'views-exposed-form-search-custom-page-1') {
return;
}
$user_input = $form_state->getUserInput();
if (empty($user_input['sort_by'])) {
return;
}
if ($user_input['sort_by'] == 'relevance') {
$user_input['sort_order'] = 'ASC';
} elseif ($user_input['sort_by'] == 'created') {
$user_input['sort_order'] = 'DESC';
}
$form_state->setUserInput($user_input);
}
Note that "views-exposed-form-search-custom-page-1" is the id of my form,
"relevance" and "created" are the sort field identifier set in drupal admin.
function hook_form_alter(&$form, \Drupal\Core\Form\FormStateInterface &$form_state, $form_id) {
echo 'inside form alter';
}

ATK4 Form datePicker - default state is 'opened', can this be changed?

I'm using the following form, and everytime the page opens (it's in an expander on a grid) the datePicker is 'open', obscuring part of the text above it.
function page_del() {
$billingid = $_GET['id'];
$now = date("Y-m-d H:i:s");
$q = $this->api->db->dsql()
->table('billing')
->where('id', $billingid)
->field('enddate')
->getOne();
if (!$q) {
$this->add('H5')->set('Are you sure you want to stop billing this item?');
$form = $this->add('Form');
$form->addField('hidden','billingid')->set($billingid);
$form->addField('datePicker','datum')->set($now);
$form->addSubmit('Confirm');
if ($form->isSubmitted()) {
$form->stopBilling('manual', $form, $now);
$this->js()->univ()->getjQuery()->trigger('reload_grid')->execute();
}
} else {
$this->add('H5')->set('This product has already been stopped, effective date: ' .$q);
}
}
}
I have other forms elsewhere that also have a datePicker as their first (visible) field that do not display this behaviour. I only mention it because it looks like a 'focus' issue? I.e. the first field gets focus?
Any thoughts on what causes this or how it can be remedied?
Actually it is field state "onfocus", not default. Your form has only one field and this (first) field is selected on page load.
This behavior is added here:
https://github.com/atk4/atk4/blob/master/lib/Form/Field/DatePicker.php#L35
function addCalendarIcon() {
$this->addButton('',array('options'=>array('text'=>false)))
->setHtml(' ')
->setIcon('ui-icon-calendar')
->js('click',$this->js()->datepicker('show'));
$this->js('focus', $this->js()->datepicker('show'));
}
You can redefine this method in your project and remove line
$this->js('focus', $this->js()->datepicker('show'));

Opencart: How to render a template only on a specific page?

In my catalog/controller/mycontroller.php, I have a script like this:
$this->data['settings'] = $this->config->get('my_module'); // retrieves data from "setting" table
foreach ($this->data['settings'] as $data) {
if ($data['pageurl'] == 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']) {
$this->render();
}
}
In Extensions > Modules, I have my extension installed where I can set different page URLs to different Layouts and Positions like this:
Page URL Layout Position
================================================
http://...?product_id=10 Product Content Top
http://...?product_id=20 Product Content Top
http://... Home Content Top
My issue is - I'd like to render a template only ONCE on a specific page that meets the condition in the above script. What's currently happening is $this->render() is showing the template MULTIPLE times based on the Position and Layout in Extensions > Modules. For instance, when I visit http://...?product_id=10 page, it displays the template twice while it's only supposed to display it once because it only meets the condition for product_id=10 in the controller. How can I do this?
First of all, do this:
var_dump('http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']);
I guess You may be missing also the $_SERVER['QUERY_STRING'] part...
to see, what URL are You building to compare to URLs You are setting in the backend...
Next, please, do not call $this->render() in a loop no matter why the condition succeed more times, instead of this do something like this:
$this->data['settings'] = $this->config->get('my_module'); // retrieves data from "setting" table
$render = false;
foreach ($this->data['settings'] as $data) {
if ($data['pageurl'] == HTTP_SERVER . $_SERVER['REQUEST_URI']) { // use defined constant HTTP_SERVER
$render = true;
}
}
if($render) {
$this->render();
}

Login only prestashop catalog

Im building a prestashop catalog, but it needs to be visible to logged in customers only. Is this possible. It would be nice if the built in prestashop login is used for this.. any help is appreciated.
I have a suggestion. You can use the Customer Groups feature in PrestaShop 1.5 and only allow logged in customers to see the prices. For every Customer that is grouped in Visitor, they would see your website in Catalog Mode.
Prestashop 1.5 solution:
Simply upload the original file:
classes\controller\FrontController.php
into:
override/classes/controller/FrontController.php
Next, rename the class. Final code should look like this:
class FrontController extends FrontControllerCore
{
public function init()
{
parent::init();
if (!$this->context->customer->isLogged() && $this->php_self != 'authentication' && $this->php_self != 'password')
{
Tools::redirect('index.php?controller=authentication?back=my-account');
}
}
}
The last step is to manually delete the following file so prestashop is aware of the overriden class (It will be re-generated automatically):
cache/class_index.php
And voilà, functionality achieved without overwriting core files.
It'll be easy.
Use this code:
if(!self::$cookie->isLogged(true) AND in_array($this->step, array(1, 2, 3)))
Tools::redirect('authentication.php');
In the preprocess of your indexController
Here’s my solution, it works like a charm and is a very easy fix!
In classes\Configuration.php (around line 114) it looks like this
static public function get($key, $id_lang = NULL)
{
if ($id_lang AND isset(self::$_CONF_LANG[(int)$id_lang][$key]))
return self::$_CONF_LANG[(int)$id_lang][$key];
elseif (is_array(self::$_CONF) AND key_exists($key, self::$_CONF))
return self::$_CONF[$key];
return false;
}
change it to this:
static public function get($key, $id_lang = NULL)
{
//Grab access to the $cookie which is already loaded in the FrontController as global $cookie;
global $cookie;
if ($id_lang AND isset(self::$_CONF_LANG[(int)$id_lang][$key]))
return self::$_CONF_LANG[(int)$id_lang][$key];
elseif (is_array(self::$_CONF) AND key_exists($key, self::$_CONF))
//If the system is trying to find out if Catalog Mode is ON, then return the configuration setting,
//but override it with the user logon status
if($key == 'PS_CATALOG_MODE')
{
return !$cookie->logged || self::$_CONF[$key];
}
else
{
return self::$_CONF[$key];
}
return false;
}
Essentially, I wanted to force the system to display the “Catalog Mode” when the user is not logged in, and to turn this off when he is logged in.
I can guarantee this works for v1.4.3.0 and the code for the current version 1.4.8.2 (at the time of this post) has not changed, so it should work there.

Adding a search box to filter a list of results in Symfony?

I need to put a search box within a list of objects as a result of a typical indexSuccess action in Symfony. The goal is simple: filter the list according to a criteria.
I've been reading the Zend Lucene approach in Jobeet tutorial, but it seems like using a sledge-hammer to crack a nut (at least for my requirements).
I'm more interested in the auto-generated admin filter forms but I don't know how to implement it in a frontend.
I could simply pass the search box content to the action and build a custom query, but is there any better way to do this?
EDIT
I forgot to mention that I would like to have a single generic input field instead of an input field for each model attribute.
Thanks!
I'm using this solution, instead of integrating Zend Lucene I manage to use the autogenerated Symonfy's filters. This is the way i'm doing it:
//module/actions.class.php
public function executeIndex(sfWebRequest $request)
{
//set the form filter
$this->searchForm = new EmployeeFormFilter();
//bind it empty to fetch all data
$this->searchForm->bind(array());
//fetch all
$this->employees = $this->searchForm->getQuery()->execute();
...
}
I made a search action which does the search
public function executeSearch(sfWebRequest $request)
{
//create filter
$this->searchForm = new EmployeeFormFilter();
//bind parameter
$fields = $request->getParameter($this->searchForm->getName());
//bind
$this->searchForm->bind($fields);
//set paginator
$this->employees = $this->searchForm->getQuery()->execute();
...
//template
$this->setTemplate("index");
}
It's important that the search form goes to mymodule/search action.
Actually, i'm also using the sfDoctrinePager for paginate setting directly the query that the form generate to get results properly paginated.
If you want to add more fields to the search form check this :)
I finally made a custom form using the default MyModuleForm generated by Symfony
public function executeIndex {
...
// Add a form to filter results
$this->form = new MyModuleForm();
}
but displaying only a custom field:
<div id="search_box">
<input type="text" name="criteria" id="search_box_criteria" value="Search..." />
<?php echo link_to('Search', '#my_module_search?criteria=') ?>
</div>
Then I created a route named #my_module_search linked to the index action:
my_module_search:
url: my_module/search/:criteria
param: { module: my_module, action: index }
requirements: { criteria: .* } # Terms are optional, show all by default
With Javascript (jQuery in this case) I append the text entered to the criteria parameter in the href attribute of the link:
$('#search_box a').click(function(){
$(this).attr('href', $(this).attr('href') + $(this).prev().val());
});
And finally, back to the executeIndex action, I detect if text was entered and add custom filters to the DoctrineQuery object:
public function executeIndex {
...
// Deal with search criteria
if ( $text = $request->getParameter('criteria') ) {
$query = $this->pager->getQuery()
->where("MyTable.name LIKE ?", "%$text%")
->orWhere("MyTable.remarks LIKE ?", "%$text%")
...;
}
$this->pager->setQuery($query);
...
// Add a form to filter results
$this->form = new MyModuleForm();
}
Actually, the code is more complex, because I wrote some partials and some methods in parent classes to reuse code. But this is the best I can came up with.