<modification>
	<id>Newsletter Enhancements Mod</id>
	<version>1.0</version>
	<vqmver>1.2.3</vqmver>
	<author>DmitryNek</author>
	
	<file name="admin/view/template/sale/contact.tpl">
		<operation>
			<search position="replace"><![CDATA[<td><span class="required">*</span> <?php echo $entry_subject; ?></td>]]></search>
			<add><![CDATA[
				<?php if ($newsletter_installed) { ?>
					<td><?php echo $entry_defined; ?></td>
					<td>
						<?php if($newsletter_defined) {
						$checked1 = ' checked="checked"';
						$checked0 = '';
						} else {
						$checked1 = '';
						$checked0 = ' checked="checked"';
						} ?>
					<label for="newsletter_defined_1"><?php echo $entry_yes; ?></label>
					<input type="radio"<?php echo $checked1; ?> id="newsletter_defined_1" name="newsletter_defined" value="1" />
					<label for="newsletter_defined_0"><?php echo $entry_no; ?></label>
					<input type="radio"<?php echo $checked0; ?> id="newsletter_defined_0" name="newsletter_defined" value="0" />
					</td>
				</tr>
				<tbody id="newsletter-defined-product">
					<tr>
					  <td><?php echo $entry_newsletter_product; ?></td>
					  <td><table>
						  <tr>
						    <td style="padding: 0;" colspan="3"><select id="category-defined" style="margin-bottom: 5px;" onchange="getProductsDefined();">
						      <?php foreach ($categories as $category) { ?>
						      <option value="<?php echo $category['category_id']; ?>"><?php echo $category['name']; ?></option>
						      <?php } ?>
						      </select></td>
						  </tr>
						  <tr>
						    <td style="padding: 0;"><select multiple="multiple" id="product-defined" size="10" style="width: 350px;">
						      </select></td>
						    <td style="vertical-align: middle;"><input type="button" value="--&gt;" onclick="addItemDefined();" />
						      <br />
						      <input type="button" value="&lt;--" onclick="removeItemDefined();" /></td>
						    <td style="padding: 0;"><select multiple="multiple" id="item-defined" size="10" style="width: 350px;">
						      </select></td>
						  </tr>
					    </table>
					    <div id="product_item-defined">
					    </div></td>
					</tr>
				</tbody>
				<tr>
					<td><?php echo $entry_special; ?></td>
					<td>
						<?php if($newsletter_special) {
						$checked1 = ' checked="checked"';
						$checked0 = '';
						} else {
						$checked1 = '';
						$checked0 = ' checked="checked"';
						} ?>
					<label for="newsletter_special_1"><?php echo $entry_yes; ?></label>
					<input type="radio"<?php echo $checked1; ?> id="newsletter_special_1" name="newsletter_special" value="1" />
					<label for="newsletter_special_0"><?php echo $entry_no; ?></label>
					<input type="radio"<?php echo $checked0; ?> id="newsletter_special_0" name="newsletter_special" value="0" />
					</td>
				</tr>
				<tr>
					<td><?php echo $entry_latest; ?></td>
					<td>
						<?php if($newsletter_latest) {
						$checked1 = ' checked="checked"';
						$checked0 = '';
						} else {
						$checked1 = '';
						$checked0 = ' checked="checked"';
						} ?>
					<label for="newsletter_latest_1"><?php echo $entry_yes; ?></label>
					<input type="radio"<?php echo $checked1; ?> id="newsletter_latest_1" name="newsletter_latest" value="1" />
					<label for="newsletter_latest_0"><?php echo $entry_no; ?></label>
					<input type="radio"<?php echo $checked0; ?> id="newsletter_latest_0" name="newsletter_latest" value="0" />
					</td>
				</tr>
				<tr>
					<td><?php echo $entry_popular; ?></td>
					<td>
						<?php if($newsletter_popular) {
						$checked1 = ' checked="checked"';
						$checked0 = '';
						} else {
						$checked1 = '';
						$checked0 = ' checked="checked"';
						} ?>
					<label for="newsletter_popular_1"><?php echo $entry_yes; ?></label>
					<input type="radio"<?php echo $checked1; ?> id="newsletter_popular_1" name="newsletter_popular" value="1" />
					<label for="newsletter_popular_0"><?php echo $entry_no; ?></label>
					<input type="radio"<?php echo $checked0; ?> id="newsletter_popular_0" name="newsletter_popular" value="0" />
					</td>
				</tr>
				<tr>
					<td><?php echo $entry_attachments; ?></td>
					<td>
						<table id="newsletter_attachment">
							<tbody id="newsletter_attachment_0">
								<tr>
									<td class="left"><input type="hidden" name="newsletter_attachments_count" value="1" /><input type="file" name="newsletter_attachment_0" value="" /></td>
									<td></td>
								</tr>
							</tbody>
							<tfoot>
								<tr>
									<td class="left"><a onclick="addFile();" class="button"><span><?php echo $button_add_file; ?></span></a></td>
									<td></td>
								</tr>
							</tfoot>
						</table>
					</td>
				</tr>
				<tr>
				<?php } ?>
				<td><span class="required">*</span> <?php echo $entry_subject; ?></td>
				]]></add>
		</operation>
		<operation>
			<search position="replace"><![CDATA[<input type="text" name="subject" value="<?php echo $subject; ?>" />]]></search>
			<add><![CDATA[<input type="text" name="subject" value="<?php echo $subject; ?>" size="60" />]]></add>
		</operation>
		<operation>
			<search position="after"><![CDATA[CKEDITOR.replace('message', {]]></search>
			<add><![CDATA[
				<?php if ($newsletter_installed) { ?>height:'500',<?php } ?>
				]]></add>
		</operation>
		<operation>
			<search position="after"><![CDATA[<script type="text/javascript" src="view/javascript/ckeditor/ckeditor.js"></script>]]></search>
			<add><![CDATA[
				<?php if ($newsletter_installed) { ?>
				<script type="text/javascript"><!--	
				$('input[name=newsletter_defined]').bind('click', function() {
					checkDefined();
				});

				$('input[name=newsletter_special], input[name=newsletter_latest], input[name=newsletter_popular]').bind('click', function() {
					getTemplate();
				});

				function checkDefined() {
					if ($('input[name=newsletter_defined]:checked').val() == 1)
					{
						$('#newsletter-defined-product').show();
					}
					else
					{
						$('#newsletter-defined-product').hide();
					}
				}

				checkDefined();

				function getTemplate() {

					var defined_products = '[';
					if ($('input[name=newsletter_defined]:checked').val() == 1)
					{
						$('#product_item-defined input').each(function(id) { 
							var item = $('#product_item-defined input').get(id); 
							defined_products += item.value + ',';
						});
					}
					if (defined_products.length > 1) {
						defined_products = defined_products.substring(0, defined_products.length-1);
					}
					defined_products += ']';

					$.ajax({
						type: 'post',
						url: '<?php echo HTTP_CATALOG ?>index.php?route=module/newsletter',
						data: {special: $('input[name=newsletter_special]:checked').val(), latest: $('input[name=newsletter_latest]:checked').val(), popular: $('input[name=newsletter_popular]:checked').val(), defined: defined_products},
						dataType: 'html',
						success: function(html) {
							for (instance in CKEDITOR.instances) {
								CKEDITOR.instances[instance].setData(html);
							}
						}
					});
				}

				if (!$('textarea[name=message]').val()) {
					getTemplate();
				}

				var newsletter_attachment_row = 1;

				function addFile() {
					html  = '<tbody id="newsletter_attachment_' + newsletter_attachment_row + '">';
					html += '<tr>';	
					html += '<td class="left"><input type="file" name="newsletter_attachment_' + newsletter_attachment_row + '" value="" /></td>';
					html += '<td class="left"><img class="newsletter_file_remove" alt="newsletter_attachment_' + newsletter_attachment_row + '" src="view/image/delete.png" /></td>';
					html += '</tr>';	
				    html += '</tbody>';

					$('#newsletter_attachment tfoot').before(html);

					newsletter_attachment_row++;

					$('input[name=newsletter_attachments_count]').val(newsletter_attachment_row);
				}

				$('.newsletter_file_remove').live('click', function(){
					removeFile($(this).attr("alt"));
				});

				function removeFile(row) {
					$('#' + row).remove();

					newsletter_attachment_row--;

					$('input[name=newsletter_attachments_count]').val(newsletter_attachment_row);
				}
				
				function addItemDefined() {
					$('#product-defined :selected').each(function() {
						$(this).remove();

						$('#item-defined option[value=\'' + $(this).attr('value') + '\']').remove();

						$('#item-defined').append('<option value="' + $(this).attr('value') + '">' + $(this).text() + '</option>');

						$('#product_item-defined input[value=\'' + $(this).attr('value') + '\']').remove();

						$('#product_item-defined').append('<input type="hidden" name="newsletter_product[]" value="' + $(this).attr('value') + '" />');
					});

					getTemplate();
				}

				function removeItemDefined() {
					$('#item-defined :selected').each(function() {
						$(this).remove();

						$('#product_item-defined input[value=\'' + $(this).attr('value') + '\']').remove();
					});

					getTemplate();
				}

				function getProductsDefined() {
					$('#product-defined option').remove();

					$.ajax({
						url: 'index.php?route=sale/contact/category&token=<?php echo $token; ?>&category_id=' + $('#category-defined').attr('value'),
						dataType: 'json',
						success: function(data) {
							for (i = 0; i < data.length; i++) {
					 			$('#product-defined').append('<option value="' + data[i]['product_id'] + '">' + data[i]['name'] + ' (' + data[i]['model'] + ') </option>');
							}
						}
					});
				}
				
				getProductsDefined();
				//--></script>
				<?php } ?>
				]]></add>
		</operation>
	</file>
	
	<file name="admin/controller/sale/contact.php">
		<operation>
			<search position="before"><![CDATA[$this->load->language('sale/contact');]]></search>
			<add><![CDATA[
				if ($this->config->get('newsletter_status')) {
					$this->load->language('module/newsletter');

					$this->data['entry_yes'] = $this->language->get('entry_yes');
					$this->data['entry_no'] = $this->language->get('entry_no');
					$this->data['entry_defined'] = $this->language->get('entry_defined');
					$this->data['entry_latest'] = $this->language->get('entry_latest');
					$this->data['entry_popular'] = $this->language->get('entry_popular');
					$this->data['entry_special'] = $this->language->get('entry_special');
					$this->data['entry_newsletter_product'] = $this->language->get('entry_newsletter_product');
					$this->data['entry_attachments'] = $this->language->get('entry_attachments');

					$this->data['button_add_file'] = $this->language->get('button_add_file');
					$this->data['button_remove'] = $this->language->get('button_remove');

					$this->load->model('catalog/product');

					$this->data['newsletter_products'] = array();

					if (isset($this->request->post['newsletter_product'])) {
						foreach ($this->request->post['newsletter_product'] as $product_id) {
							$product_info = $this->model_catalog_product->getProduct($product_id);

							if ($product_info) {
								$this->data['newsletter_products'][] = array(
									'product_id' => $product_info['product_id'],
									'name'       => $product_info['name']
								);
							}
						}
						unset($product_info);
						unset($product_id);
					}

					if (isset($this->request->post['newsletter_defined'])) {
						$this->data['newsletter_defined'] = $this->request->post['newsletter_defined'];
					} else {
						$this->data['newsletter_defined'] = false;
					}

					if (isset($this->request->post['newsletter_special'])) {
						$this->data['newsletter_special'] = $this->request->post['newsletter_special'];
					} else {
						$this->data['newsletter_special'] = false;
					}

					if (isset($this->request->post['newsletter_latest'])) {
						$this->data['newsletter_latest'] = $this->request->post['newsletter_latest'];
					} else {
						$this->data['newsletter_latest'] = false;
					}

					if (isset($this->request->post['newsletter_popular'])) {
						$this->data['newsletter_popular'] = $this->request->post['newsletter_popular'];
					} else {
						$this->data['newsletter_popular'] = false;
					}

					$newsletter_attachments = array();

					if (isset($this->request->post['newsletter_attachments_count']) && $this->request->post['newsletter_attachments_count']) {
						for ($i=0; $i<$this->request->post['newsletter_attachments_count']; $i++) {
							if (is_uploaded_file($this->request->files['newsletter_attachment_'.$i]['tmp_name'])) {
								$filename = $this->request->files['newsletter_attachment_'.$i]['name'];

								move_uploaded_file($this->request->files['newsletter_attachment_'.$i]['tmp_name'], DIR_DOWNLOAD . $filename);

								if (file_exists(DIR_DOWNLOAD . $filename)) {
									$newsletter_attachments[] = array(
										'filename' => $this->request->files['newsletter_attachment_'.$i]['name'],
										'path'     => DIR_DOWNLOAD . $filename
									);
								}
							}
						}
					}

					$this->data['newsletter_installed'] = true;
				} else {
					$this->data['newsletter_installed'] = false;
				}
				]]></add>
		</operation>
		<operation>
			<search position="replace"><![CDATA[$message .= '<body>' . html_entity_decode($this->request->post['message'], ENT_QUOTES, 'UTF-8') . '</body>' . "\n";]]></search>
			<add><![CDATA[
				if ($this->config->get('newsletter_status')) {
					
					$this->load->model('module/newsletter');
					
					$newsletter_id = time();
					
					$this->model_module_newsletter->addNewsletter(array(
						'subject' => $this->request->post['subject'],
						'message' => $this->request->post['message'],
						'date' => date('Y-m-d H:i:s'),
						'time' => $newsletter_id,
						'recipients_count' => count($emails)
					));
					
					$message .= '<body style="padding:0;margin:0;">' . html_entity_decode($this->request->post['message'], ENT_QUOTES, 'UTF-8') . '</body>' . "\n";
					$message = str_replace(array(HTTP_CATALOG . 'index.php?route=module/newsletter/track&n=', HTTP_CATALOG . 'index.php?route=module/newsletter/track&amp;n=', HTTP_CATALOG . 'index.php?route=module/newsletter/track&amp;amp;n='), HTTP_CATALOG . 'index.php?route=module/newsletter/track&n=' . $newsletter_id, $message);
					
					$message = str_replace(array(HTTP_CATALOG . 'index.php?route=module/newsletter&show=', HTTP_CATALOG . 'index.php?route=module/newsletter&amp;show=', HTTP_CATALOG . 'index.php?route=module/newsletter&amp;amp;show='), HTTP_CATALOG . 'index.php?route=module/newsletter&show=' . $newsletter_id, $message);
					
				} else {
					$message .= '<body>' . html_entity_decode($this->request->post['message'], ENT_QUOTES, 'UTF-8') . '</body>' . "\n";
				}
				]]></add>
		</operation>
		<operation>
			<search position="replace" offset="12"><![CDATA[if (preg_match_all('#(src="([^"]*)")#mis', $message, $matches)) {]]></search>
			<add><![CDATA[]]></add>
		</operation>
		<operation>
			<search position="before"><![CDATA[$mail->setHtml($message);]]></search>
			<add><![CDATA[
				if ($this->config->get('newsletter_status')) {
					foreach ($newsletter_attachments as $newsletter_attachment) {
						$mail->addAttachment($newsletter_attachment['path'], $newsletter_attachment['filename']);
					}
				}
				]]></add>
		</operation>
		<operation>
			<search position="before"><![CDATA[foreach ($emails as $email) {]]></search>
			<add><![CDATA[
				if ($this->config->get('newsletter_use_throttle')) {
					$i = 0;
				}
				]]></add>
		</operation>
		<operation>
			<search position="replace"><![CDATA[$mail->send();]]></search>
			<add><![CDATA[
				if ($this->config->get('newsletter_use_throttle')) {
					if ($i > (int)$this->config->get('newsletter_throttle_emails')) {
						$i = 0;
						sleep((int)$this->config->get('newsletter_throttle_interval'));
					}
					$i++;
				}
				$mail->send();
				]]></add>
		</operation>
		<operation>
			<search position="before"><![CDATA[$this->session->data['success'] = $this->language->get('text_success');]]></search>
			<add><![CDATA[
					if ($this->config->get('newsletter_status')) {
						foreach ($newsletter_attachments as $newsletter_attachment) {
							if ($newsletter_attachment['path']) {
								unlink($newsletter_attachment['path']);
							}
						}
					}
				]]></add>
		</operation>
	</file>
	
	<file name="admin/view/template/common/header.tpl">
		<operation>
			<search position="after"><![CDATA[<li><a href="<?php echo $contact; ?>"><?php echo $text_contact; ?></a></li>]]></search>
			<add><![CDATA[
				<?php if($contact_stats) { ?>
					<li><a href="<?php echo $contact_stats; ?>"><?php echo $text_contact_stats; ?></a></li>
				<?php } ?>
				]]></add>
		</operation>
	</file>
	
	<file name="admin/controller/common/header.php">
		<operation>
			<search position="after"><![CDATA[$this->data['contact'] = HTTPS_SERVER . 'index.php?route=sale/contact&token=' . $this->session->data['token'];]]></search>
			<add><![CDATA[
				if ($this->config->get('newsletter_status')) {
					$this->load->language('module/newsletter');
					$this->data['text_contact_stats'] = $this->language->get('text_contact_stats');
					$this->data['contact_stats'] = HTTPS_SERVER . 'index.php?route=module/newsletter/stats&token=' . $this->session->data['token'];
				} else {
					$this->data['contact_stats'] = false;
				}
				]]></add>
		</operation>
	</file>
	
</modification>