Filter admin grid with custom render values Magento - magento

i have created a custom grid in which one of the columns i have render
my custom date.On the basis of data i am returning
yes or no
like below
public function render(Varien_Object $row)
{
$currentruleid = $this->getRequest()->getParam('id');
$value = (int)$row->getData($this->getColumn()->getIndex());
$read = Mage::getSingleton('core/resource')->getConnection('core_read');
$write = Mage::getSingleton("core/resource")->getConnection("core_write");
$query = "SELECT exclusive_coupon_id FROM mutually_exclusive
WHERE rule_id ='$currentruleid' AND exclusive_coupon_id ='$value' ";
$result = $read->query($query);
$affected_rows = $result->rowCount();
if($affected_rows > 0){
return 'Yes';
}
else{
return 'No';
}
}
and in grid my column code is
$this->addColumn('', array(
'header' => Mage::helper('salesrule')->__('Exclusive'),
'index' => 'coupon_id',
'width' => '100',
'type' => 'options',
'options' => array(
Mage::helper('adminhtml')->__('No'),
Mage::helper('adminhtml')->__('Yes')
),
'renderer' => 'adminhtml/promo_quote_edit_tab_exclusivecoupons_grid_column_renderer_used',
));
my renderer is returning accurate data
and its showing in columns
i need to filter also with yes or no. But filtering is not working.
Can you please suggest me how can i do this.
thanks

i have check that but my renderer is not any direct field , its a custom data like below is my renderer function
public function render(Varien_Object $row)
{
$currentruleid = $this->getRequest()->getParam('id');
$value = (int)$row->getData($this->getColumn()->getIndex());
$read = Mage::getSingleton('core/resource')->getConnection('core_read');
$write = Mage::getSingleton("core/resource")->getConnection("core_write");
$query = "SELECT exclusive_coupon_id FROM mutually_exclusive
WHERE rule_id ='$currentruleid' AND exclusive_coupon_id ='$value' ";
$result = $read->query($query);
$affected_rows = $result->rowCount();
if($affected_rows > 0){
return 'Yes';
}
else{
return 'No';
}
}

Related

Error updating data of 1 column by id in Codeigniter 3 database

I am posting the hosting order and I want to increase the "number of sales" column in my "hostings" table, but the data of all packages in my hostings table is increasing.
Here is my relevant code, I can say that there is no method I haven't tried.
$hostingdata = $this->db->query("select number_of_sales from hostings where id=" . $this->input->post('package_id'))->row();
$quantity = 1;
$new_number_of_sales = $hostingdata->number_of_sales + $quantity;
$data = array(
'number_of_sales' => $new_number_of_sales
);
$this->db->update('hostings', $data);
//FULL CODE
public function buy_hosting_post()
{
if ($this->input->post('package_id') && $this->session->userdata('id')) {
$hosting = $this->db->from('hostings')->where('id', $this->input->post('package_id'))->get()->row_array();
if ($hosting) {
$data = array(
'user_id' => $this->session->userdata('id'),
'domain' => $this->input->post('domain'),
'price' => $this->input->post('price'),
// 'end_date' => date('Y-m-d h:i:s',$end_date),
'package_id' => $hosting['id'],
'package_title' => $hosting['name'],
'payment_status' => 0,
);
$this->db->insert('hosting_orders', $data);
$hostingdata = $this->db->query("select number_of_sales from hostings where id=" . $this->input->post('package_id'))->row();
$quantity = 1;
$new_number_of_sales = $hostingdata->number_of_sales + $quantity;
$data = array(
'number_of_sales' => $new_number_of_sales
);
$this->db->update('hostings', $data);
$hosting_order_successful = array(
'hosting_order_successful' => 'success',
);
$this->session->set_userdata('hosting_order_successful', $hosting_order_successful);
redirect(("hosting-siparisi-olusturuldu"));
} else {
redirect(base_url());
}
} else {
redirect(base_url());
}
}
I solved the problem in the following way, but it makes the system heavy.
$hostingdata = $this->db->query("select number_of_sales from hostings where id=" . $this->input->post('package_id'))->row();
$quantity = 1;
$new_number_of_sales = $hostingdata->number_of_sales + $quantity;
$data = array(
'number_of_sales' => $new_number_of_sales
);
$this->db->where('id', $this->input->post('package_id')); // added here.
$this->db->update('hostings', $data);

