<?php
// +------------------------------------------------------------------------+
// | PHP Melody version 1.6.7 ( www.phpsugar.com )
// +------------------------------------------------------------------------+
// | PHP Melody IS NOT FREE SOFTWARE
// | If you have downloaded this software from a website other
// | than www.phpsugar.com or if you have received
// | this software from someone who is not a representative of
// | phpSugar, you are involved in an illegal activity.
// | ---
// | In such case, please contact: support@phpsugar.com.
// +------------------------------------------------------------------------+
// | Developed by: phpSugar (www.phpsugar.com) / support@phpsugar.com
// | Copyright: (c) 2004-2011 PhpSugar.com. All rights reserved.
// +------------------------------------------------------------------------+

$showm = 'mod_article';
include('header.php');

$action = $_GET['do'];
if ( ! in_array($action, array('edit', 'new', 'delete')) )
{
	$action = 'new';	//	default action
}

?>
<script type="text/javascript">
$(document).ready(function(){
	$("img[name='article_thumbnail']").click(function() {
		//alert($(this).parent().css);
		var img = $(this);
		var ul = img.parents('.thumbs_ul');
		var li = img.parent();
		var input = ul.next("input[name='post_thumb_show']");
		
		if ( ! li.hasClass('thumbs_li_selected'))
		{
			ul.children().removeClass('thumbs_li_selected').addClass('thumbs_li_default');
			li.addClass('thumbs_li_selected');
			input.val(img.attr('src'));
		}
	});
});
</script>
<div id="sub_menu">
<li><a href="article_manager.php?do=new">Post a new article</a></li>
<li><a href="articles.php">Manage articles</a></li>
<li><a href="article_categories.php">Manage categories</a></li>
<?php 
if (is_admin() || (is_moderator() && mod_can('manage_comments')))
{
	?>
	<li><a href="comments.php?page=1&filter=articles">Manage comments</a></li>
	<?php
}
?>
</div>

<div id="content">
<h2>Write your article</h2>
<div id="display_result" style="display:none;"></div>
 <?php
 if ( ! $config['mod_article'])
 {
 	?>
 	 <div class="info_msg">
	  The Article Module is currently disabled. Please enable it from '<a href="settings.php">Settings</a>'.
	 </div>
	</div>
	<?php
	include('footer.php');
	exit();
 }
 ?>
<?php 

$inputs = array();

if ('' != $_POST['submit'])
{
	$_POST['title'] = after_post_filter($_POST['title']);
	$_POST['tags'] = after_post_filter($_POST['tags']);
	
	if ($action == 'new')
	{
		$result = insert_new_article($_POST);
	}
	else if ($action == 'edit')
	{
		$result = update_article($_POST['id'], $_POST);
	}
	
	if ($result['type'] == 'error')
	{
		echo '<div class="info_msg_err"><strong>'. $result['msg'] .'</strong></div>';
	}
	else
	{
		if ($action == 'new')
		{
			echo '<div class="info_msg_ok"><strong>'. $result['msg'] .'.</strong> <a href="'. _URL .'/article_read.php?a='.$result['article_id'].'&mode=preview" title="View article">View article</a></div>';
			echo '<br />';
			echo '<input name="continue" type="button" value="&laquo; Manage articles" onClick="location.href=\'articles.php\'" class="inputbutton" />';
			echo '<input name="add_new" type="button" value="Post a new article &raquo;" onClick="location.href=\'article_manager.php?do=new\'" class="inputbutton" />';
			
			include('footer.php');
			exit();
		}
		else
		{
			echo '<div class="info_msg_ok"><strong>'. $result['msg'] .'</strong> <a href="'. _URL .'/article_read.php?a='. $_POST['id'] .'&mode=preview">View article</a></div>';
			echo '<br />';
			echo '<input name="continue" type="button" value="&laquo; Manage articles" onClick="location.href=\'articles.php\'" class="inputbutton" />';
			echo '<input name="add_new" type="button" value="Post a new article &raquo;" onClick="location.href=\'article_manager.php?do=new\'" class="inputbutton" />';
		}
	}	
}

