JToolbar frontend submit value on click - joomla

I'm building a joomla component and I can't find a solution to the following. In my front end I'm using the joomlas build in class JToolbar to handle events on click like edit, delete so one.
<form action="<?php echo JRoute::_('index.php');?>" method="post"
name="termForm" id="adminForm">
<table class="stripeMe">
<tbody>
<thead>
<tr>
<th>Begriff</th>
<th>Definition</th>
<?php if ($user->authorize('com_glossary', 'edit', 'glossary', 'all')): ?><th>Published</th> <?php endif; ?>
</tr>
</thead>
<?php foreach($this->items as $i => $item): ?>
<tr>
<td>
<span class="title"><?php echo $item->tterm; ?></span>
<?php if ($user->authorize('com_glossary', 'edit', 'bearbeiten', 'all')):?>
<?php echo $this->getEdit(); ?><?php endif; ?>
</td>
<td><?php echo $item->tdefinition; ?></td>
<?php if ($user->authorize('com_glossary', 'edit', 'bearbeiten', 'all')): ?>
<td><?php echo $this->getPublished(); ?></td> <?php endif; ?>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<div>
<input type="hidden" name="task" value="" /> <input type="hidden"
name="id" value="" onclick="submitbutton(<?php echo count( $item->id ); ?>);" /> <input type="hidden"
name="option" value="com_glossary" /> <input type="hidden"
name="controller" value="bearbeiten" />
<?php echo JHtml::_('form.token'); ?>
</div>
</form>
I would like to pass to id of the selected row to the sub-controller on button event and I don't really know how to do it

Here you have some useful tips about using JToolbar on the frontend http://docs.joomla.org/How_to_use_the_JToolBar_class_in_the_frontend
I have done it once in the past, and from what I remember I did some tricks in order to make it work.
1.) Firstly remove the "id" input and add the following one at the end of your form:
<input type="hidden" name="boxchecked" value="0" />
2.) Secondly make sure Mootools is attached to the source
3.) Finally: There, where you started your foreach loop, after "tr" tag add another table column:
<td><?php echo JHTML::_('grid.id', $i, $item->id ); ?></td>
Don't forget to create a column heading in thead for this column.
These steps will create a checkbox in the first cell of every row and make the form able to send selected field's id with request.
edit:
tbody tag is in wrong place, it's supposed to be after thead tag. Also there is no use of attaching events to hidden input as they won't be triggered
Cheers
Peter

Related

Outputting the product atribute table within the product page (catalog/product/view.phtml)

I'm attempting to place the product attribute table within a div on my product page, seperately from the content of the usual collateral tabs.
I've located the code from my attributes.phtml and i've tried to just copy and paste this code into the appropriate part of my view.phtml, resulting in the following:
<div class="spec-table">
<?php if($_additional = $this->getAdditionalData()): ?>
<table class="data-table" id="product-attribute-specs-table">
<col width="25%" />
<col />
<tbody>
<?php foreach ($_additional as $_data): ?>
<?php $_attribute = $_product->getResource()->getAttribute($_data['code']);
if (!is_null($_product->getData($_attribute->getAttributeCode())) && ((string)$_attribute->getFrontend()->getValue($_product) != '')) { ?>
<tr>
<th class="label"><?php echo $this->htmlEscape($this->__($_data['label'])) ?></th>
<td class="data"><?php echo $_helper->productAttribute($_product, $_data['value'], $_data['code']) ?></td>
</tr>
<?php } ?>
<?php endforeach; ?>
</tbody>
</table>
<script type="text/javascript">decorateTable('product-attribute-specs-table')</script>
<?php endif;?>
</div>
When this didn't work, I tried invoking the attributes.phtml from within my view.phtml using:
<?php echo $this->getLayout()->createBlock('core/template')->setTemplate('catalog/product/view/attributes.phtml')->toHtml();?>
but sadly neither of these approaches seems to work, and I just end up with a blank div.
What's the best way to inject my attributes.phtml (or for that matter, the contents of any given .phtml file) into my product page at some arbitrary point?
Managed to get this to work using the following
<?php echo $this->getLayout()->createBlock('catalog/product_view_attributes')->setTemplate('catalog/product/view/attributes.phtml')->toHtml();?>