codeigniter how to store real time data

public function insert_employee($fldCompanyStringID) {
$fldCompanyID = getCoompanyByStringID($fldCompanyStringID)->fldCompanyID;
$data = array(
'fldUserFName' => $this->input->post('fldUserFName'),
'fldUserBankAccountNumber' => $this->input->post('fldUserBankAccountNumber')
);
$data2 = array(
'fldWorkHistoryCompanyName' => $this->input->post('fldWorkHistoryCompanyName')
);
if ($this->db->insert('tblUser', $data)&& $this->db->insert(' tblWorkHistory', $data2)) {
$this->session->set_flashdata('success_msg', 'New Employee is inserted');
}
}
The tblUser table auto generates a userID. I want to take that userID and store it into the tblWorkHistory table **
Here CodeIgniter transactions will help you.
https://www.codeigniter.com/user_guide/database/transactions.html
Please use this code --
<?php
public function insert_employee($fldCompanyStringID) {
$this->db->trans_start();
$fldCompanyID = getCoompanyByStringID($fldCompanyStringID)->fldCompanyID;
/* Insert User */
$data = array(
'fldUserFName' => $this->input->post('fldUserFName'),
'fldUserBankAccountNumber' => $this->input->post('fldUserBankAccountNumber')
);
$this->db->insert('tblUser', $data)
$insert_id = $this->db->insert_id();
/* Insert Work History */
$data2 = array(
'userID' => $insert_id,
'fldWorkHistoryCompanyName' => $this->input->post('fldWorkHistoryCompanyName')
);
$this->db->insert('tblWorkHistory', $data2)
/* Manage Transaction */
$this->db->trans_complete();
if ($this->db->trans_status() === FALSE){
$this->session->set_flashdata('error_msg', 'Failed, please try again');
}else{
$this->session->set_flashdata('success_msg', 'New Employee is inserted');
}
}
?>
$this->db->insert_id() is used to get the last insert auto increment id data.
$this->db->insert('tblUser', $data);
$insert_id = $this->db->insert_id();
if($insert_id) {
$data2 = array(
'userID' => $insert_id,
'fldWorkHistoryCompanyName' => $this->input->post('fldWorkHistoryCompanyName')
);
if ($this->db->insert(' tblWorkHistory', $data2)) {
$this->session->set_flashdata('success_msg', 'New Employee is inserted');
}
} else {
$this->session->set_flashdata('error_msg', 'Something went wrong');
}

Filter Orders based on product_id or user_id(Vendor)

