Post by opencart-templates » Wed Oct 24, 2012 8:23 pm

Lets say I have a very simple extension which adds a new field into the product form, with vqmod where we need to replace the full line.

If this was split onto multi-lines then you could <search> for only the particular part of the SQL we needed to edit.

This would help with:
1. backwards compatible with OC versions
2. Compatible with other extensions which may have already modified the same line.

E.g at the moment to add a field into the product we have to do something similar to this

Code: Select all

<file name="admin/controller/catalog/product.php">
       <operation>
           <search position="before"><![CDATA[
           $this->data['entry_meta_keyword'] = $this->language->get('entry_meta_keyword');
           ]]></search>
           <add><![CDATA[
           $this->data['entry_holiday'] = $this->language->get('entry_holiday');
           ]]></add>
       </operation>
       <operation>
           <search position="before"><![CDATA[
           if (isset($this->request->post['status'])) {
           ]]></search>
           <add><![CDATA[
           if (isset($this->request->post['holiday'])) {
			$this->data['holiday'] = $this->request->post['holiday'];
		} elseif (!empty($product_info)) {
			$this->data['holiday'] = $product_info['holiday'];
		} else {
			$this->data['holiday'] = 0;
		}
           ]]></add>
       </operation>
</file>

Code: Select all

<file name="admin/model/catalog/product.php">
    	<operation>
           <search position="replace"><![CDATA[
           $this->db->query("INSERT INTO " . DB_PREFIX . "product SET model = '" . $this->db->escape($data['model']) . "', sku = '" . $this->db->escape($data['sku']) . "', upc = '" . $this->db->escape($data['upc']) . "', location = '" . $this->db->escape($data['location']) . "', quantity = '" . (int)$data['quantity'] . "', minimum = '" . (int)$data['minimum'] . "', subtract = '" . (int)$data['subtract'] . "', stock_status_id = '" . (int)$data['stock_status_id'] . "', date_available = '" . $this->db->escape($data['date_available']) . "', manufacturer_id = '" . (int)$data['manufacturer_id'] . "', shipping = '" . (int)$data['shipping'] . "', price = '" . (float)$data['price'] . "', points = '" . (int)$data['points'] . "', weight = '" . (float)$data['weight'] . "', weight_class_id = '" . (int)$data['weight_class_id'] . "', length = '" . (float)$data['length'] . "', width = '" . (float)$data['width'] . "', height = '" . (float)$data['height'] . "', length_class_id = '" . (int)$data['length_class_id'] . "', status = '" . (int)$data['status'] . "', tax_class_id = '" . $this->db->escape($data['tax_class_id']) . "', sort_order = '" . (int)$data['sort_order'] . "', date_added = NOW()");
           ]]></search>
           <add><![CDATA[
           $this->db->query("INSERT INTO " . DB_PREFIX . "product SET model = '" . $this->db->escape($data['model']) . "', sku = '" . $this->db->escape($data['sku']) . "', upc = '" . $this->db->escape($data['upc']) . "', location = '" . $this->db->escape($data['location']) . "', quantity = '" . (int)$data['quantity'] . "', minimum = '" . (int)$data['minimum'] . "', subtract = '" . (int)$data['subtract'] . "', stock_status_id = '" . (int)$data['stock_status_id'] . "', date_available = '" . $this->db->escape($data['date_available']) . "', manufacturer_id = '" . (int)$data['manufacturer_id'] . "', shipping = '" . (int)$data['shipping'] . "', price = '" . (float)$data['price'] . "', points = '" . (int)$data['points'] . "', weight = '" . (float)$data['weight'] . "', weight_class_id = '" . (int)$data['weight_class_id'] . "', length = '" . (float)$data['length'] . "', width = '" . (float)$data['width'] . "', height = '" . (float)$data['height'] . "', length_class_id = '" . (int)$data['length_class_id'] . "', status = '" . (int)$data['status'] . "', tax_class_id = '" . $this->db->escape($data['tax_class_id']) . "', sort_order = '" . (int)$data['sort_order'] . "', date_added = NOW(), `holiday` = '" . (isset($data['holiday']) ? (int)$data['holiday'] : 0) . "'");
           ]]></add>
       </operation>
    	<operation>
           <search position="replace"><![CDATA[
           $this->db->query("UPDATE " . DB_PREFIX . "product SET model = '" . $this->db->escape($data['model']) . "', sku = '" . $this->db->escape($data['sku']) . "', upc = '" . $this->db->escape($data['upc']) . "', location = '" . $this->db->escape($data['location']) . "', quantity = '" . (int)$data['quantity'] . "', minimum = '" . (int)$data['minimum'] . "', subtract = '" . (int)$data['subtract'] . "', stock_status_id = '" . (int)$data['stock_status_id'] . "', date_available = '" . $this->db->escape($data['date_available']) . "', manufacturer_id = '" . (int)$data['manufacturer_id'] . "', shipping = '" . (int)$data['shipping'] . "', price = '" . (float)$data['price'] . "', points = '" . (int)$data['points'] . "', weight = '" . (float)$data['weight'] . "', weight_class_id = '" . (int)$data['weight_class_id'] . "', length = '" . (float)$data['length'] . "', width = '" . (float)$data['width'] . "', height = '" . (float)$data['height'] . "', length_class_id = '" . (int)$data['length_class_id'] . "', status = '" . (int)$data['status'] . "', tax_class_id = '" . $this->db->escape($data['tax_class_id']) . "', sort_order = '" . (int)$data['sort_order'] . "', date_modified = NOW() WHERE product_id = '" . (int)$product_id . "'");
           ]]></search>
           <add><![CDATA[
           $this->db->query("UPDATE " . DB_PREFIX . "product SET model = '" . $this->db->escape($data['model']) . "', sku = '" . $this->db->escape($data['sku']) . "', upc = '" . $this->db->escape($data['upc']) . "', location = '" . $this->db->escape($data['location']) . "', quantity = '" . (int)$data['quantity'] . "', minimum = '" . (int)$data['minimum'] . "', subtract = '" . (int)$data['subtract'] . "', stock_status_id = '" . (int)$data['stock_status_id'] . "', date_available = '" . $this->db->escape($data['date_available']) . "', manufacturer_id = '" . (int)$data['manufacturer_id'] . "', shipping = '" . (int)$data['shipping'] . "', price = '" . (float)$data['price'] . "', points = '" . (int)$data['points'] . "', weight = '" . (float)$data['weight'] . "', weight_class_id = '" . (int)$data['weight_class_id'] . "', length = '" . (float)$data['length'] . "', width = '" . (float)$data['width'] . "', height = '" . (float)$data['height'] . "', length_class_id = '" . (int)$data['length_class_id'] . "', status = '" . (int)$data['status'] . "', tax_class_id = '" . $this->db->escape($data['tax_class_id']) . "', sort_order = '" . (int)$data['sort_order'] . "', date_modified = NOW(), `holiday` = '" . (isset($data['holiday']) ? (int)$data['holiday'] : 0) . "' WHERE product_id = '" . (int)$product_id . "'");
           ]]></add>
       </operation>
</file>

Code: Select all

<file name="admin/language/english/catalog/product.php">
       <operation>
           <search position="before"><![CDATA[
           ?>
           ]]></search>
           <add><![CDATA[
           $_['entry_holiday'] = 'Available during holiday?';
           ]]></add>
       </operation>
</file>

Code: Select all

<file name="admin/view/template/catalog/product_form.tpl">
       <operation>
           <search position="before"><![CDATA[
           <td><?php echo $entry_status; ?></td>
           ]]></search>
           <add><![CDATA[
	      <td><?php echo $entry_holiday; ?></td>
	      <td><?php if ($holiday) { ?>
               <input type="checkbox" name="holiday" value="1" checked="checked" />
               <?php } else { ?>
               <input type="checkbox" name="holiday" value="1" />
               <?php } ?>
             </td>
	    </tr>
	    <tr>
           ]]></add>
       </operation>
</file>

Advanced Professional Email Template
Customers Pre-Sale. Inc abandoned cart email
Order Follow-Up Email. Inc request review
Email Validation with ZeroBounce


User avatar
Active Member

Posts

Joined
Mon May 16, 2011 7:24 pm
Location - UK

Post by Qphoria » Wed Oct 24, 2012 9:52 pm

I don't think there is any issue with the way it currently works. The vqmod replace option allows inline partial replacements, tho I see many people don't use it correctly

Instead of this:

Code: Select all

<search position="replace">
$this->db->query("INSERT INTO order SET order_id = 'x', customer_id = 'y'");
</search>

<add>
$this->db->query("INSERT INTO order SET order_id = 'x', customer_id = 'y', new_field = 'z'");
</add>
Be sure your scripts only target one unique spot in the string and replace that only:

Code: Select all

<search position="replace">
customer_id = 'y'
</search>

<add trim="true">
customer_id = 'y', new_field = 'z'
</add>
This way you alter as little as possible and you don't break the rest of the string for other vqmods. Be sure you use the trim="true" option here so that it goes inline. Now if 2 vqmod scripts were triggering on "customer_id = 'y'" then they will both work now

Image


User avatar
Administrator

Posts

Joined
Tue Jul 22, 2008 3:02 am

Post by opencart-templates » Wed Oct 24, 2012 11:53 pm

Wow I didn't realise vqmod could do that, thank you. Yes you are correct that completely solves what I thought was an issue.

When I had tried to replace just a section of a string before without the trim=true, that whys I didn't think it was possible.

Advanced Professional Email Template
Customers Pre-Sale. Inc abandoned cart email
Order Follow-Up Email. Inc request review
Email Validation with ZeroBounce


User avatar
Active Member

Posts

Joined
Mon May 16, 2011 7:24 pm
Location - UK
Who is online

Users browsing this forum: No registered users and 2 guests