Checkbox Array Validation Codeigniter

On my form each row has it's on submit button and you need to check the check box before you can delete it else it should through error.
Question: My checkbox is in_array but if I do not check the box and then press submit it does not through the codeigniter form_validation error. I have used $this->form_validation->set_rules('selected[]', 'Selected', 'required'); But error not showing up.
What is the best solution in making it getting form_validation error to work?
View
<?php echo form_open('admin/design/layout/delete'); ?>
<?php echo validation_errors('<div class="alert alert-danger">', '</div>'); ?>
<div class="table-responsive">
<table class="table table-striped table-bordered table-hover">
<thead>
<tr>
<td style="width: 1px;" class="text-center">
<input type="checkbox" onclick="$('input[name*=\'selected\']').prop('checked', this.checked);" />
</td>
<td>Layout Name</td>
<td class="text-right">Action</td>
</tr>
</thead>
<tbody>
<?php if ($layouts) { ?>
<?php foreach ($layouts as $layout) { ?>
<tr>
<td class="text-center"><?php if (in_array($layout['layout_id'], $selected)) { ?>
<input type="checkbox" name="selected[]" value="<?php echo $layout['layout_id']; ?>" checked="checked" />
<?php } else { ?>
<input type="checkbox" name="selected[]" value="<?php echo $layout['layout_id']; ?>" />
<?php } ?>
</td>
<td><?php echo $layout['name']; ?></td>
<td class="text-right">
<button type="submit" class="btn btn-danger">Delete</button>
Edit
</td>
</tr>
<?php } ?>
<?php } else { ?>
<?php } ?>
</tbody>
</table>
</div>
</div>
<?php echo form_close(); ?>
Controller Function
public function delete() {
$this->load->library('form_validation');
$this->form_validation->set_rules('selected[]', 'Selected', 'required');
if ($this->form_validation->run() == FALSE) {
echo "Error";
$this->get_list();
} else {
$selected_post = $this->input->post('selected');
if (isset($selected_post)) {
foreach ($selected_post as $layout_id) {
}
echo "Deleted $layout_id";
$this->get_list();
}
}
}
It won't validate per field. selected[] selector in rules means, when you submit your form, it should be at least one selected item. Now you have submit buttons, which are independently submit the form, no matter where are they in the dom, and which checkboxes are selected.
Currently it the same, as you would have one submit button at the end.
I would add some javascript, and set if the checkbox is not selected, you can disable that field:
<script>
$(function() {
$('input:checkbox').on('change', function() {
if($(this).is(':checked')) {
$(this).closest('tr').find('button:submit').prop('disabled', false);
}
else {
$(this).closest('tr').find('button:submit').prop('disabled', true);
}
})
})
</script>
And add disabled to your submit buttons:
<button type="submit" class="btn btn-danger" disabled>Delete</button>
It's not a server side validation, but you can achieve to prevent push the button next unchecked checkboxes.

magento output custom options

