<?php 
class ControllerNewsArticle extends Controller {
	private $error = array(); 
     
  	public function index() {
		$this->load->language('news/article');
    	
		$this->document->setTitle($this->language->get('heading_title')); 
		
		$this->load->model('news/article');
		
		$this->getList();
  	}
  
  	public function insert() {
    	$this->load->language('news/article');

    	$this->document->setTitle($this->language->get('heading_title')); 
		
		$this->load->model('news/article');
		
    	if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validateForm()) {
			$this->model_news_article->addArticle($this->request->post);
	  		
			$this->session->data['success'] = $this->language->get('text_success');
	  
			$url = '';
			
			if (isset($this->request->get['filter_name'])) {
				$url .= '&filter_name=' . urlencode(html_entity_decode($this->request->get['filter_name'], ENT_QUOTES, 'UTF-8'));
			}	
			
			if (isset($this->request->get['filter_status'])) {
				$url .= '&filter_status=' . $this->request->get['filter_status'];
			}
			/*category*/
			if (isset($this->request->get['filter_article_category'])) {
			$url .= '&filter_article_category=' . $this->request->get['filter_article_category'];}
			/*author*/
			if (isset($this->request->get['filter_author'])) {
			$url .= '&filter_author=' . $this->request->get['filter_author'];}
			/*filter_date*/ 
			if (isset($this->request->get['filter_date'])) {
			$url .= '&filter_date=' . $this->request->get['filter_date'];}
			
					
			if (isset($this->request->get['sort'])) {
				$url .= '&sort=' . $this->request->get['sort'];
			}

			if (isset($this->request->get['order'])) {
				$url .= '&order=' . $this->request->get['order'];
			}

			if (isset($this->request->get['page'])) {
				$url .= '&page=' . $this->request->get['page'];
			}
			
			$this->redirect($this->url->link('news/article', 'token=' . $this->session->data['token'] . $url, 'SSL'));
    	}
	
