joomla 3.0 pass variable from index to view - joomla3.0

I am trying to pass a variable from the index page to an article view. Basically the width of a div needs to change based on whether there are sidebars.
index.php:
if ($this->countModules('position-1')&$this->countModules('position-3')){
$content_margin = 'contentCenter';
}elseif ($this->countModules('position-1')&!$this->countModules('position-3')){
$content_margin = 'contentRight';
}elseif (!$this->countModules('position-1')&$this->countModules('position-3')){
$content_margin = 'contentLeft';
}else{
$content_margin = '';
}
How do I then access the $content_margin variable within the component?
<jdoc:include type="component" class="<?php echo $content_margin; ?>" />

I would rather try something like this:
<div class="<?php echo $content_margin; ?>">
<jdoc:include type="component" />
</div>
You do not need to pass this variable into your component, just give your CSS classes different widths.
If you want to count the number of modules in your component you could look at this link

Related

Codeigniter foreach Select Menu 'Selected' Option

I am creating a theme select option for an App. I want the user to choose from a select option which theme they want, light or dark.
I am storing theme names in the database like so:
I am then calling all themes in the Model and returning the array with this code:
public function getThemes() {
$query = $this->db->query('SELECT * FROM theme');
return $query->result_array();
}
I am using my Constructor to grab the data and Render it to my settings view like so: (I am sending a few things at the same time in $this->data that's why it is in an array but I cut that code out)
$this->data = [
'themes' => $this->model_setting->getThemes()
];
$this->render('backend/standart/administrator/setting/setting_general', $this->data);
In my HTML View I have the following HTML which successfully displays the correct 2 themes available:
<div class="col-sm-12">
<label>Select Your Desired Theme</label><br>
<select class="form-control" name="site_color_theme" id="site_color_theme">
<?php foreach ($themes as $theme): ?>
<option value="<?= $theme['theme']; ?>" ><?= $theme['name']; ?></option>
<?php endforeach; ?>
</select>
</div>
When I save the settings form I am updating my options table in the database with the theme name. The Html Body Class calls on this options table and finds the chosen theme and uses this to render a specific CSS Stylesheet.
The problem I am having is when I save my settings, the Select Option does not display the saved theme name to show that this is the active theme chosen.
How can I get the Select Option to display the chosen theme when the user visits the settings page to select another theme?
You should have active_theme column in the theme table
active_theme set to 1 when form dropdown site_color_theme is submitted (the rest theme records set to 0)
You should use Codeigniter Form Helper to do that : https://www.codeigniter.com/user_guide/helpers/form_helper.html#form_dropdown
// $themes = Theme list array [id => theme-name, id => theme-name]
// $active_theme = Record of "active_theme" with value equal to "1"
// $extra = Custom HTML attributes e.g. class="something" onclick="function(js)"
echo form_dropdown('site_color_theme', $themes, $active_theme, $extra);

Removing 'src' attribute from output of $html->image() in CakePHP

I am using CakePHP 1.2. I am trying to use jQuery Lazy (http://jquery.eisbehr.de/lazy/example_basic-usage) from CakePHP. Reading the documentation at https://book.cakephp.org/1.2/en/The-Manual/Core-Helpers/HTML.html#image, it shows how
<?php echo $html->image('cake_logo.png', array('alt' => 'CakePHP'))?>
produces the following output:
<img src="/img/cake_logo.png" alt="CakePHP" />
I need to produce this output:
<img class="lazy" data-src="/img/cake_logo.png" />
How can I do that using $html->image() in CakePHP 1.2? The problem is that in the syntax image(string $path, array $htmlAttributes = array()), the first parameter is mandatory and in the output it produces the src=... attribute of img. I need to have an output with <img... /> that does not contain the src=... attribute. How could I achieve that using $html->image() in CakePHP?
Via the built-in HTML helper it's not possible without modifying the tag template. So if you need lazy loading on all images, then you could for example change the image tag template to something like:
<img class="lazy" data-src="%s" %s/>
However this would clash with using the class attribute. So alternatively you could extend the HTML helper and implement a custom image() method (or an additional method). Here's a quick & dirty example, which should be pretty self-explantory:
function image($path, $options = array())
{
$defaults = array(
'class' => null
);
$options += $defaults;
$options['class'] = 'lazy ' . $options['class'];
$originalTemplate = $this->tags['image'];
$this->tags['image'] = '<img data-src="%s" %s/>';
$imageHtml = parent::image($path, $options);
$this->tags['image'] = $originalTemplate;
return $imageHtml;
}
See also
Cookbook > Core Helpers > HTML > Changing the tags output by HtmlHelper
Cookbook > Developing With CakePHP > Helpers > Creating Helpers

Echoing html link mailto

I'm using Ckeditor on a field of a CakePHP form.
No problems saving the information on the database the problem is when I echo the content of that field. If the field as a link of the type mailto I get a denied: in the beginning of the href attribute.
For example:
name#domain.com
To echo the field value I'm using:
echo $data['Ent']['text'];
you can use :
<?php echo $this->Html->link($v['mail'],'mailto:'.$v['mail'],array('target' => '_blank'));?>
Dont know why Ckediter is doing that but you can remove it using jquery
<a id="link1" href="denied:mailto:name#domain.com">name#domain.com</a>
<script>
$(document).ready(function (){
$("#link1").attr("href","mailto:name#domain.com");
});
</script>

Cannot render submit button

I've customized the edit profile view using template.php and user-profile-form.php
All shows up correctly but the Submit (and Delete) button..
I'm using Adaptive Theme and I've modified like this :
template.php
function adaptivetheme_theme(&$existing, $type, $theme, $path) {
return array(
'user_profile_form' => array(
'template' => 'templates/user-profile-form',
'render element' => 'form',
),
);
}
function adaptivetheme_preprocess_user_profile_form(&$vars) {
$vars['form']['account']['name']['#description'] = t('blabla');
$vars['form']['submit']['#value'] = t('Save profile');
$vars['form']['delete']['#value'] = t('Delete account');
$vars['account'] = drupal_render($vars['form']['account']);
$vars['theme_select'] = drupal_render($vars['form']['theme_select']);
$vars['picture'] = drupal_render($vars['form']['picture']);
$vars['signature_settings'] = drupal_render($vars['form']['signature_settings']);
$vars['contact'] = drupal_render($vars['form']['contact']);
$vars['timezone'] = drupal_render($vars['form']['timezone']);
$vars['submit'] = drupal_render($vars['form']['submit']);
$vars['delete'] = drupal_render($vars['form']['delete']);
}
then in the user-profile-form.tpl.php :
<div id="user-profile-form">
<?php echo $account; ?>
<?php echo $timezone; ?>
<?php echo $submit; ?>
<?php echo $delete; ?>
</div>
The edit form of the account shows correctly. I've tried adding/removing variables successfully (ie the $timezone) but the submit/delete are missing.
I don't know what's wrong..
I've tried to change the name of the variables 'submit' and 'delete' but still no button shows up. Of course I've cleared the cache every times needed (and not).
I have no JS hiding the buttons neither..
I render this form through a custom block in a Panel :
<?
module_load_include('inc', 'user', 'user.pages');
global $user;
print drupal_render(drupal_get_form('user_profile_form', $user));
?>
Maybe a problem with Panels ???
Any idea is appreciated :)
Thx for reading
Erwan
I forgot the "[action]".. :
$vars['submit'] = drupal_render($vars['form']['actions']['submit']);
$vars['cancel'] = drupal_render($vars['form']['actions']['cancel']);
And 'Delete' button didn't show up at first because it's called "cancel", and its #access param was sent to FALSE. Thx DPM ;)
Now, the problem is that when I trigger the submit button, the form is not sent, it justs reload he page. I will update if I manage to solve that too.
The page is only reloading because you forget to render the hidden form elements. To do so in your template preprocess you can use something like that :
function THEME_preprocess_user_profile_form(&$variables) {
$hidden = array();
foreach(element_children($variables['form']) as $key)
{
$type = $variables['form'][$key]['#type'];
if($type == "hidden" || $type == "token"){
$hidden[] = $variables['form'][$key];
}
}
$variables['hidden'] = $hidden;
//Dont forget to report your variables like you already did ...
}
Then when it s done render the $hidden variable in your template file
<?php print render($hidden);?>
And there you go !

cakephp view will not display

I have an order form to purchse a voucher but for some reason now when I click phurcase it displays a blank screen. I cant see what has changed and why the view is not being passed the information from the controller.
Vourchers form
<h2>Vouchers</h2>
<?php
foreach($vouchers as $v){
?>
<div id="voucher_box" class="add_shadow column span-8">
<h3><?=$v['Voucher']['title'];?></h3>
<p>
<?=$v['Voucher']['description'];?>
</p>
<?php
echo $this->Form->create('Voucher',array('id'=>'ResVoucherForm','url'=>'/res/voucher'));
echo $this->Form->input('slug',array('type'=>'hidden','value'=>$v['Voucher']['slug']));
?>
<select id="VoucherPrice" name="data[Voucher][price]">
<? $prices = explode(',',$v['Voucher']['prices'])?>
<? foreach($prices as $price){?>
<option value="<?=$price?>">€ <?=$price?></option>
<? } ?>
</select>
<div class="submit"><input type="submit" id="check_rates" value="Purchase this" class="ui-button ui-widget ui-state-default ui-corner-all" /></div>
</form>
</div>
<?
}
?>
Controller
function voucher() {
$this->layout = '360';
$msg[0] = array(); // 0 = bad messages
$msg[1] = array(); // 1 = good messages
$total_price = 0.00;
if(isset($data['Voucher'])) { $this->data['Voucher'] = $data['Voucher']; }
if(isset($this->data['Voucher'])) {
// if we have posted a voucher purchase: add it to session array
if(isset($this->data['Voucher']['slug'])){
$slug = $this->data['Voucher']['slug'];
$voucher = $this->getVoucher($slug);
$voucher['Voucher']['price'] = $this->data['Voucher']['price'];
$vouchers = $this->Session->read('Res.Vouchers'); // read existing voucher orders
if(is_array($vouchers) && isset($voucher['Voucher'])){
$temp['id'] = $voucher['Voucher']['id'];
$temp['title'] = $voucher['Voucher']['title'];
$temp['description'] = $voucher['Voucher']['description'];
$temp['slug'] = $voucher['Voucher']['slug'];
$temp['price'] = $this->data['Voucher']['price'];
$vouchers[] = $temp;
}
$this->Session->write('Res.Vouchers',$vouchers);
} else {
$vouchers = $this->Session->read('Res.Vouchers'); // read existing voucher orders
}
$this->set('voucher_orders', $vouchers);
}
This next view displays blank, I do not know how to test the information in the controller
<?php
/*
if voucher show
*/
if (isset($voucher)) {
?>
<div id="voucher_box" class="add_shadow column span-8">
<h3><?=$voucher['Voucher']['title'];?></h3>
<p>
<?=$voucher['Voucher']['description'];?>
</p>
<?php
echo $this->Form->create('Voucher',array('id'=>'ResVoucherForm','url'=>'/res/voucher'));
echo $this->Form->input('slug',array('type'=>'hidden','value'=>$voucher['Voucher']['slug']));
?>
<select id="VoucherPrice" name="data[Voucher][price]">
<? $prices = explode(',',$voucher['Voucher']['prices'])?>
<? foreach($prices as $price){?>
<option value="<?=$price?>">€ <?=$price?></option>
<? } ?>
</select>
<div class="submit"><input type="submit" id="check_rates" value="Purchase this" class="ui-button ui-widget ui-state-default ui-corner-all" /></div>
</form>
</div>
<?
}
// end if voucher show
?>
Upadate Error Returned #######################################################
Notice (8): Undefined index: id [APP/controllers/res_controller.php, line 739]
Notice (8): Undefined index: title [APP/controllers/res_controller.php, line 740]
Notice (8): Undefined index: description [APP/controllers/res_controller.php, line 741]
Notice (8): Undefined index: slug [APP/controllers/res_controller.php, line 742]
Array
(
[0] => Array
(
[id] =>
[title] =>
[description] =>
[slug] =>
[price] => 100
)
)
In the view, you have this around the whole HTML:
if (isset($voucher)) {
// ...
}
Meaning: display voucher information if there is a voucher, and do not display anything otherwise.
Now, in your controller, you don't seem to be passing a voucher variable to the view (which should be done with $this->set('varname', $var);
So, your view does not get any data, thus it does not display anything.
This could explain your issue. But if the screen is completely blank (not even the layout displays), then you must enable error logging to check what's going on.
You are setting 'voucher_orders' to your view not 'voucher' the name of the variable in the view is always the string passed in the set method. Also I don't quite understand what you are doing in the controller with the $data variable, where does it come from? Also it's redundant doing $this->data['Voucher'] = $data['Voucher'] and the checking $this->data isset if you already checked if $data is set. You might be overwriting your $this->data array, what you are doing is not necessary since the view already gives you a populated $this->data if its a form submission.
You should really try doing some tutorials and reading up a little before jumping in, you seem to not properly understand the CakePHP structure and workflow.
Thanks for all your help. By using the pr() or debug() in the controller I found the problem. It was in the function that was getting called on the line $voucher = $this->getVoucher($slug);. I now have a better understanding of cakephp.

Resources