Post by drest341 » Fri Feb 08, 2019 1:00 am

Hi friends.
I 've created a module that searches special prices from product_special table for OC2.3, between given dates from a form and display them with ajax in same page. I 'm confused cause i 'm not sure if the form not pass the values to controller or my controller is misconfigured and doesn't pass them to model. The only results i get, are products saved with dates 0000-00-00. Any help is very appreciated. :-(
My view tpl contains..

Code: Select all

.........
<div class="form-group">
            <div class="col-sm-2">
              <div class="input-group date">
                <input type="text" id="datestart" name="datestart" value="" placeholder="<?php echo $text_date_start; ?>" data-date-format="YYYY-MM-DD" class="form-control" />
                <span class="input-group-btn">
                <button type="button" class="btn btn-default"><i class="fa fa-calendar"></i></button>
                </span>
              </div>
            </div>
            <div class="col-sm-2">
              <div class="input-group date">
                <input type="text" id="dateend" name="dateend" value="" placeholder="<?php echo $text_date_end; ?>" data-date-format="YYYY-MM-DD" class="form-control" />
                <span class="input-group-btn">
                <button type="button" class="btn btn-default"><i class="fa fa-calendar"></i></button>
                </span>
              </div>
            </div>
     <button type="button" id="button_search" name="button_search" class="btn btn-primary"><i class="fa fa-search"></i> </button>
     <button type="reset" data-toggle="tooltip" title="<?php echo $button_cancel; ?>" class="btn btn-primary"> <?php echo $button_cancel; ?></button>
        </div><br>
 <div id="product_summary"></div>

    </div>
 </div>
 </div>
 </div>

  <script type="text/javascript"><!--

 $('#button_search').on('click', function() {

  var datestart = $('input[name=\'datestart\']').val();
  var dateend = $('input[name=\'dateend\']').val();
     $.ajax({
  url: 'index.php?route=extension/module/massSpecialPrices/search&token=<?php echo $_GET["token"] ?>&datestart=' + this.value+'&dateend=' + this.value,
  dataType: 'html',
  success: function(htmlText) {
    $('#product_summary').html(htmlText);
  },
  error: function(xhr, ajaxOptions, thrownError) {
    alert(thrownError + "\r\n" + xhr.statusText + "\r\n" +   xhr.responseText);
    }
   });
 });
 //--></script>

    <script type="text/javascript"><!--
 $('.date').datetimepicker({
  pickTime: false
 });
 //--></script>
<?php echo $footer; ?>
the controller is...

Code: Select all

<?php
 class ControllerExtensionModuleMassSpecialPrices extends Controller {
 private $error = array();

 public function index() {

  .......


 public function search() {

$this->load->language('extension/module/massSpecialPrices');
$this->document->setTitle($this->language->get('heading_title'));

 //load the model defined    
 $this->load->model('extension/module/massSpecialPrices');
//It checks for a valid POST request
if ($this->request->server['REQUEST_METHOD'] == 'POST' && $this->validate()) {
// prepare the $data array based of tpl text input and call the getProductSpecials method

$data['datestart'] = $this->request->post['datestart'];
$data['dateend'] = $this->request->post['dateend'];

$this->model_extension_module_massSpecialPrices->getProductSpecials($data);

}

$results = $this->model_extension_module_massSpecialPrices->getProductSpecials($data);

        foreach ($results as $special) {
            $data['results'][] = array(
                'product_id' => $special['product_id'],
                'price'      => $special['price'],
                'date_start' => $special['date_start'], 
                'date_end'   => $special['date_end']
            );
        }

 // pass information from controller method to the view template.
    $data['text_model'] = $this->language->get('text_model');
    $data['column_total'] = $this->language->get('column_total');
    $data['text_date_start'] = $this->language->get('text_date_start');
    $data['text_date_end'] = $this->language->get('text_date_end');

    $this->response->setOutput($this->load->view('extension/module/massSpecialPrices_ajax_product', $data));

 } 
And the model file ..

Code: Select all

<?php
  class ModelExtensionModuleMassSpecialPrices extends Model {
  public function getProductSpecials($data) {

    if (isset($data['datestart'])) {
        $datestart = $this->db->escape($data['datestart']);
    } else {
    $datestart = '0000-00-00';
    }

    if (isset($data['dateend'])) {
        $dateend = $this->db->escape($data['dateend']);
    } else {
    $dateend = '0000-00-00';
    }

  $sql = "SELECT * FROM " . DB_PREFIX ."product_special WHERE 
  (date_start BETWEEN '$datestart' AND  ' $dateend' ) AND 
  (date_end BETWEEN '$datestart' AND  '$dateend' ) group by product_id ";   

    $query = $this->db->query($sql);

    return $query->rows;
}
}

Any fool can write code that a computer can understand. Good programmers write code that humans can understand ~ Martin Fowler.


User avatar
New member

Posts

Joined
Fri Feb 21, 2014 10:47 pm

Post by straightlight » Fri Feb 08, 2019 5:26 am

There are already built-in examples from the default template and the admin template on how to output the dates. For instance, in the admin order report template files, the date is being applied between from and to dates. No controller files are required to be used in this case since JS files are already implemented into the header template file in order to use proper keys and values to capture the date and time.

The most generated errors being found on Opencart forum originates from contributed programming. The increased post counters are caused by redundancies of the same solutions that were already provided prior.


Regards,
Straightlight
Opencart.com contractor officials representative


Legendary Member

Posts

Joined
Mon Nov 14, 2011 11:38 pm
Location - Canada, ON

Post by drest341 » Fri Feb 08, 2019 4:47 pm

straightlight wrote:
Fri Feb 08, 2019 5:26 am
There are already built-in examples from the default template and the admin template on how to output the dates. For instance, in the admin order report template files, the date is being applied between from and to dates. No controller files are required to be used in this case since JS files are already implemented into the header template file in order to use proper keys and values to capture the date and time.
Yes right, my form was based on this report tpl and controller as well , i assume values are passed in the controller php file, but for some unknown reason for me, i can't understand what i'm missing.
Thanks for your reply.

Any fool can write code that a computer can understand. Good programmers write code that humans can understand ~ Martin Fowler.


User avatar
New member

Posts

Joined
Fri Feb 21, 2014 10:47 pm

Post by OSWorX » Fri Feb 08, 2019 7:40 pm

You should use the correct method on your template file.
Now you are sending the date data with get, but inside the controller you use post:

Code: Select all

$data['datestart'] = $this->request->post['datestart'];
$data['dateend'] = $this->request->post['dateend'];
That never wont work ..

Forum Rules [en]: viewtopic.php?f=176&t=200480
Commercial Request: viewforum.php?f=88
Dedicated Support: https://dedicated.opencart.com/
Marketplace Support: https://www.opencart.com/index.php?rout ... rt/support
Documentation: http://docs.opencart.com/en-gb/extension/theme/

Image Image Image


User avatar
Guru Member

Posts

Joined
Mon Jan 11, 2010 10:52 pm
Location - Austria

Post by pprmkr » Fri Feb 08, 2019 7:52 pm

Code: Select all

<?php
  class ModelExtensionModuleMassSpecialPrices extends Model {
  public function getProductSpecials($data) {

	$sql = "SELECT * FROM " . DB_PREFIX ."product_special";
  
	if (!empty($data['datestart']) || !empty($data['dateend'])) {
	
  		$sql .= " WHERE product_id > '0'";
  	
		if (!empty($data['datestart'])) {
			$sql .= " AND DATE(date_start) >= '" . $this->db->escape($data['datestart']) . "'";
		}

		if (!empty($data['dateend'])) {
			$sql .= " AND DATE(date_end) <= '" . $this->db->escape($data['dateend']) . "'";
		}
	}
		
	$sql .= " GROUP BY product_id";
	
    $query = $this->db->query($sql);

    return $query->rows;
  }
}

User avatar
Active Member
Online

Posts

Joined
Sat Jan 08, 2011 11:05 pm
Location - Netherlands

Post by drest341 » Fri Feb 08, 2019 8:41 pm

Model file is working well, but data are not captured by model to retrieve them from DB, even the controller was changed as...
....
if (isset($this->request->get['datestart'])) {
$data['datestart'] = $this->request->get['datestart'];
}
....
also my function call in tpl ajax script is correct in URL "search"

Any fool can write code that a computer can understand. Good programmers write code that humans can understand ~ Martin Fowler.


User avatar
New member

Posts

Joined
Fri Feb 21, 2014 10:47 pm

Post by drest341 » Sat Feb 09, 2019 4:49 pm

Unfortunately i'm still stacked, no progress was made!

Any fool can write code that a computer can understand. Good programmers write code that humans can understand ~ Martin Fowler.


User avatar
New member

Posts

Joined
Fri Feb 21, 2014 10:47 pm

Post by pprmkr » Sat Feb 09, 2019 11:53 pm

Script in tpl:

Code: Select all

 var datestart = $('input[name=\'datestart\']').val();
  var dateend = $('input[name=\'dateend\']').val();
     $.ajax({
  url: 'index.php?route=extension/module/massSpecialPrices/search&token=<?php echo $_GET["token"] ?>&datestart=' + this.value+'&dateend=' + this.value,
Change into:

Code: Select all

 var datestart = $('input[name=\'datestart\']').val();
  var dateend = $('input[name=\'dateend\']').val();
     $.ajax({
  url: 'index.php?route=extension/module/massSpecialPrices/search&token=<?php echo $_GET["token"] ?>&datestart=' +datestart+'&dateend=' + dateend,
And in model:

Code: Select all

    if (isset($data['datestart'])) {
        $datestart = $this->db->escape($data['datestart']);
    } else {
    $datestart = '0000-00-00';
    }

    if (isset($data['dateend'])) {
        $dateend = $this->db->escape($data['dateend']);
    } else {
    $dateend = '0000-00-00';
    }
Into:

Code: Select all

 	$data['datestart'] = !empty($this->request->get['datestart'])?$this->request->get['datestart']:'0000-00-00';
	$data['dateend'] = !empty($this->request->get['dateend'])?$this->request->get['dateend']:'0000-00-00';
Query:

Code: Select all

	$sql = "SELECT * FROM " . DB_PREFIX ."product_special";
	$sql .= " WHERE (date_start BETWEEN '" . $this->db->escape($data['datestart']) ."' AND  '" . $this->db->escape($data['dateend']) ."')";
	$sql .= " AND (date_end BETWEEN '" . $this->db->escape($data['datestart']) ."' AND  '" . $this->db->escape($data['dateend']) ."')";	
	$sql .= " GROUP BY product_id";
	
	$query = $this->db->query($sql);

User avatar
Active Member
Online

Posts

Joined
Sat Jan 08, 2011 11:05 pm
Location - Netherlands

Post by drest341 » Mon Feb 11, 2019 2:22 am

Hey pprmkr, you made my day.
It works like a charm, many thanks for your time.
I realised why the input value is declared like "...+'&dateend=' + dateend,".
In other implementations with select in the form i use '&dateend=' + this.value.
:-)

Any fool can write code that a computer can understand. Good programmers write code that humans can understand ~ Martin Fowler.


User avatar
New member

Posts

Joined
Fri Feb 21, 2014 10:47 pm
Who is online

Users browsing this forum: No registered users and 2 guests