<?php
/*======================================================================================
Module: Articles
======================================================================================*/

// Check authorisation
if (!isset($gCms)) exit;
if ( !$this->CheckPermission('Articles: modify categories') ) exit;
$errors = array();
#print_r($params);exit;
// get parameters
$tabto = preg_replace('/[^0-9a-zA-Z_]/','',$params['tabto']);
$category = isset($params['category']) ? (int)$params['category'] : 0;
$belongs_to = isset($params['belongs_to']) ? (int)$params['belongs_to'] : 0;
$active = $params['active'] ? 1 : 0;
$title = isset($params['title']) ? $params['title'] : '';
$short_desc = isset($params['short_desc']) ? $params['short_desc'] : '';
$long_desc = isset($params['long_desc']) ? $params['long_desc'] : '';
$return_id = isset($params['return_id']) ? (int)$params['return_id'] : $this->GetPreference('returnid');
$image = '';
$image_del = isset($params['image_del']) ? $params['image_del'] : 0;
$seo_url = isset($params['seo_url']) ? $params['seo_url'] : '';
$meta_title = isset($params['meta_title']) ? $params['meta_title'] : '';
$meta_description = isset($params['meta_description']) ? $params['meta_description'] : '';
$noindex = $params['noindex'] ? 1 : 0;
$nofollow = $params['nofollow'] ? 1 : 0;
$noarchive = $params['noarchive'] ? 1 : 0;
$noodp = $params['noodp'] ? 1 : 0;
$notranslate = $params['notranslate'] ? 1 : 0;

// Save category changes
if (isset($params['submit']) || isset($params['cache']) || isset($params['add']) || isset($params['cacheadd']) || isset($params['apply'])) {
	// Edit category
	if ($category) {
		$sql = 'UPDATE '.cms_db_prefix().'module_articles_category SET belongs_to=?, title=?, short_desc=?, long_desc=?, active=?, returnid=?, seo_url=?, meta_title=?, meta_description=?, noindex=?, nofollow=?, noarchive=?, noodp=?, notranslate=? WHERE entry_id=?';
		$res = $db->Execute($sql,array($belongs_to, $title, $short_desc, $long_desc, $active, $return_id, $seo_url, $meta_title, $meta_description, $noindex, $nofollow, $noarchive, $noodp, $notranslate, $category));
		if(!$res) { $errors[] = 'SQL ERROR: ' . $db->ErrorMsg() . '(with ' . $db->sql . ')'; }
		// Modify category content for the search module
		$module = cms_utils::get_search_module();
		if (is_object($module) ) {
			$module->DeleteWords($this->GetName(),$category,'category');
			$text = $title.' '.$short_desc.' '.$long_desc;
			$module->AddWords($this->GetName(), $category, 'category', $text);
		}
		// Send event
		@$this->SendEvent('Articles_CategoryEdited', array('category' => $category, 'title' => $title, 'content' => $long_desc, 'summary' => $short_desc, 'active' => $active));
	// Insert new category
	} else {
		// Get maximum order number
		$maxorder = (int)$db->GetOne('SELECT sort_order FROM '.cms_db_prefix().'module_articles_category ORDER BY sort_order DESC', array());
		$maxorder++;
		// Insert article
		$sql = 'INSERT INTO '.cms_db_prefix().'module_articles_category (belongs_to, title, short_desc, long_desc, active, sort_order, returnid, seo_url, meta_title, meta_description, noindex, nofollow, noarchive, noodp, notranslate) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)';
		$res = $db->Execute($sql,array($belongs_to, $title, $short_desc, $long_desc, $active, $maxorder, $return_id, $seo_url, $meta_title, $meta_description, $noindex, $nofollow, $noarchive, $noodp, $notranslate));
		$category = $db->GetOne('SELECT entry_id FROM '.cms_db_prefix().'module_articles_category WHERE title = ? ORDER BY entry_id DESC LIMIT 1', array($title));
		// Add category content to the search module
		if($category) {
			//Update search index
			$module = cms_utils::get_search_module();
			if (is_object($module)) {
				$text = $title.' '.$short_desc.' '.$long_desc;
				$module->AddWords($this->GetName(), $category, 'category', $text);
			}
		}
		// Send event
		if ($category) @$this->SendEvent('Articles_CategoryAdded', array('category' => $category, 'title' => $title, 'content' => $long_desc, 'summary' => $short_desc, 'active' => $active));
	}
	// Delete image
	if ($image_del) {
		$filename = $db->GetOne('SELECT image_url FROM '.cms_db_prefix().'module_articles_category WHERE entry_id = ?', array($category));
		if ($filename) {
			$deldir = cms_join_path($config['image_uploads_path'],'Articles','categories',$category);
			$delfile = cms_join_path($deldir,$filename);
			if (is_file($delfile)) unlink($delfile);
		}
		$db->Execute('UPDATE '.cms_db_prefix().'module_articles_category SET image_url = "" WHERE entry_id = ?', array($category));
	}
	// Upload image
	if (is_array($_FILES)) {
		$params_prefix_tmp = explode(',',$_REQUEST['mact']);
		$params_prefix = $params_prefix_tmp[1];
		$iname = $params_prefix.'image';
		$image = isset($_FILES[$iname]) ? $_FILES[$iname]['name']: '';
		#echo $image;exit;
		if ($image) {
			#$base = cms_join_path($config['image_uploads_path'],'Articles'.DIRECTORY_SEPARATOR.'categories');
			$newdir = cms_join_path($config['image_uploads_path'],'Articles','categories',$category);
			$dest = cms_join_path($newdir,basename($image));
			#print_r($_FILES);echo "<br />newdir:'$newdir'<br />dest:'$dest'<br />image:'$image'";exit;
			if (!is_dir($newdir)) {
				if (!mkdir($newdir)) $errors[] = "Error creating dir $newdir";
			}
			if (is_dir($newdir)) {
				if (move_uploaded_file($_FILES[$iname]['tmp_name'],$dest)) { $db->Execute('UPDATE '.cms_db_prefix().'module_articles_category SET image_url = ? WHERE entry_id = ?', array(basename($image),$category)); }
				else { $errors[] = "Upload failed : Unable to write $dest"; }
			}
		}
		
	}
}

// Redirect
if (isset($params['submit']) || isset($params['add'])) $this->Redirect($id, 'defaultadmin', $returnid, array('tabto'=>$tabto, 'errors' => implode(',',str_replace(',',';',$errors)), 'message' => $this->Lang('changessaved')));
elseif (isset($params['cancel'])) $this->Redirect($id, 'defaultadmin', $returnid, array('tabto'=>$tabto));
else $this->Redirect($id, 'defaultadmin', $returnid, array( 'tabto'=>$tabto, 'category'=>$category ));

// EOF