Is it possible to output the available list of custom options onto the view.phtml page within Magento? Obviously the drop down lists allow you to filter down but I need to list them in a html table and allow the customer to add to cart on each item.
I will show you how to convert dropdown and multi-select options into table format. This is the code that do the trick.
<?php
$product = Mage::Registry('current_product');
$options = $product->getOptions();
//check for option exists
if($options):
foreach ($options as $option) :
//table format allows only for select options
if($option->getType()== Mage_Catalog_Model_Product_Option::OPTION_TYPE_DROP_DOWN ||
$option->getType() == Mage_Catalog_Model_Product_Option::OPTION_TYPE_MULTIPLE):
$require = ($option->getIsRequire()) ? ' required-entry' : '';
if($option->getType()== Mage_Catalog_Model_Product_Option::OPTION_TYPE_DROP_DOWN):
$value_name = 'options['.$option->getid().']';
else:
$value_name = 'options['.$option->getid().'][]';
endif;
?>
<table border="1" style="width:500px;margin-bottom:30px">
<thead>
<tr<?php if ($option->getIsRequire()) echo ' class="required"' ?>>
<th><?php echo $option->getTitle() ?></th>
<th><?php if ($option->getIsRequire()) echo '<em>*</em>' ?></th>
</tr>
</thead>
<tbody id="<?php echo 'select_'.$option->getId() ?>" class="<?php echo $require.' product-custom-option' ?>">
<?php
foreach ($option->getValues() as $values) :
$value = $values->getOptionTypeId();
$value_title = $values->getTitle();
?>
<tr>
<td>
<?php
if($option->getType()== Mage_Catalog_Model_Product_Option::OPTION_TYPE_DROP_DOWN):
?>
<input type="radio" name="<?php echo $value_name; ?>" class="radio_option" id="<?php echo 'radio_option_'.$value ?>" value="<?php echo $value; ?>" />
<?php
else:
?>
<input type="checkbox" name="<?php echo $value_name; ?>" class="checkbox_option" id="<?php echo 'checkbox_option_'.$value ?>" value="<?php echo $value; ?>" />
<?php
endif;
?>
</td>
<td><?php echo $value_title; ?></td>
</tr>
<?php
endforeach;
?>
</tbody>
</table>
<?php
endif;
endforeach;
endif;
?>
Here for dropdown options, we are using radio button to show their values. For multiselect options, checkbox inputs are used. So if checkbox appears in the table, that means it a multi select option and hence more than one checkboxes can have tick at a time. Since radio buttons are used for dropdown options, it is important to make sure that, only one radio button is checked at a time. (We need to use javascript for ensure this).
So if we are set some custom options for a product through admin like this..
The output correspond to our code will be look like this
Note: Please note that, the code is in very basic format. You may require additional css and javascripts make it more user frontly. This is for giving you a basic idea and additional css, js are hence out of box topic.
For more information about code, check out my blog
Hope it helps

Fetch the entered quantity box values from multiple quantity box to cart page in magento

I am new to Magento.
I am displaying the attributes in table format using foreach in product detail page.
Problem i am facing is when i enter the value in any quantity box, it takes the last quantity value to the cart page since i am using foreach loop to display the quantity box.I need to display the exact value which i have entered in the quantity box.
Code:
<table border="1" style="width:100%;">
<thead>
<tr>
<th>Fabric</th>
<th>Color</th>
<th>Required Qty</th>
<th>Total Cost</th>
<th>Cart</th>
</tr>
</thead>
<?php
$attribute = Mage::getModel('eav/config')->getAttribute('catalog_product', 'fabric');
$buttonTitle = $this->__('Add to Cart');
$i=0;foreach ( $attribute->getSource()->getAllOptions(true, true) as $option){
$i++;
if($i==1) continue;
?>
<tbody>
<tr>
<td>
<?php echo $attributeArray[$option['value']] = $option['label'];
?>
</td>
<td>
<select name="color" id="color">
<option>Select Color</option>
<?php
$color = Mage::getModel('eav/config')->getAttribute('catalog_product', 'fabric_color');
$j=0;foreach ( $color->getSource()->getAllOptions(true, true) as $option_color){
$j++;
if($j==1) continue;?>
<option value="<?php echo $option_color['value'];?>" price="<?php echo $option_color['price'];?>"><?php echo $option_color['label'];?></option>
<? }
?>
</select>
</td>
<td>
<?php if($_product->isSaleable()): ?>
<div class="add-to-cart">
<?php if(!$_product->isGrouped()): ?>
<label for="qty"><?php echo $this->__('Qty:') ?></label>
<div class="qty_pan">
<input type="text" name="qty" id="qty<?=$i;?>" maxlength="12" value="1" title="<?php echo $this->__('Qty') ?>" class="input-text qty" />
</div>
<?php endif; ?>
</div>
<?php endif; ?>
</td>
<td ><span class="qty<?=$i;?>" id=""><?php echo $_product->getPrice();?></span></td>
<td>
<button type="button" title="<?php echo $buttonTitle ?>" class="button btn-cart" onclick="productAddToCartForm.submit(this)"><span><span><?php echo $buttonTitle ?></span></span></button>
<!-- <span id='ajax_loader' style='display:none'><img src='<?php echo $this->getSkinUrl('images/opc-ajax-loader.gif')?>'/></span>-->
<?php //echo $this->getChildHtml('', true, true) ?>
</td>
</tr>
</tbody>
<?php }?>
</table>
Screenshot:
https://imageshack.us/scaled/large/12/detailpage.png
Any help is appreciated!.
I Got The solution.
We can use Query String to fetch the quantity values to cart.
Yourdomain.com/checkout/cart/add?product=getId();?>&qty=1
You can use the above querystring in href.
Hope this Helps.