if ($action == 'edit')
{
	$id = (int) $_GET['id'];
	if ($id == 0)
	{
		$action = 'new';
		$inputs = array();
		$inputs['allow_comments'] = 1;
		$inputs['status'] = 1;
		$inputs['author'] = $userdata['id'];
		$inputs['category_as_arr'] = array();
	}
	else
	{
		$inputs = get_article($id);
	}
}
else if ($action == 'new')
{
	if ('' != $_POST['submit'])
	{
		$inputs = $_POST;
	}
	else
	{
		$inputs['allow_comments'] = 1;
		$inputs['status'] = 1;
		$inputs['author'] = $userdata['id'];
	}
	if ( ! is_array($inputs['category_as_arr']))
	{
		$inputs['category_as_arr'] = array();
	}
}

$categories = art_get_categories();

//	Filter some fields before output
$inputs['title'] = pre_post_filter($inputs['title']);
$inputs['tags'] = pre_post_filter($inputs['tags']);

?>
  <form name="write_article" method="post" action="article_manager.php?do=<?php echo $action; ?>&id=<?php echo $_GET['id'];?>"
   onsubmit="return validateFormOnSubmit(this, 'Please fill in the required fields (highlighted).')">
<div id="write_article">
   <table width="100%" cellpadding="5" cellspacing="0">
    <tr>
      <td width="70%"><span class="article_field_label">Title</span></td>
      <td width="30%" rowspan="15" valign="top">
	  <table width="90%" border="0" align="center" cellpadding="3" cellspacing="1" class="article_rightp">
        <tr>
          <td colspan="2"><span class="article_field_label">Category</span></td>
        </tr>
        <tr>
          <td colspan="2">
			<?php
				$str = '<ul>';
	/*
				if(count($categories) > 0)
				{
					foreach ($categories as $id => $cat_arr)
					{
						if ($cat_arr['parent_id'] == 0)
						{
							$str .= '<li><label>';
							$str .= ' <input type="checkbox" name="categories[]" value="'. $id .'"';
							if ( ! is_array($inputs['category_as_arr']))
							{
								$inputs['category_as_arr'] = array();
							}
							if (array_key_exists($id, $inputs['category_as_arr']))
							{
								$str .= ' checked ';
							}
							$str .= ' /> ';
							$str .= '<strong>';
							$str .= $cat_arr['name'];
							$str .= '</strong>';
							$str .= '</label>';
							$str .= '</li>';
						}
						
						//	subcategories
						foreach ($categories as $parent_id => $subcategory)
						{
							if ($subcategory['parent_id'] == $id)
							{						
								$str .= '<li><label>';
								$str .= '<img src="images/ico_subcat2.gif">';
								$str .= '<input type="checkbox" name="categories[]" value="'. $subcategory['id'] .'"';
								if (array_key_exists($subcategory['id'], $inputs['category_as_arr']))
								{
									$str .= ' checked ';
								}
								$str .= ' /> ';
								$str .= $subcategory['name'];
								$str .= '</label>';
								$str .= '</li>';
							}
						}
					} //	end foreach (categories)
					$str .= '</ul>';
					echo $str;
				}
				else	//	No categories
				{
					echo 'You haven\'t defined any categories. Please <a href="article_categories.php">create</a> a new category before posting a new article'; 
				}*/
			?>
			 <input type="hidden" name="categories_old" value="<?php echo $inputs['category'];?>"  />
			 
			 <?php 
			 $checklist_options = array('selected' => explode(',', $inputs['category']));
			 echo art_cats_checklist($categories, $checklist_options);?>
			 
			</td>
        </tr>
		<?php 
		if($action == 'edit')
		{
		?>
		<tr>
          <td colspan="2"><span class="article_field_label">Post thumbnail</span></td>
        </tr>
		<tr>
          <td colspan="2">
		   <div id="artThumbs">
		<?php

			$all_meta = $inputs['meta']['*'];
 			$total_thumbs = count($all_meta['post_thumb']);
					
			if ($total_thumbs > 0)
			{ 
				echo '<ul class="thumbs_ul">';
				
				// display current selected thumbnail
				if ($inputs['meta']['post_thumb_show'] != '')
				{
					echo '<li class="thumbs_li_selected"><img src="images/bg_selected_thumb.gif" alt="" border="0" style="display:none" /><img src="'. _ARTICLE_ATTACH_DIR . $inputs['meta']['post_thumb_show'] .'" width="'. THUMB_W_ARTICLE .'" height="'. THUMB_H_ARTICLE .'" alt="Thumb 1" name="article_thumbnail" /></li>';	
				}
				
				// display next thumbnails available for this post.
				$limit = 3;
				for ($i = 0; $i < $limit; $i++)
				{
					if (strlen($all_meta['post_thumb'][$i]) > 0)
					{
						if ($all_meta['post_thumb'][$i] != $inputs['meta']['post_thumb_show'])
						{
							echo '<li class="thumbs_li_default"><img src="images/bg_selected_thumb.gif" alt="" border="0" style="display:none" /><img src="'. _ARTICLE_ATTACH_DIR . $all_meta['post_thumb'][$i] .'" width="'. THUMB_W_ARTICLE .'" height="'. THUMB_H_ARTICLE .'"  alt="Thumb '. ($i + 2) .'" name="article_thumbnail" /></li>';
						}
						else
						{
							$limit++;
						}
						
						if ($limit > 99)
						{
							break;
						}
					}
				}
				echo '</ul>';
			} // end if ($total_thumbs > 0)
			else
			{
				echo '<em>There are no thumbnails associated with this article. To create a thumbnail for this article first upload an image then press "Save".</em>';
			}
		?>
			<input type="hidden" name="post_thumb_show" value="<?php if ($inputs['meta']['post_thumb_show'] != '') echo $inputs['meta']['post_thumb_show'];?>" />
		   </div>
		  </td>
        </tr>
		<?php
		} // end if action == edit
		?>
		
        <tr>
          <td colspan="2"><span class="article_field_label">Availability</span></td>
        </tr>
        <tr>
          <td colspan="2">
			<label><input type="radio" name="status" value="0" <?php if ($inputs['status'] == 0) echo 'checked="checked"'; ?> /> Private</label> 
			<label><input name="status" type="radio" value="1" <?php if ($inputs['status'] == 1) echo 'checked="checked"'; ?> /> Public</label>		  </td>
        </tr>
        <tr>
          <td colspan="2"><span class="article_field_label">Tags</span></td>
        </tr>
        <tr>
          <td colspan="2">
		  <input name="tags" type="text" value="<?php echo $inputs['tags']; ?>" size="50" style="width:100%" />
		  <br /><small><em>(e.g. inteview, bio, madonna)</em></small>		  </td>
        </tr>
        <tr>
          <td colspan="2"><span class="article_field_label">Commenting</span></td>
        </tr>
        <tr>
          <td colspan="2">
		  <label><input name="allow_comments" type="checkbox" value="1" <?php if ($inputs['allow_comments'] == 1) echo 'checked="checked"';?> /> Allow comments on this article</label>		  </td>
        </tr>
		<tr>
        <tr>
          <td colspan="2">&nbsp;</td>
        </tr>
        <tr>
          <td align="left"><input name="cancel" type="button" value="Cancel" onClick="location.href='articles.php'" class="deletebutton" /></td>
          <td width="50%" align="right"><input name="submit" type="submit" <?php echo ($action == 'edit') ? 'value="Save"' : 'value="Publish"';?> class="approvebutton" /></td>
        </tr>
      </table></td>
    </tr>
    <tr>
	 <td width="47%">
	  <input name="title" type="text" id="must" value="<?php echo $inputs['title']; ?>" style="width: 99%;" class="article_title" />	 </td>
	 </tr>
	<tr>
	  <td><span class="article_field_label">Article</span></td>
	  </tr>
	<tr>
	 <td>
        	 <script language="javascript">
				if(navigator.userAgent.toLowerCase().indexOf('chrome/1') > -1)	//	not compatible with Chrome and older versions of Safari.
				{
					document.write('<div class="info_msg">If the article editor does not contain a RICH TEXT editor please upgrade your browser or try a different browser. Thank you!</div>');
				} else if(navigator.userAgent.toLowerCase().indexOf('safari') > -1) 
				{
					document.write('<div class="info_msg">If the article editor does not contain a RICH TEXT editor please upgrade your browser or try a different browser. Thank you!</div>');
				}
			 </script>
			 <textarea name="content" cols="100" id="textarea-WYSIWYG" style="height: 350px;"><?php echo $inputs['content']; ?></textarea>
				<script language="javascript1.2">
					if(navigator.userAgent.toLowerCase().indexOf('chrome/1') == -1)
					{
						// Overwrite WYSIWYG Width and Height
						wysiwygWidth = '100%';
						wysiwygHeight = 350;
						generate_wysiwyg('textarea-WYSIWYG');
					}
				</script>
				<script type="text/javascript">
					//	Editor not loaded?
					if ($("#wysiwygtextarea-WYSIWYG").length == 0)
					{
						// restore textarea
						$("#textarea-WYSIWYG").show();
					}
				</script>
	  </td>
	 </tr>
	<tr>
	  <td><span class="article_field_label">Select image to upload</span></td>
	  </tr>
	<tr>
	 <td>
	  <script type="text/javascript">
		var swfu;
		var swfu_msg = {
				queue_limit_exceeded : "You have attempted to queue too many files.",
				file_exceeds_size_limit : "This file is too big.", 
				zero_byte_file : "This file is empty. Please try another.",
				invalid_filetype : "This file type is not allowed. Please try another.",
				default_error : "An error occurred in the upload. Please try again later.",
				missing_upload_url : "There was a configuration error. Please contact the server administrator.",
				upload_limit_exceeded : "You may only upload 1 file.",
				http_error : "HTTP error: ",
				upload_failed: "Upload failed.",
				io_error: "IO error.",
				security_error: "Security error.",
				file_cancelled: "File cancelled.",
				upload_stopped: "Upload stopped.",
				dismiss: "Dismiss",
				crunching: "Crunching&hellip;",
				deleted: "Deleted", 
				file_validation_failed: "Failed Validation.  Upload skipped."
		};
		
		window.onload = function() {
		
			var settings = {
				flash_url : "swfupload/swfupload.swf",
				upload_url: "upload_image.php",
				file_size_limit : "<?php echo ($upload_max_filesize > 0) ? $upload_max_filesize.' B' : '0';?>",
				file_types : "*.jpg; *.gif; *.png",
				file_types_description : "Images",
				file_upload_limit : 10,
				file_queue_limit : 1,
				custom_settings : {
					progressTarget : "fsUploadProgress"
				},
				post_params: {
						"PHPSESSID" : "<?php echo session_id(); ?>"
						},

				// Button settings
				button_image_url: "swfupload/upload.png",
				button_placeholder_id: "ButtonPlaceHolder",
				button_width: "132",
				button_height: "24",
				button_text: '<span class="button">Browse ...</span>',
				button_text_style: '.button { text-align: left; font-weight: bold; font-family:"Lucida Grande",Verdana,Arial,"Bitstream Vera Sans",sans-serif; }',
				button_text_top_padding: 3,
				button_text_left_padding: 18,
				button_window_mode: "window",
				button_cursor: SWFUpload.CURSOR.HAND,
				
				// The event handler functions are defined in handlers.js
				file_queued_handler : fileQueued,
				file_queue_error_handler : fileQueueError,
				file_dialog_complete_handler : fileDialogComplete,
				upload_start_handler : uploadStart,
				upload_progress_handler : uploadProgress,
				upload_error_handler : uploadError,
				upload_success_handler : uploadSuccess,
				upload_complete_handler : uploadComplete, 
				
				debug : false
			};

			swfu = new SWFUpload(settings);
	     };
		</script>
	  <div id="fsUploadProgress"></div>
	  <div>
	  <span id="ButtonPlaceHolder"></span></div>
	  <div id="divStatus"></div>
	  </td>
	 </tr>
	<tr>
	 <td>
	  <input type="hidden" name="author" value="<?php  echo $inputs['author'];?>" />
	  <input type="hidden" name="id" value="<?php echo $inputs['id'];?>" />
	  </td>
	 </tr>
  </table>
</div>
</form>
</div>
<?php
include('footer.php');
?>