I have followed How to create a custom grid from scratch to create custom Sales Orders. Admin is creating vendors and vendors will be uploading product. I want to restrict vendors such that they can see orders placed on their own product only.
There is one new model jbmarketplace/jbmarketplaceproducts in which vendors user_id and product_id is being stored when vendor creates product. But when I'm filtering it gives SQLSTATE[42S22]: Column not found: 1054 Unknown column 'product_id' in 'where clause'. But product_id is available in sales_flat_order_item table.
This problem is Fixed. Updated Code
protected function _prepareCollection()
{
// Get current logged in user
$current_user = Mage::getSingleton( 'admin/session' )->getUser();
// Limit only for vendors
if ( $current_user->getRole()->getRoleId() == Mage::getStoreConfig( 'jbmarketplace/jbmarketplace/vendors_role' ) ) {
// echo( $current_user->getUserId());
$my_products = Mage::getModel( 'jbmarketplace/jbmarketplaceproducts' )
->getCollection()
->addFieldToSelect( 'product_id' )
->addFieldToFilter( 'user_id', $current_user->getUserId() )
->load();
$my_product_array = array();
foreach ( $my_products as $product ) {
$my_product_array[] = $product->getProductId();
$entity = Mage::getModel('sales/order_item')
->getCollection()
->addFieldToSelect('order_id')
->addFieldToFilter('product_id',$my_product_array)
->load();
// echo $entity->getSelect();// will print sql query
}
$d=$entity->getData();
if($d){
$collection = Mage::getResourceModel('sales/order_collection')
// My code
->addFieldToFilter('entity_id', $d)
->join(array('a' => 'sales/order_address'), 'main_table.entity_id = a.parent_id AND a.address_type != \'billing\'', array(
'city' => 'city',
'country_id' => 'country_id'
))
// ->join(Mage::getConfig()->getTablePrefix().'catalog_product_entity_varchar', 'main_table.products_id ='.Mage::getConfig()->getTablePrefix().'catalog_product_entity_varchar.entity_id',array('value'))
->join(array('c' => 'customer/customer_group'), 'main_table.customer_group_id = c.customer_group_id', array(
'customer_group_code' => 'customer_group_code'
))
->addExpressionFieldToSelect(
'fullname',
'CONCAT({{customer_firstname}}, \' \', {{customer_lastname}})',
array('customer_firstname' => 'main_table.customer_firstname', 'customer_lastname' => 'main_table.customer_lastname'))
->addExpressionFieldToSelect(
'products',
'(SELECT GROUP_CONCAT(\' \', x.name)
FROM sales_flat_order_item x
WHERE {{entity_id}} = x.order_id
AND x.product_type != \'configurable\')',
array('entity_id' => 'main_table.entity_id')
)
;
parent::_prepareCollection();
$this->setCollection($collection);
return $this;
}
else
{
echo("Current there are no purchases on your product. Thank you");
}
}
else{
echo("Please Login as Vendor and you will see orders on your products.<br>");
// $current_user = Mage::getSingleton( 'admin/session' )->getUser()->getUserId();
// echo($current_user);
}
}
Here is the code which worked for me.
protected function _prepareCollection()
{
// Get current logged in user
$current_user = Mage::getSingleton( 'admin/session' )->getUser();
// Limit only for vendors
if ( $current_user->getRole()->getRoleId() == Mage::getStoreConfig( 'jbmarketplace/jbmarketplace/vendors_role' ) ) {
// echo( $current_user->getUserId());
$my_products = Mage::getModel( 'jbmarketplace/jbmarketplaceproducts' )
->getCollection()
->addFieldToSelect( 'product_id' )
->addFieldToFilter( 'user_id', $current_user->getUserId() )
->load();
$my_product_array = array();
foreach ( $my_products as $product ) {
$my_product_array[] = $product->getProductId();
$entity = Mage::getModel('sales/order_item')
->getCollection()
->addFieldToSelect('order_id')
->addFieldToFilter('product_id',$my_product_array)
->load();
// echo $entity->getSelect();// will print sql query
}
$d=$entity->getData();
if($d){
$collection = Mage::getResourceModel('sales/order_collection')
// My code
->addFieldToFilter('entity_id', $d)
->join(array('a' => 'sales/order_address'), 'main_table.entity_id = a.parent_id AND a.address_type != \'billing\'', array(
'city' => 'city',
'country_id' => 'country_id'
))
// ->join(Mage::getConfig()->getTablePrefix().'catalog_product_entity_varchar', 'main_table.products_id ='.Mage::getConfig()->getTablePrefix().'catalog_product_entity_varchar.entity_id',array('value'))
->join(array('c' => 'customer/customer_group'), 'main_table.customer_group_id = c.customer_group_id', array(
'customer_group_code' => 'customer_group_code'
))
->addExpressionFieldToSelect(
'fullname',
'CONCAT({{customer_firstname}}, \' \', {{customer_lastname}})',
array('customer_firstname' => 'main_table.customer_firstname', 'customer_lastname' => 'main_table.customer_lastname'))
->addExpressionFieldToSelect(
'products',
'(SELECT GROUP_CONCAT(\' \', x.name)
FROM sales_flat_order_item x
WHERE {{entity_id}} = x.order_id
AND x.product_type != \'configurable\')',
array('entity_id' => 'main_table.entity_id')
)
;
parent::_prepareCollection();
$this->setCollection($collection);
return $this;
}
else
{
echo("Current there are no purchases on your product. Thank you");
}
}
else{
echo("Please Login as Vendor and you will see orders on your products.<br>");
// $current_user = Mage::getSingleton( 'admin/session' )->getUser()->getUserId();
// echo($current_user);
}
}

Add total in magento custom module grid footer