How to make CodeIgniter respect line breaks on message when sending an email

How can I make codeigniter to send an email while respecting the line breaks from the message field?
the form message - http://d.pr/Sae5
<?php echo form_open($this->uri->uri_string()); ?>
<table class="forms-table">
<tr>
<td>
<label for="name">Name</label>
</td>
<td>
<input type="text" id="name" name="name" value="<?php echo set_value('name'); ?>" />
</td>
<td>
<?php echo form_error('name'); ?>
</td>
</tr>
<tr>
<td>
<label for="email">Email</label>
</td>
<td>
<input type="text" id="email" name="email" value="<?php echo set_value('email'); ?>" />
</td>
<td>
<?php echo form_error('email'); ?>
</td>
</tr>
<tr>
<td>
<label for="message">Message</label>
</td>
<td>
<textarea name="message" id="message" cols="40" rows="6"><?php echo set_value('message'); ?></textarea>
</td>
<td>
<?php echo form_error('message'); ?>
</td>
</tr>
<tr>
<td colspan="3">
<input type="submit" value="submit" />
</td>
</tr>
</table>
<?php echo form_close(); ?>
When I get the email, I get "hi, i am awesome" all in one line. I have the newline and crlf config set to "\r\n", charset is "utf-8", and i get the value of my message field using
$message = $this->input->post('message');
...
$this->email->message($message);
any thoughts?
why not send the email has html ?
first you must prepare the message (I'm assuming that you're using POST)
$message = str_replace ("\r\n", "<br>", $this->input->post('message') );
or you can use the native php way to get $_POST
$message = str_replace ("\r\n", "<br>", $_POST['message'] );
What you've done, is to replace new lines with <br>
Then you just need to load the lib and set it correctly trough config, for example:
$this->load->library('email');
$config['mailtype'] = 'html';
$this->email->initialize($config);
$this->email->from('your#example.com', 'Your Name');
$this->email->to('someone#example.com');
$this->email->subject('Email Test');
$this->email->message( $message );
$this->email->send();
That's it! I hope this helps,
You can get more info on http://codeigniter.com/user_guide/libraries/email.html
Hopefully you'll take the time to read it!
Just to add, you can simplify this process using nl2br and simply ->mailtype = 'html';. Like so:
$message = nl2br($this->input->post('message')); // https://codeigniter.com/user_guide/libraries/input.html
$this->load->library('email'); // use autoload.php to remove this line
$this->email->mailtype = 'html';
Also, if you want to create a config to use all the time, you can actually create a config file and CI will use it automatically, thus you never need to use ->initialize. To do so, just follow these simple steps:
in the directory of application\config, create a file named email.php
Then you can simply write in your configuration in that file like so:
`$config['mailtype'] = 'html';`
Viola! You're done. It's just that simple! Now just call your email class and use as usual with no need to configure things like mailtype. You can see full list of email config options under the title Email Preferences here. Don't forget, you can use the application\config\autoload.php to load the email library automatically, thus removing this line $this->load->library('email'); from your code.

Resources