    	$this->getForm();
  	}

  	public function update() {
    	$this->load->language('news/article');

    	$this->document->setTitle($this->language->get('heading_title'));
		
		$this->load->model('news/article');
	
    	if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validateForm()) {
			$this->model_news_article->editArticle($this->request->get['article_id'], $this->request->post);
			
			$this->session->data['success'] = $this->language->get('text_success');
			
			$url = '';
			
			if (isset($this->request->get['filter_name'])) {
				$url .= '&filter_name=' . urlencode(html_entity_decode($this->request->get['filter_name'], ENT_QUOTES, 'UTF-8'));
			}
		
					
			if (isset($this->request->get['filter_status'])) {
				$url .= '&filter_status=' . $this->request->get['filter_status'];
			}
			
			/*category*/
			if (isset($this->request->get['filter_article_category'])) {
			$url .= '&filter_article_category=' . $this->request->get['filter_article_category'];}
			/*author*/
			if (isset($this->request->get['filter_author'])) {
			$url .= '&filter_author=' . $this->request->get['filter_author'];}
			/*filter_date*/ 
			if (isset($this->request->get['filter_date'])) {
			$url .= '&filter_date=' . $this->request->get['filter_date'];}
					
			if (isset($this->request->get['sort'])) {
				$url .= '&sort=' . $this->request->get['sort'];
			}

			if (isset($this->request->get['order'])) {
				$url .= '&order=' . $this->request->get['order'];
			}

			if (isset($this->request->get['page'])) {
				$url .= '&page=' . $this->request->get['page'];
			}
			
			$this->redirect($this->url->link('news/article', 'token=' . $this->session->data['token'] . $url, 'SSL'));
		}

    	$this->getForm();
  	}

  	public function delete() {
    	$this->load->language('news/article');

    	$this->document->setTitle($this->language->get('heading_title'));
		
		$this->load->model('news/article');
		
		if (isset($this->request->post['selected']) && $this->validateDelete()) {
			foreach ($this->request->post['selected'] as $article_id) {
				$this->model_news_article->deleteArticle($article_id);
	  		}

			$this->session->data['success'] = $this->language->get('text_success');
			
			$url = '';
			
			if (isset($this->request->get['filter_name'])) {
				$url .= '&filter_name=' . urlencode(html_entity_decode($this->request->get['filter_name'], ENT_QUOTES, 'UTF-8'));
			}
			
		
			if (isset($this->request->get['filter_status'])) {
				$url .= '&filter_status=' . $this->request->get['filter_status'];
			}
			/*category*/
			if (isset($this->request->get['filter_article_category'])) {
			$url .= '&filter_article_category=' . $this->request->get['filter_article_category'];}
			/*author*/
			if (isset($this->request->get['filter_author'])) {
			$url .= '&filter_author=' . $this->request->get['filter_author'];}
			/*filter_date*/ 
			if (isset($this->request->get['filter_date'])) {
			$url .= '&filter_date=' . $this->request->get['filter_date'];}
					
			if (isset($this->request->get['sort'])) {
				$url .= '&sort=' . $this->request->get['sort'];
			}

			if (isset($this->request->get['order'])) {
				$url .= '&order=' . $this->request->get['order'];
			}

			if (isset($this->request->get['page'])) {
				$url .= '&page=' . $this->request->get['page'];
			}
			
			$this->redirect($this->url->link('news/article', 'token=' . $this->session->data['token'] . $url, 'SSL'));
		}

    	$this->getList();
  	}

  	public function copy() {
    	$this->load->language('news/article');

    	$this->document->setTitle($this->language->get('heading_title'));
		
		$this->load->model('news/article');
		
		if (isset($this->request->post['selected']) && $this->validateCopy()) {
			foreach ($this->request->post['selected'] as $article_id) {
				$this->model_news_article->copyArticle($article_id);
	  		}

			$this->session->data['success'] = $this->language->get('text_success');
			
			$url = '';
			
			if (isset($this->request->get['filter_name'])) {
				$url .= '&filter_name=' . urlencode(html_entity_decode($this->request->get['filter_name'], ENT_QUOTES, 'UTF-8'));
			}
			/*category*/
			if (isset($this->request->get['filter_article_category'])) {
			$url .= '&filter_article_category=' . $this->request->get['filter_article_category'];}
			/*author*/
			if (isset($this->request->get['filter_author'])) {
			$url .= '&filter_author=' . $this->request->get['filter_author'];}
			/*filter_date*/ 
			if (isset($this->request->get['filter_date'])) {
			$url .= '&filter_date=' . $this->request->get['filter_date'];}
			
			if (isset($this->request->get['filter_status'])) {
				$url .= '&filter_status=' . $this->request->get['filter_status'];
			}
					
			if (isset($this->request->get['sort'])) {
				$url .= '&sort=' . $this->request->get['sort'];
			}

			if (isset($this->request->get['order'])) {
				$url .= '&order=' . $this->request->get['order'];
			}

			if (isset($this->request->get['page'])) {
				$url .= '&page=' . $this->request->get['page'];
			}
			
			$this->redirect($this->url->link('news/article', 'token=' . $this->session->data['token'] . $url, 'SSL'));
		}

    	$this->getList();
  	}
	
  	private function getList() {	
		$this->load->model('news/category');
		$this->data['categories'] = $this->model_news_category->getCategories(0);	
			
		$this->load->model('news/author');
		$this->data['authors'] = $this->model_news_author->getAuthors(0);
					
		if (isset($this->request->get['filter_name'])) {
			$filter_name = $this->request->get['filter_name'];
		} else {
			$filter_name = null;
		}
		//filter category start//
		if (isset($this->request->get['filter_article_category'])) {
			$filter_article_category = $this->request->get['filter_article_category'];
		} else {$filter_article_category = NULL;}
		//filter_author
		if (isset($this->request->get['filter_author'])) {
			$filter_author = $this->request->get['filter_author'];
		} else {$filter_author = NULL;}
		//filter_date
		if (isset($this->request->get['filter_date'])) {
			$filter_date = $this->request->get['filter_date'];
		} else {$filter_date = NULL;}
		
		if (isset($this->request->get['filter_status'])) {
			$filter_status = $this->request->get['filter_status'];
		} else {
			$filter_status = null;
		}

		if (isset($this->request->get['sort'])) {
			$sort = $this->request->get['sort'];
		} else {
			$sort = 'pd.name';
		}
		
		if (isset($this->request->get['order'])) {
			$order = $this->request->get['order'];
		} else {
			$order = 'ASC';
		}
		
		if (isset($this->request->get['page'])) {
			$page = $this->request->get['page'];
		} else {
			$page = 1;
		}
						
		$url = '';
						
		if (isset($this->request->get['filter_name'])) {
			$url .= '&filter_name=' . urlencode(html_entity_decode($this->request->get['filter_name'], ENT_QUOTES, 'UTF-8'));
		}		
		
			/*category*/
			if (isset($this->request->get['filter_article_category'])) {
			$url .= '&filter_article_category=' . $this->request->get['filter_article_category'];}
			/*author*/
			if (isset($this->request->get['filter_author'])) {
			$url .= '&filter_author=' . $this->request->get['filter_author'];}
			/*filter_date*/ 
			if (isset($this->request->get['filter_date'])) {
			$url .= '&filter_date=' . $this->request->get['filter_date'];}
			
		if (isset($this->request->get['filter_status'])) {
			$url .= '&filter_status=' . $this->request->get['filter_status'];
		}
						
		if (isset($this->request->get['sort'])) {
			$url .= '&sort=' . $this->request->get['sort'];
		}

		if (isset($this->request->get['order'])) {
			$url .= '&order=' . $this->request->get['order'];
		}
		
		if (isset($this->request->get['page'])) {
			$url .= '&page=' . $this->request->get['page'];
		}

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

   		$this->data['breadcrumbs'][] = array(
       		'text'      => $this->language->get('text_home'),
			'href'      => $this->url->link('common/home', 'token=' . $this->session->data['token'], 'SSL'),
      		'separator' => false
   		);

   		$this->data['breadcrumbs'][] = array(
       		'text'      => $this->language->get('heading_title'),
			'href'      => $this->url->link('news/article', 'token=' . $this->session->data['token'] . $url, 'SSL'),       		
      		'separator' => ' :: '
   		);
		
		$this->data['insert'] = $this->url->link('news/article/insert', 'token=' . $this->session->data['token'] . $url, 'SSL');
		$this->data['copy'] = $this->url->link('news/article/copy', 'token=' . $this->session->data['token'] . $url, 'SSL');	
		$this->data['delete'] = $this->url->link('news/article/delete', 'token=' . $this->session->data['token'] . $url, 'SSL');
    	
		$this->data['articles'] = array();

		$data = array(
			'filter_name'	  => $filter_name, 
			'filter_article_category'=> $filter_article_category,
			'filter_author'=> $filter_author,
			'filter_date'	  => $filter_date,
			'filter_status'   => $filter_status,
			'sort'            => $sort,
			'order'           => $order,
			'start'           => ($page - 1) * $this->config->get('config_admin_limit'),
			'limit'           => $this->config->get('config_admin_limit')
		);
		
		$this->load->model('tool/image');
		
		$article_total = $this->model_news_article->getTotalArticles($data);
			
		$results = $this->model_news_article->getArticles($data);
				    	
		foreach ($results as $result) {
			$action = array();
			$action[] = array(
				'text' => $this->language->get('text_edit'),
				'target' => $this->language->get('_self'),
				'href' => $this->url->link('news/article/update', 'token=' . $this->session->data['token'] . '&article_id=' . $result['article_id'] . $url, 'SSL')
			);
			$action[] = array(
				'text' => $this->language->get('text_view'),
				'target' => $this->language->get('_blank'),
				'href' => HTTP_CATALOG.'index.php?route=news/article&article_id=' . $result['article_id']
			);
			if ($result['image'] && file_exists(DIR_IMAGE . $result['image'])) {
				$image = $this->model_tool_image->resize($result['image'], 40, 40);
			} else {
				$image = $this->model_tool_image->resize('no_image.jpg', 40, 40);
			}
		
		//filter category start//			
      $category =  $this->model_news_article->getArticleCategories($result['article_id']);
	  //filter category end//
	  
      		$this->data['articles'][] = array(
				'article_id' => $result['article_id'],
				'name'       => $result['name'],
				'category'   => $category,
				'author_id'=>$result['author_id'],
				'date'      => date('Y-d-m',strtotime($result['date_added'])),
				'image'      => $image,
				'status'     => ($result['status'] ? $this->language->get('text_enabled') : $this->language->get('text_disabled')),
				'sort_order'=>$result['sort_order'],
				'selected'   => isset($this->request->post['selected']) && in_array($result['article_id'], $this->request->post['selected']),
				'action'     => $action
			);
    	}
		
		$this->data['heading_title'] = $this->language->get('heading_title');		
				
		$this->data['text_enabled'] = $this->language->get('text_enabled');		
		$this->data['text_disabled'] = $this->language->get('text_disabled');		
		$this->data['text_no_results'] = $this->language->get('text_no_results');		
		$this->data['text_image_manager'] = $this->language->get('text_image_manager');		
			
		$this->data['column_image'] = $this->language->get('column_image');		
		$this->data['column_name'] = $this->language->get('column_name');		
		$this->data['column_category'] = $this->language->get('column_category');	
		$this->data['column_author'] = $this->language->get('column_author');			
		$this->data['column_status'] = $this->language->get('column_status');			
		$this->data['column_sort'] = $this->language->get('column_sort');		
		$this->data['column_action'] = $this->language->get('column_action');		
				
		$this->data['button_copy'] = $this->language->get('button_copy');		
		$this->data['button_insert'] = $this->language->get('button_insert');		
		$this->data['button_delete'] = $this->language->get('button_delete');		
		$this->data['button_filter'] = $this->language->get('button_filter');
		 
 		$this->data['token'] = $this->session->data['token'];
		
 		if (isset($this->error['warning'])) {
			$this->data['error_warning'] = $this->error['warning'];
		} else {
			$this->data['error_warning'] = '';
		}

		if (isset($this->session->data['success'])) {
			$this->data['success'] = $this->session->data['success'];
		
			unset($this->session->data['success']);
		} else {
			$this->data['success'] = '';
		}

		$url = '';
			/*category*/
			if (isset($this->request->get['filter_article_category'])) {
			$url .= '&filter_article_category=' . $this->request->get['filter_article_category'];}
			/*author*/
			if (isset($this->request->get['filter_author'])) {
			$url .= '&filter_author=' . $this->request->get['filter_author'];}
			/*filter_date*/ 
			if (isset($this->request->get['filter_date'])) {
			$url .= '&filter_date=' . $this->request->get['filter_date'];}

		if (isset($this->request->get['filter_name'])) {
			$url .= '&filter_name=' . urlencode(html_entity_decode($this->request->get['filter_name'], ENT_QUOTES, 'UTF-8'));
		}
		
		if (isset($this->request->get['filter_status'])) {
			$url .= '&filter_status=' . $this->request->get['filter_status'];
		}
								
		if ($order == 'ASC') {
			$url .= '&order=DESC';
		} else {
			$url .= '&order=ASC';
		}

		if (isset($this->request->get['page'])) {
			$url .= '&page=' . $this->request->get['page'];
		}
					
		$this->data['sort_name'] = $this->url->link('news/article', 'token=' . $this->session->data['token'] . '&sort=pd.name' . $url, 'SSL');
		$this->data['sort_author'] = $this->url->link('news/article', 'token=' . $this->session->data['token'] . '&sort=na.author' . $url, 'SSL');
		$this->data['sort_date'] = $this->url->link('news/article', 'token=' . $this->session->data['token'] . '&sort=p.date_added' . $url, 'SSL');
		$this->data['sort_status'] = $this->url->link('news/article', 'token=' . $this->session->data['token'] . '&sort=p.status' . $url, 'SSL');
		$this->data['sort_order'] = $this->url->link('news/article', 'token=' . $this->session->data['token'] . '&sort=p.sort_order' . $url, 'SSL');
		
		$url = '';

		if (isset($this->request->get['filter_name'])) {
			$url .= '&filter_name=' . urlencode(html_entity_decode($this->request->get['filter_name'], ENT_QUOTES, 'UTF-8'));
		}
		/*category*/
		if (isset($this->request->get['filter_article_category'])) {
		$url .= '&filter_article_category=' . $this->request->get['filter_article_category'];}
		/*author*/
		if (isset($this->request->get['filter_author'])) {
		$url .= '&filter_author=' . $this->request->get['filter_author'];}
		/*filter_date*/ 
		if (isset($this->request->get['filter_date'])) {
		$url .= '&filter_date=' . $this->request->get['filter_date'];}
				
		if (isset($this->request->get['filter_status'])) {
			$url .= '&filter_status=' . $this->request->get['filter_status'];
		}

		if (isset($this->request->get['sort'])) {
			$url .= '&sort=' . $this->request->get['sort'];
		}
												
		if (isset($this->request->get['order'])) {
			$url .= '&order=' . $this->request->get['order'];
		}
				
		$pagination = new Pagination();
		$pagination->total = $article_total;
		$pagination->page = $page;
		$pagination->limit = $this->config->get('config_admin_limit');
		$pagination->text = $this->language->get('text_pagination');
		$pagination->url = $this->url->link('news/article', 'token=' . $this->session->data['token'] . $url . '&page={page}', 'SSL');
			
		$this->data['pagination'] = $pagination->render();
	
		$this->data['filter_article_category'] = $filter_article_category;
		$this->data['filter_author'] = $filter_author;
		$this->data['filter_date'] = $filter_date;
		$this->data['filter_name'] = $filter_name;
		$this->data['filter_status'] = $filter_status;
		
		$this->data['sort'] = $sort;
		$this->data['order'] = $order;

		$this->template = 'news/article_list.tpl';
		$this->children = array(
			'common/header',
			'news/shortcut_route',
			'news/shortcut_module',
			'common/footer'
		);;
				
		$this->response->setOutput($this->render());
  	}

  	private function getForm() {
		

		
    	$this->data['heading_title'] = $this->language->get('heading_title'); 
    	$this->data['text_enabled'] = $this->language->get('text_enabled');
    	$this->data['text_disabled'] = $this->language->get('text_disabled');
    	$this->data['text_none'] = $this->language->get('text_none');
    	$this->data['text_yes'] = $this->language->get('text_yes');
    	$this->data['text_no'] = $this->language->get('text_no');
		$this->data['text_select_all'] = $this->language->get('text_select_all');
		$this->data['text_unselect_all'] = $this->language->get('text_unselect_all');
		$this->data['text_plus'] = $this->language->get('text_plus');
		$this->data['text_minus'] = $this->language->get('text_minus');
		$this->data['text_default'] = $this->language->get('text_default');
		$this->data['text_image_manager'] = $this->language->get('text_image_manager');
		$this->data['text_browse'] = $this->language->get('text_browse');
		$this->data['text_clear'] = $this->language->get('text_clear');
		$this->data['text_option'] = $this->language->get('text_option');
		$this->data['text_option_value'] = $this->language->get('text_option_value');
		$this->data['text_select'] = $this->language->get('text_select');
		$this->data['text_none'] = $this->language->get('text_none');
		$this->data['text_percent'] = $this->language->get('text_percent');
		$this->data['text_amount'] = $this->language->get('text_amount');

		$this->data['entry_name'] = $this->language->get('entry_name');
		$this->data['entry_meta_description'] = $this->language->get('entry_meta_description');
		$this->data['entry_meta_keyword'] = $this->language->get('entry_meta_keyword');
		$this->data['entry_description'] = $this->language->get('entry_description');
		$this->data['entry_store'] = $this->language->get('entry_store');
		$this->data['entry_keyword'] = $this->language->get('entry_keyword');
		$this->data['entry_author'] = $this->language->get('entry_author');
		$this->data['entry_poll'] = $this->language->get('entry_poll');
    	$this->data['entry_image'] = $this->language->get('entry_image');
    	$this->data['entry_download'] = $this->language->get('entry_download');
    	$this->data['entry_category'] = $this->language->get('entry_category');
		$this->data['entry_related_article'] = $this->language->get('entry_related_article');
		$this->data['entry_related_product'] = $this->language->get('entry_related_product');
		$this->data['entry_text'] = $this->language->get('entry_text');
		$this->data['entry_required'] = $this->language->get('entry_required');
		$this->data['entry_sort_order'] = $this->language->get('entry_sort_order');
		$this->data['entry_status'] = $this->language->get('entry_status');
		$this->data['entry_customer_group'] = $this->language->get('entry_customer_group');
		$this->data['entry_date_start'] = $this->language->get('entry_date_start');
		$this->data['entry_date_end'] = $this->language->get('entry_date_end');
		$this->data['entry_priority'] = $this->language->get('entry_priority');
		$this->data['entry_tag'] = $this->language->get('entry_tag');
		$this->data['entry_customer_group'] = $this->language->get('entry_customer_group');
		$this->data['entry_layout'] = $this->language->get('entry_layout');
				
    	$this->data['button_save'] = $this->language->get('button_save');
    	$this->data['button_cancel'] = $this->language->get('button_cancel');
		$this->data['button_add_image'] = $this->language->get('button_add_image');
		$this->data['button_remove'] = $this->language->get('button_remove');
		
    	$this->data['tab_general'] = $this->language->get('tab_general');
    	$this->data['tab_data'] = $this->language->get('tab_data');
    	$this->data['tab_image'] = $this->language->get('tab_image');		
		$this->data['tab_links'] = $this->language->get('tab_links');
		$this->data['tab_design'] = $this->language->get('tab_design');
		 
 		if (isset($this->error['warning'])) {
			$this->data['error_warning'] = $this->error['warning'];
		} else {
			$this->data['error_warning'] = '';
		}

 		if (isset($this->error['name'])) {
			$this->data['error_name'] = $this->error['name'];
		} else {
			$this->data['error_name'] = array();
		}

 		if (isset($this->error['meta_description'])) {
			$this->data['error_meta_description'] = $this->error['meta_description'];
		} else {
			$this->data['error_meta_description'] = array();
		}		
   
   		if (isset($this->error['description'])) {
			$this->data['error_description'] = $this->error['description'];
		} else {
			$this->data['error_description'] = array();
		}	
		
   		if (isset($this->error['author'])) {
			$this->data['error_author'] = $this->error['author'];
		} else {
			$this->data['error_author'] = '';
		}		
 		if (isset($this->error['keyword'])) {
			$this->data['error_keyword'] = $this->error['keyword'];
		} else {
			$this->data['error_keyword'] = '';
		}

		$url = '';

		if (isset($this->request->get['filter_name'])) {
			$url .= '&filter_name=' . urlencode(html_entity_decode($this->request->get['filter_name'], ENT_QUOTES, 'UTF-8'));
		}
		/*category*/
		if (isset($this->request->get['filter_article_category'])) {
		$url .= '&filter_article_category=' . $this->request->get['filter_article_category'];}
		/*author*/
		if (isset($this->request->get['filter_author'])) {
		$url .= '&filter_author=' . $this->request->get['filter_author'];}
		/*filter_date*/ 
		if (isset($this->request->get['filter_date'])) {
		$url .= '&filter_date=' . $this->request->get['filter_date'];}

		
		if (isset($this->request->get['filter_status'])) {
			$url .= '&filter_status=' . $this->request->get['filter_status'];
		}
								
		if (isset($this->request->get['sort'])) {
			$url .= '&sort=' . $this->request->get['sort'];
		}

		if (isset($this->request->get['order'])) {
			$url .= '&order=' . $this->request->get['order'];
		}
		
		if (isset($this->request->get['page'])) {
			$url .= '&page=' . $this->request->get['page'];
		}

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

   		$this->data['breadcrumbs'][] = array(
       		'text'      => $this->language->get('text_home'),
			'href'      => $this->url->link('common/home', 'token=' . $this->session->data['token'], 'SSL'),
			'separator' => false
   		);

   		$this->data['breadcrumbs'][] = array(
       		'text'      => $this->language->get('heading_title'),
			'href'      => $this->url->link('news/article', 'token=' . $this->session->data['token'] . $url, 'SSL'),
      		'separator' => ' :: '
   		);
									
		if (!isset($this->request->get['article_id'])) {
			$this->data['action'] = $this->url->link('news/article/insert', 'token=' . $this->session->data['token'] . $url, 'SSL');
		} else {
			$this->data['action'] = $this->url->link('news/article/update', 'token=' . $this->session->data['token'] . '&article_id=' . $this->request->get['article_id'] . $url, 'SSL');
		}
		
		$this->data['cancel'] = $this->url->link('news/article', 'token=' . $this->session->data['token'] . $url, 'SSL');

		if (isset($this->request->get['article_id']) && ($this->request->server['REQUEST_METHOD'] != 'POST')) {
      		$article_info = $this->model_news_article->getArticle($this->request->get['article_id']);
    	}

		$this->data['token'] = $this->session->data['token'];
		
		$this->load->model('localisation/language');
		
		$this->data['languages'] = $this->model_localisation_language->getLanguages();
		
		if (isset($this->request->post['article_description'])) {
			$this->data['article_description'] = $this->request->post['article_description'];
		} elseif (isset($this->request->get['article_id'])) {
			$this->data['article_description'] = $this->model_news_article->getArticleDescriptions($this->request->get['article_id']);
		} else {
			$this->data['article_description'] = array();
		}
		if (isset($this->request->get['article_id'])) {
			$this->data['article_id'] = $this->request->get['article_id'];
		} elseif (isset($this->request->post['author_id'])) {
			$this->data['article_id'] = $this->request->post['article_id'];
		} else{
			$this->data['article_id'] = FALSE;			
		}
		
		if (isset($this->request->post['model'])) {
      		$this->data['model'] = $this->request->post['model'];
    	} elseif (!empty($article_info)) {
			$this->data['model'] = $article_info['model'];
		} else {
      		$this->data['model'] = '';
    	}

		$this->load->model('setting/store');
		
		$this->data['stores'] = $this->model_setting_store->getStores();
		
		if (isset($this->request->post['article_store'])) {
			$this->data['article_store'] = $this->request->post['article_store'];
		} elseif (isset($this->request->get['article_id'])) {
			$this->data['article_store'] = $this->model_news_article->getArticleStores($this->request->get['article_id']);
		} else {
			$this->data['article_store'] = array(0);
		}	
		
		if (isset($this->request->post['keyword'])) {
			$this->data['keyword'] = $this->request->post['keyword'];
		} elseif (!empty($article_info)) {
			$this->data['keyword'] = $article_info['keyword'];
		} else {
			$this->data['keyword'] = '';
		}
		
		if (isset($this->request->post['image'])) {
			$this->data['image'] = $this->request->post['image'];
		} elseif (!empty($article_info)) {
			$this->data['image'] = $article_info['image'];
		} else {
			$this->data['image'] = '';
		}
		
		$this->load->model('tool/image');
		
		if (isset($this->request->post['image']) && file_exists(DIR_IMAGE . $this->request->post['image'])) {
			$this->data['thumb'] = $this->model_tool_image->resize($this->request->post['image'], 100, 100);
		} elseif (!empty($article_info) && $article_info['image'] && file_exists(DIR_IMAGE . $article_info['image'])) {
			$this->data['thumb'] = $this->model_tool_image->resize($article_info['image'], 100, 100);
		} else {
			$this->data['thumb'] = $this->model_tool_image->resize('no_image.jpg', 100, 100);
		}
	
		$this->load->model('news/author');
		
    	$this->data['authors'] = $this->model_news_author->getAuthors();

    	if (isset($this->request->post['author_id'])) {
      		$this->data['author_id'] = $this->request->post['author_id'];
		} elseif (!empty($article_info)) {
			$this->data['author_id'] = $article_info['author_id'];
		} else {
      		$this->data['author_id'] = 0;
    	} 
		
		$this->load->model('news/poll');
		$this->data['polls'] = $this->model_news_poll->getPolls();
    	if (isset($this->request->post['poll_id'])) {
      		$this->data['poll_id'] = $this->request->post['poll_id'];
		} elseif (!empty($article_info)) {
			$this->data['poll_id'] = $article_info['poll_id'];
		} else {
      		$this->data['poll_id'] = 0;
    	} 
		
		if (isset($this->request->post['sort_order'])) {
      		$this->data['sort_order'] = $this->request->post['sort_order'];
    	} elseif (!empty($article_info)) {
      		$this->data['sort_order'] = $article_info['sort_order'];
    	} else {
			$this->data['sort_order'] = 999;
		}
				
    	if (isset($this->request->post['status'])) {
      		$this->data['status'] = $this->request->post['status'];
    	} elseif (!empty($article_info)) {
			$this->data['status'] = $article_info['status'];
		} else {
      		$this->data['status'] = 1;
    	}

		
		if (isset($this->request->post['article_image'])) {
			$this->data['article_images'] = $this->request->post['article_image'];
		} elseif (isset($this->request->get['article_id'])) {
			$this->data['article_images'] = $this->model_news_article->getArticleImages($this->request->get['article_id']);
		} else {
			$this->data['article_images'] = array();
		}
		$this->data['no_image'] = $this->model_tool_image->resize('no_image.jpg', 100, 100);

		$this->load->model('news/download');
		
		$this->data['downloads'] = $this->model_news_download->getDownloads();
		
		if (isset($this->request->post['article_download'])) {
			$this->data['article_download'] = $this->request->post['article_download'];
		} elseif (isset($this->request->get['article_id'])) {
			$this->data['article_download'] = $this->model_news_article->getArticleDownloads($this->request->get['article_id']);
		} else {
			$this->data['article_download'] = array();
		}		
		
		$this->load->model('news/category');				
		$this->data['categories'] = $this->model_news_category->getCategories(0);
		
		if (isset($this->request->post['article_category'])) {
			$this->data['article_category'] = $this->request->post['article_category'];
		} elseif (isset($this->request->get['article_id'])) {
			$this->data['article_category'] = $this->model_news_article->getArticleCategories($this->request->get['article_id']);
		} else {
			$this->data['article_category'] = array();
		}		
			
		if (isset($this->request->post['related_article'])) {
			$this->data['related_article'] = $this->request->post['related_article'];
		} elseif (isset($article_info)) {
			$this->data['related_article'] = $this->model_news_article->getArticleRelated($this->request->get['article_id']);
		} else {
			$this->data['related_article'] = array();
		}

		$this->load->model('catalog/category');				
		$this->data['product_categories'] = $this->model_catalog_category->getCategories(0);
		
		if (isset($this->request->post['related_product'])) {
			$this->data['related_product'] = $this->request->post['related_product'];
		} elseif (isset($article_info)) {
			$this->data['related_product'] = $this->model_news_article->getProductRelated($this->request->get['article_id']);
		} else {
			$this->data['related_product'] = array();
		}

		
		if (isset($this->request->post['article_layout'])) {
			$this->data['article_layout'] = $this->request->post['article_layout'];
		} elseif (isset($this->request->get['article_id'])) {
			$this->data['article_layout'] = $this->model_news_article->getArticleLayouts($this->request->get['article_id']);
		} else {
			$this->data['article_layout'] = array();
		}

		$this->load->model('design/layout');
		
		$this->data['layouts'] = $this->model_design_layout->getLayouts();
										
		$this->template = 'news/article_form.tpl';
		$this->children = array(
			'common/header',
			'news/shortcut_route',
			'news/shortcut_module',
			'common/footer'
		);;
				
		$this->response->setOutput($this->render());
  	} 
	
  	private function validateForm() { 
	$this->load->model('news/utf8');
    	if (!$this->user->hasPermission('modify', 'news/article')) {
      		$this->error['warning'] = $this->language->get('error_permission');
    	}

    	foreach ($this->request->post['article_description'] as $language_id => $value) {
      		if (($this->model_news_utf8->utf8_strlen($value['name']) < 1) || ($this->model_news_utf8->utf8_strlen($value['name']) > 255)) {
        		$this->error['name'][$language_id] = $this->language->get('error_name');
      		}
    	}
    	if ($this->request->post['author_id']==0) {
      		$this->error['author'] = $this->language->get('error_author');
    	}
		 
		if ($this->error && !isset($this->error['warning'])) {
			$this->error['warning'] = $this->language->get('error_warning');
		}
		/*SEO Keyword*/ 
		if (isset($this->request->post['article_id'])&&isset($this->request->post['keyword'])) {
		 $keyword_total = $this->model_news_article->getTotalKeywordByObjectID($this->request->post['keyword'],'article_id',$this->request->post['article_id']);    
			if ($keyword_total) {
	  			$this->error['keyword'] = $this->language->get('error_keyword');	
			}				
		}elseif (!isset($this->request->post['article_id'])&&isset($this->request->post['keyword'])) {
		 $keyword_total = $this->model_news_article->getTotalKeyword($this->request->post['keyword']);    
			if ($keyword_total) {
	  			$this->error['keyword'] = $this->language->get('error_keyword');	
			}	
		}		
		/*SEO Keyword*/ 	
					
    	if (!$this->error) {
			return true;
    	} else {
      		return false;
    	}
  	}
	
  	private function validateDelete() {
    	if (!$this->user->hasPermission('modify', 'news/article')) {
      		$this->error['warning'] = $this->language->get('error_permission');  
    	}
		
		if (!$this->error) {
	  		return true;
		} else {
	  		return false;
		}
  	}
  	
  	private function validateCopy() {
    	if (!$this->user->hasPermission('modify', 'news/article')) {
      		$this->error['warning'] = $this->language->get('error_permission');  
    	}
		
		if (!$this->error) {
	  		return true;
		} else {
	  		return false;
		}
  	}
		
	public function autocomplete() {
		$json = array();
		
		if (isset($this->request->get['filter_name']) || isset($this->request->get['filter_model']) || isset($this->request->get['filter_article_category_id'])) {
			$this->load->model('news/article');
			
			if (isset($this->request->get['filter_name'])) {
				$filter_name = $this->request->get['filter_name'];
			} else {
				$filter_name = '';
			}
						
			if (isset($this->request->get['filter_article_category_id'])) {
				$filter_article_category_id = $this->request->get['filter_article_category_id'];
			} else {
				$filter_article_category_id = '';
			}
			
			if (isset($this->request->get['filter_sub_category'])) {
				$filter_sub_category = $this->request->get['filter_sub_category'];
			} else {
				$filter_sub_category = '';
			}
			
			if (isset($this->request->get['limit'])) {
				$limit = $this->request->get['limit'];	
			} else {
				$limit = 20;	
			}			
						
			$data = array(
				'filter_name'         => $filter_name,
				'filter_article_category_id'  => $filter_article_category_id,
				'filter_sub_category' => $filter_sub_category,
				'start'               => 0,
				'limit'               => $limit
			);
			
			$results = $this->model_news_article->getArticles($data);
			
			foreach ($results as $result) {					
				$json[] = array(
					'article_id' => $result['article_id'],
					'name'       => strip_tags(html_entity_decode($result['name'], ENT_QUOTES, 'UTF-8'))
				);	
			}
		}

		$this->response->setOutput(json_encode($json));
	}
	public function article_category() {
		$this->load->model('news/article');
		
		if (isset($this->request->get['article_category_id'])) {
			$article_category_id = $this->request->get['article_category_id'];
		} else {
			$article_category_id = 0;
		}
		
		$article_data = array();
		
		$results = $this->model_news_article->getArticlesByCategoryId($article_category_id);
		
		foreach ($results as $result) {
			$article_data[] = array(
				'article_id' => $result['article_id'],
				'name'       => $result['name']
			);
		}		
		$this->response->setOutput(json_encode($article_data));
	}
	
	public function related_article() {
		$this->load->model('news/article');
		
		if (isset($this->request->post['related_article'])) {
			$articles = $this->request->post['related_article'];
		} else {
			$articles = array();
		}
	
		$article_data = array();
		
		foreach ($articles as $article_id) {
			$article_info = $this->model_news_article->getArticle($article_id);
			
			if ($article_info) {
				$article_data[] = array(
					'article_id' => $article_info['article_id'],
					'name'       => $article_info['name']
				);
			}
		}		
		$this->response->setOutput(json_encode($article_data));
	}
	
	public function product_category() {
		$this->load->model('catalog/product');
		
		if (isset($this->request->get['category_id'])) {
			$category_id = $this->request->get['category_id'];
		} else {
			$category_id = 0;
		}
		
		$product_data = array();
		
		$results = $this->model_catalog_product->getProductsByCategoryId($category_id);
		
		foreach ($results as $result) {
			$product_data[] = array(
				'product_id' => $result['product_id'],
				'name'       => $result['name']
			);
		}		
		$this->response->setOutput(json_encode($product_data));
	}
	public function related_product() {
		$this->load->model('catalog/product');
		
		if (isset($this->request->post['related_product'])) {
			$products = $this->request->post['related_product'];
		} else {
			$products = array();
		}
	
		$product_data = array();
		
		foreach ($products as $product_id) {
			$product_info = $this->model_catalog_product->getProduct($product_id);
			
			if ($product_info) {
				$product_data[] = array(
					'product_id' => $product_info['product_id'],
					'name'       => $product_info['name']
				);
			}
		}		
		$this->response->setOutput(json_encode($product_data));
	}
}
?>