i have added sales order grid in my custom module.and also get footer total row in the grid. but the problem is when i add 'adult' column in the total it display nothing.for adult,child,total i m using renderer to get row data from order.so how to get total for adult,child,total at the footer.
i have used below code to display footer totals in grid.php
protected $_countTotals = true;
public function getTotals()
{
$totals = new Varien_Object();
$fields = array(
'base_grand_total' => 0,
'adult'=>0,
//actual column index, see _prepareColumns()
);
foreach ($this->getCollection() as $item) {
foreach($fields as $field=>$value){
$fields[$field]+=$item->getData($field);
}
}
//First column in the grid
$fields['increment_id'] = 'Totals';
$totals->setData($fields);
return $totals;
}
Thanks
Add this function to the grid.php
public function getReport($from, $to) {
if ($from == '') {
$from = $this->getFilter('report_from');
}
if ($to == '') {
$to = $this->getFilter('report_to');
}
$totalObj = Mage::getModel('reports/totals');
$totals = $totalObj->countTotals($this, $from, $to);
$this->setTotals($totals);
$this->addGrandTotals($totals);
return $this->getCollection()->getReport($from, $to);
}
and also in adding column add 'total' => 'sum' as below (I think you made this but once verify it properly):
$this->addColumn('item_id', array(
'header' => Mage::helper('mymodule')->__('Item ID'),
'align' => 'right',
'index' => 'item_id',
'type' => 'number',
'total' => 'sum',
));

CodeIgniter Captcha is not working

I am at my wits end and don't know why my code is not working . Everything seems good to me .. I have implemented a captcha in my user review function and added a verification method using callback_ .
The captcha is showing in the view and i have dumped the session data and the input field data and they are both working.
The form validation is also working in case of captcha input field but seems like the callback_check_captcha parameter is not working but the function seems fine to me .
Here is my controller
function user_review($id = null , $start = 0){
$check_id = $this->mdl_phone->get_phone_feature($id);
if ($check_id == null ) {
$data['phone_model'] = $this->get_phone_models();
$data['feature'] = null;
$data['title'] = 'Nothing Found';
$data['main_content']= 'phone/error';
echo Modules::run('templates/main',$data);
} else{
$data['phone_model'] = $this->get_phone_models();
$data['success'] = null;
$data['errors'] = null;
if($this->input->server("REQUEST_METHOD") === 'POST' ){
$this->load->library('form_validation');
$this->form_validation->set_rules('text','Review','required|xss_clean');
$this->form_validation->set_rules('captcha', 'Captcha','trim|required|callback_check_captcha');
if($this->form_validation->run() == FALSE){
$data['errors'] = validation_errors();
}else{
$user = $this->ion_auth->user()->row();
$user_id = $user->id;
$data = array(
'phone_id' => $id,
'user_id' => $user_id,
'text' => strip_tags($this->input->post('text')),
);
$this->db->insert('user_review' ,$data);
$data['phone_model'] = $this->get_phone_models();
$data['success'] = 'Your Review has been successfully posted ';
$data['errors']= null;
}
}
// Initilize all Data at once by $id
$data['feature'] = $this->mdl_phone->get_phone_feature($id);
//$data['rating'] = $this->mdl_phone->get_user_rating($id);
$data['user_review'] = $this->mdl_phone->get_user_review($id , 5 , $start);
$this->load->library('pagination');
$config['base_url'] = base_url().'phone/user_review/'.$id;
$config['total_rows'] = $this->mdl_phone->get_user_review_count($id);
$config['per_page'] = 5;
$config['uri_segment'] = 4;
$config['anchor_class'] = 'class="page" ';
$this->pagination->initialize($config);
$this->load->helper('captcha');
$vals = array(
'img_path' => './captcha/',
'img_url' => base_url().'captcha/',
'img_width' => 150,
'img_height' => 30,
);
$cap = create_captcha($vals);
$this->session->set_userdata('captcha',$cap['word']);
$data['captcha'] = $cap['image'];
$data['title'] = $this->mdl_phone->get_phone_title($id)." User Review , Rating and Popularity";
$data['main_content'] = 'phone/user_review';
echo Modules::run('templates/main',$data);
}
}
function check_captcha($cap)
{
if($this->session->userdata('captcha') == $cap )
{
return true;
}
else{
$this->form_validation->set_message('check_captcha', 'Security number does not match.');
return false;
}
}
The bellow CAPTHA working properly refer this code
$this->load->helper('captcha');
$vals = array(
'img_path' => './captcha/',
'img_url' => base_url() . '/captcha/'
);
$cap = create_captcha($vals);
$data = array(
'captcha_time' => $cap['time'],
'ip_address' => $this->input->ip_address(),
'word' => $cap['word']
);
$this->session->set_userdata($data);
$data['cap_img'] = $cap['image'];
I think your image url problem or you are not giving a file permission to the captha folder .

Resources