<?php
if (!class_exists('PHPWS_ArticleManager'))
  require_once(PHPWS_SOURCE_DIR . 'mod/article/class/ArticleManager.php');
if (!class_exists('PHPWS_Article_Section'))
  require_once(PHPWS_SOURCE_DIR . 'mod/article/class/Section.php');
/**
* This is the PHPWS_Article class.  It handles saving, updating, and organization
* of sections.  It also contains functions that allow this article to be edited
* and saved.
*
* @version $Id: Article.php,v 1.23 2004/11/22 03:57:35 adarkling Exp $
*
* @author Eloi George <eloi@NOSPAM.bygeorgeware.com>
* @module Article Manager
* @param int id : Database id of this article
* @param string title : Title of this article 
* @param array sections : Array of sections controlled by this article (key=SECT_id, value=SECT_OBJ)
* @param array order : Array denoting the order of the sections within this article (key=order 0,1,2..., value=SECT_id)
* @param int sectionID : Generated alphabetical temporary section ids
* @param array RemoveList : Generated array of sections to be deleted on article save
* @param int mainarticle : Whether this article is the main article for this site or not
* @param int rank : This article's priority position in a summary listing
* @param string created_username : username of the user that created this article
* @param date created_date : Date this article was created
* @param string updated_username : username of the lastuser to update this article
* @param date updated_date : Date this article was last updated
* @param string summary : The Section that serves as summary for this article
* @param int allow_comments : Whether users can submit & view comments
* @param int allow_anonymous : Whether anonymous users can submit & view comments
* @param int hits : Keeps track of how many times this article was accessed
* @param int expiration_date : Date this article will become obsolete
*                       A NULL value means that the article will never expire
* @param int publication_date : Date this article was/will be published
*                       A NULL value means that the article is unapproved for display
* @param int wordcount : length of article content not including the summary
* @param int pages    : Number of pages in this article
* @param int template : Display template for this article
* @param int editlock : UNIX time this article was opened for editing
* @param int edituser : ID of the user that opened this article for editing
* @param int approved : Whether or not this article is approved for display
* @param int version : Version number of this page
* @param int editmode : Temporary Flag showing what mode we're in
* @param int announce : Whether or not this article will show up in the homepage summary list
* @param string meta_keywords : Keywords to include in this page's metatags 
* @param string meta_robots : Search Engine instructions for this page's metatags 
* @param string oldversion : Serialized copy of this article for archiving
* @param string language : 2-character code of the language that this article is written in
* @param int translation_group : All articles that are translations of each other carry the same id
*
*/
class PHPWS_Article
{
	var $id;
	var $title;
	var $sections;
	var $order;
	var $sectionID;
	var $RemoveList;
	var $mainarticle;
	var $rank;
	var $created_username;
	var $created_id;
	var $updated_username;
	var $updated_id;
	var $created_date;
	var $updated_date;
	var $summary;
	var $allow_comments;
	var $allow_anonymous;
	var $hits;
	var $expiration_date;
	var $publication_date;
	var $wordcount;
	var $pages;
	var $template;
	var $editlock;
	var $edituser;
	var $approved;
	var $version;
	var $editmode;
	var $announce;
	var $menuLink;
	var $meta_keywords;
	var $meta_robots;
  var $oldversion;
  var $language;
  var $translation_group;

	/**
	* Constructor for the PHPWS_Article object. ARTICLE_id = database id of a saved article.
	*
  * @author Eloi George <eloi@NOSPAM.bygeorgeware.com>
	* @module Article Manager
	* @param int id : Database id of the article to be instantiated (sp? heh).
	* @param int data : If this article is to be optimized for summary display (no sections),
	*                   the data will be sent as an array.
	* @return none
	*/
	function PHPWS_Article ($id = NULL, $data = NULL)
	{
    $this->menuLink = '<div style="text-align:center">'
      . PHPWS_Text::moduleLink($_SESSION['translate']->it('Main Menu'), 'article'
        ,array('disp'=>'main_menu')).'</div>';
    $this->sectionID = 'a';
    $this->editmode = FALSE;

		/* If this is to be an existing article... */
		if($id) 
		{
			/* If this is a regular (full) article, load all variables into $data */
			if ($data==NULL) 
			{
		    if (!$data = $GLOBALS['core']->quickFetch('SELECT * FROM '
		        . $_SESSION['SES_ART_master']->sql_article_table
            .' WHERE id = '.$id . $_SESSION['SES_ART_master']->sql_predicate))
        {
          $_SESSION['SES_ART_master']->error['article'][] = $_SESSION['translate']->it('Article data could not be found');
          return false;
        }
		    /* Load section data */
		    $this->order = unserialize($data['section_order']);
				if ((count($this->order)>0) 
						&& $result = $GLOBALS['core']->query('SELECT * FROM mod_article_sections WHERE id IN ('
							. implode(',', $this->order).')',1))
					while ($row = $result->fetchrow(DB_FETCHMODE_ASSOC)) 
				    $this->sections[$row['id']] = new PHPWS_Article_Section($row['id'],$row);
				else
          $_SESSION['SES_ART_master']->error['article'][] = $_SESSION['translate']->it('Section data could not be found');
			}

			/* Assign data to class variables */
			$this->id = $id;
			$this->title = $data['title'];
			$this->mainarticle = $data['mainarticle'];
			$this->rank = $data['rank'];
			$this->created_username = $data['created_username'];
			$this->created_id = $data['created_id'];
			$this->updated_username = $data['updated_username'];
			$this->updated_id = $data['updated_id'];
			$this->created_date = $data['created_date'];
			$this->updated_date = $data['updated_date'];
			$this->summary = $data['summary'];
			$this->allow_comments = $data['allow_comments'];
			$this->allow_anonymous = $data['allow_anonymous'];
			$this->hits = $data['hits'];
			$this->expiration_date = $data['expiration_date'];
			$this->publication_date = $data['publication_date'];
			$this->wordcount = $data['wordcount'];
			$this->pages = $data['pages'];
			$this->template = $data['template'];
			$this->editlock = $data['editlock'];
			$this->edituser = $data['edituser'];
			$this->approved = $data['approved'];
			$this->version = $data['version'];
			$this->announce = $data['announce'];
			$this->meta_keywords = $data['meta_keywords'];
			$this->meta_robots = $data['meta_robots'];
//			$this->language = $data['language'];
//			$this->translation_group = $id;
		}
		// otherwise this is a new article.  Load Default values.
		else
		{
			$this->title = "";
			$this->sections = array();
			$this->order = array();
			$this->mainarticle = 0;
			$this->rank = 50;
			if ($_SESSION['OBJ_user']->username) {
			  $this->created_username = $this->updated_username = $_SESSION['OBJ_user']->username;
			  $this->created_id = $this->updated_id = $_SESSION['OBJ_user']->user_id;
			}
			else {
		    $this->created_username = $this->updated_username = "";
		    $this->created_id = $this->updated_id = "";
		  }
			$this->created_date = date("Y-m-d H:i:s");
			$this->updated_date = date("Y-m-d H:i:s");
			$this->summary = "";
			$this->allow_comments = $_SESSION['SES_ART_master']->val['default_allow_comments'];
			$this->allow_anonymous = $_SESSION['SES_ART_master']->val['default_allow_anon'];
			$this->hits = 0;
			$this->wordcount = 0;
			$this->expiration_date = ($_SESSION['SES_ART_master']->val['can_expire']) 
          ?date("Y-m-d H:i:s", mktime(0,0,0
              ,date('m')+$_SESSION['SES_ART_master']->val['expiration_months']
              ,date('d')+$_SESSION['SES_ART_master']->val['expiration_days']
              ,date('Y')+$_SESSION['SES_ART_master']->val['expiration_years']))
          :NULL;
			$this->publication_date = date("Y-m-d H:i:s");
			$this->pages = 1;
			$this->template = $_SESSION['SES_ART_master']->val['default_article_template'];
	    $this->approved = (!$_SESSION['SES_ART_master']->val['need_approval'] || $_SESSION['OBJ_user']->allow_access('approval')) ?1:0; 
 			$this->version = -1;
			$this->announce = 1;
			$this->meta_keywords = $this->meta_robots = '';
			$this->language = $_SESSION['translate']->current_language;
			$this->translation_group = $data['translation_group'];
		}
	}

	/**
	* Displays this article to the user.
	*
  * @author Eloi George <eloi@NOSPAM.bygeorgeware.com>
	* @module Article Manager
	* @param bool Fullpage : FALSE = Summary Display 
	* @param bool returnHTML : TRUE = returns the entire output at function termination 
	* @return string
	* @access public
	*/
	function view ($Fullpage = true, $returnHTML = false)
	{
		/* If this is a fullpage view & there's no data, they aren't authorized to view this page */
		if ($Fullpage && !$this->pages)
    {
			$_SESSION['SES_ART_master']->error['article'][] = 
			    $_SESSION['translate']->it('The article you requested either does not exist or is not available to your group.');
			$GLOBALS['CNT_article']['content'] = $_SESSION['SES_ART_master']->get_error('article');
			return;
  	}
    /* Now test to see if article is publishable */
		if ($Fullpage && !$this->is_viewable()) 
		{
		  $GLOBALS['CNT_article']['title'] = $_SESSION['translate']->it('Sorry!');
		  if(!$this->approved)
			  $_SESSION['SES_ART_master']->error['article'][] =  
			    $_SESSION['translate']->it('The article you requested is awaiting approval by an administrator.');
			else 
			  $_SESSION['SES_ART_master']->error['article'][] =  
			    $_SESSION['translate']->it('The article you requested is not viewable.');
      $GLOBALS['CNT_article']['content'] = $_SESSION['SES_ART_master']->get_error('article');
      PHPWS_Fatcat::deactivate($this->id, 'article');
			return;
		}

    /* My copyright statement. Do not disable or remove. */
    $rights = array( 'http://www.vibride.com','http://www.realvidreams.com','http://www.bygeorgeware.com','http://www.bygeorgeadvertising.com');
		$copyright = '<a href="'.$rights[rand(0,2)].'" title="Article Manager module by by George! Software."><img src="http://'.PHPWS_SOURCE_HTTP 
  			. 'mod/article/img/mark.gif" border="0" alt="Article Manager module by by George! Software." /></a>';
    $article_link = './index.php?module=article&amp;view='.$this->id; 

		$article_tags['TITLE'] = $this->title;
		if(isset($_REQUEST['viewarchive']))
		  $article_tags['TITLE'] .= '<br />'.$_SESSION['translate']->it('Archived Version [var1]', $this->version);
		if ($this->summary)
  	  $article_tags['SUMMARYTEXT'] = PHPWS_Text::parseOutput($this->summary);
  	if(!$this->approved)
  	  $article_tags['SUMMARYTEXT'] = PHPWS_Text::parseOutput($this->summary)
  	        . '<br /><span class="errortext">'.$_SESSION['translate']->it('<b>NOTE:</b> This [var1] is awaiting approval.  Visitors cannot see it.', $_SESSION['translate']->it('article')).'</span>';
		$article_tags['HITS'] = $this->hits;
		$article_tags['CREATED_DATE'] = date(PHPWS_DATE_FORMAT . ' ' . PHPWS_TIME_FORMAT, (strtotime($this->created_date)+$GLOBALS['core']->datetime->time_dif));
		$article_tags['UPDATED_DATE'] = date(PHPWS_DATE_FORMAT . ' ' . PHPWS_TIME_FORMAT, (strtotime($this->updated_date)+$GLOBALS['core']->datetime->time_dif));
		if(!empty($this->expiration_date))
		  $article_tags['EXPIRATION_DATE'] = date(PHPWS_DATE_FORMAT . ' ' . PHPWS_TIME_FORMAT, (strtotime($this->expiration_date)+$GLOBALS['core']->datetime->time_dif));
		else
		  $article_tags['EXPIRATION_DATE'] = $_SESSION['translate']->it('Never');
		$article_tags['PUBLICATION_DATE'] = date(PHPWS_DATE_FORMAT . ' ' . PHPWS_TIME_FORMAT, (strtotime($this->publication_date)+$GLOBALS['core']->datetime->time_dif));
		$article_tags['VERSION'] = $this->version;

		/* If Username is blank, substitute "Anonymous" */
		if (!$this->created_username) $article_tags['CREATED_USER'] = $_SESSION['translate']->it("Anonymous");
		else $article_tags['CREATED_USER'] = $this->created_username;

		if (!$this->updated_username) $article_tags['UPDATED_USER'] = $_SESSION['translate']->it("Anonymous");
		else $article_tags['UPDATED_USER'] = $this->updated_username;

    /* Create custom metatags */
    if($Fullpage && $_SESSION['SES_ART_master']->val['make_metatags']) 
    {
      $GLOBALS['Layout_title'] = $article_tags['TITLE'] . ' - ' . strip_tags($_SESSION['OBJ_layout']->page_title);
  	  if(isset($article_tags['SUMMARYTEXT']))
  	    $_SESSION['OBJ_layout']->meta_vars['meta_description'] = strip_tags($article_tags['SUMMARYTEXT']);
  	  $_SESSION['OBJ_layout']->meta_vars['meta_author'] = strip_tags($article_tags['CREATED_USER'].' @ '.PHPWS_HOME_HTTP);
  	  $_SESSION['OBJ_layout']->meta_vars['meta_keywords'] = strip_tags($this->meta_keywords);
  	  $_SESSION['OBJ_layout']->meta_vars['meta_robots'] = $this->meta_robots;
    }
		$article_tags['CREATED_TEXT'] = $_SESSION['translate']->it('Created By [var1] on [var2]'
		    , $this->created_username, $article_tags['CREATED_DATE']);
		$article_tags['UPDATED_TEXT'] = $_SESSION['translate']->it('Last updated by [var1] on [var2]'
		    , $this->updated_username, $article_tags['UPDATED_DATE']);

    if ($Fullpage)   /* Full article display */
    {
      $printAll = (isset($_REQUEST['page_num']) && $_REQUEST['page_num']=='all') || isset($_REQUEST['lay_quiet']);
      /* Keep track of any sub-page titles for the NavBar */
      if(!empty($this->sections[reset($this->order)]->title))
        $page_list[] = $this->sections[reset($this->order)]->title;
      else
        $page_list[] = $this->title;
      $page_count = 1;
  		/* Construct article sections */
  		$first_section = TRUE;
  		$article_tags['BODYTEXT'] = '';
  		$page_wanted = (isset($_REQUEST['page_num']))
  			?$_REQUEST['page_num']
  			:1;
  		foreach($this->order as $value)
  		{
  			/* If this section starts the next article, increment the article counter */
  	 		if ($this->sections[$value]->new_page && !$first_section)
  	 		{ 
	 		    $page_list[] = $this->sections[$value]->title;
	 		    $page_count++;
  	 		}
  	 		/* If this section is in the requested page range, print it */
  			if (is_object($this->sections[$value]) 
  			      && ($page_count==$page_wanted || $printAll))
  	 			$article_tags['BODYTEXT'] .= $this->sections[$value]->view_section(0,$this->id
  	 			    ,$this->created_username,$this->created_date,$this->updated_username
  	 			    ,$this->updated_date);
    		$first_section = FALSE;
   		}
  		/* Show multi-article navigation list & bar */
  		if ($this->pages>1 && !$printAll)
  		{
  			$article_tags['NAVLIST'] = $article_tags['NAVBAR'] = '';
  			for ($page_count = 1; ($page_count <= $this->pages); $page_count++)
  			{
  				/* If this is the current article... */
  				if ($page_count == $page_wanted)
  				{
  					$article_tags['NAVLIST'] .= $page_list[$page_count-1].'<br />';
  
  					$article_tags['NAVBAR'] .= (($page_count > 1)? ' | ' : '') . $page_count;
  				/* otherwise, create a article link */
  				} else {
  					$article_tags['NAVLIST'] .= PHPWS_Text::moduleLink($page_list[$page_count-1], 'article'
            ,array('view'=>$this->id, 'page_num'=>$page_count))
  					.'<br />';
  
  					$article_tags['NAVBAR'] .= (($page_count > 1)? " | " : "")
  						. PHPWS_Text::moduleLink($page_count, 'article'
                  ,array('view'=>$this->id, 'page_num'=>$page_count));
  				}
  			}
  			/* "Next" & "Previous" article tags */
  			$spacer = '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
  			if ($page_wanted > 1) 
  			  $article_tags['PREV'] = PHPWS_Text::moduleLink($_SESSION['translate']->it('Previous Page'), 'article'
            ,array('view'=>$this->id, 'page_num'=>($page_wanted-1)));
      	else 
      	  $article_tags['PREV'] = $spacer;
  			if ($page_wanted < $this->pages) 
    			$article_tags['NEXT'] = PHPWS_Text::moduleLink($_SESSION['translate']->it('Next Page'), 'article'
            ,array('view'=>$this->id, 'page_num'=>($page_wanted+1)));
      	else 
      	  $article_tags['NEXT'] = $spacer;
  		}
    }
    else  /* Summary Display */
    {
   		$article_tags['TITLE_LINK'] = PHPWS_Text::moduleLink($this->title, 'article', array('view'=>$this->id));
      if($this->wordcount>0) {
        $article_tags['READMORE'] = '['
            . PHPWS_Text::moduleLink($_SESSION['translate']->it('Read More'), 'article'
              ,array('view'=>$this->id)). ']';
    		$article_tags['MOREWORDS'] = $this->wordcount." ".$_SESSION['translate']->it('more words');
  
    		$article_tags['MOREPAGES'] = $this->pages . ' '
    			.(($this->pages==1)
    				?$_SESSION['translate']->it('page')
    				:$_SESSION['translate']->it('pages'));	
    	}
  		/* Show link to comments */
  		if($_SESSION['SES_ART_master']->val['use_comments'] && $this->allow_comments && isset($_SESSION['PHPWS_CommentManager'])) 
  		{
  			$num_comments=$_SESSION['PHPWS_CommentManager']->numComments('article',$this->id);
  			$article_tags['NUM_COMMENTS']= PHPWS_Text::moduleLink((($num_comments>0)
      					?$num_comments
      					:$_SESSION['translate']->it('Add'))
      				." "
      				.(($num_comments==1)
      					?$_SESSION['translate']->it('Comment')
      					:$_SESSION['translate']->it('Comments'))
      			, 'article'
            , array('view'=>$this->id), null, null, 'comments');
  		}
  	}
		if (!$this->mainarticle) {
		  $s = $_SESSION['translate']->it('Printable Version');
		  $article_tags['PRINT_ICON'] = '<a href="' . $article_link
  			.'&amp;lay_quiet=1" target="_blank" style="text-decoration:none" title="'. $s . '"><img src="http://' . PHPWS_SOURCE_HTTP 
  			. 'mod/article/img/print.gif" border="0" width="22" height="20" alt="'.$s.'" />'.$_SESSION['translate']->it('Print').'</a>';
		  $s = $_SESSION['translate']->it('E-mail this Article to a Friend');
		  $article_tags['EMAIL'] = '<a href="./index.php?module=article&amp;email='.$this->id
  			.'" style="text-decoration:none" title="'.$s.'"><img src="http://'. PHPWS_SOURCE_HTTP 
  			. 'mod/article/img/mailto.png" border="0" width="19" height="12" alt="'.$s.'" />'.$_SESSION['translate']->it('eMail').'</a>';
    }

		/* if user can edit articles or can edit this article... */
 		if ($_SESSION['SES_ART_master']->can('edit',$this->created_username) 
    	    && !($this->mainarticle && !$_SESSION['OBJ_user']->allow_access('approval')))
  	    /* If someone else is editing this page... */
  		if ($this->isLocked($this->editlock, $this->edituser))
  			$article_tags['EDIT'] = '['. $_SESSION['translate']->it('Locked') . ']'; 
  		else
  			$article_tags['EDIT'] = '[<a href="./index.php?module=article&amp;edit='.$this->id 
  								.'">' . $_SESSION['translate']->it('Edit').'</a>]';
		     					
		/* if user can delete articles or can delete this article... */
 		if ($_SESSION['SES_ART_master']->can('delete',$this->created_username))
	     $article_tags['DELETE'] = '[<a href="./index.php?module=article&amp;edit=delete&amp;ARTICLE_id='
	          . $this->id.'">'.$_SESSION['translate']->it('Delete').' </a>]';

		/* Show FatCat Category icons */
		if(isset($_SESSION['OBJ_fatcat']) 
		      && is_array($icons = $_SESSION['OBJ_fatcat']->getIcon($this->id, TRUE, TRUE, 'article')))
			$article_tags['CATEGORY'] = implode(' ', $icons); 

		/* Now apply the template. */
		if (!$Fullpage)
		{
	    /* Show it in summary format */
  		if ($returnHTML)
   			return PHPWS_Template::processTemplate($article_tags, 'article', 'summary_view.tpl');
   		else
   			$GLOBALS['CNT_article_summaries']['content'] 
 			      .= PHPWS_Template::processTemplate($article_tags, 'article', 'summary_view.tpl');
		}
		elseif ($returnHTML)
		{
		  $article_tags['PRINT_ICON'] = $article_tags['EMAIL'] = $article_tags['EDIT'] = $article_tags['DELETE'] = NULL;
 			return PHPWS_Template::processTemplate($article_tags
 			      , 'article', 'article/'.$this->template.'/title.tpl')
 			  .PHPWS_Template::processTemplate($article_tags
 			      , 'article', 'article/'.$this->template.'/content.tpl');
		}
		else 
		{
	    /* Show it in full page format */
 			$GLOBALS['CNT_article']['title'] = PHPWS_Template::processTemplate($article_tags
 			      , 'article', 'article/'.$this->template.'/title.tpl');
 			$GLOBALS['CNT_article']['content'] = PHPWS_Template::processTemplate($article_tags
 			      , 'article', 'article/'.$this->template.'/content.tpl');
 			if ($GLOBALS['module'] != "home")	$GLOBALS['CNT_article']['content'] .= $copyright;

  	 	/* If we need a printable version... */
      if (isset($_REQUEST['lay_quiet']))
      {
        if ($_SESSION['SES_ART_master']->val['brand_printable'])
          $_SESSION['OBJ_layout']->displayPrintTheme('CNT_article');
  	 	  else
  	 	    echo '<h2>'.$GLOBALS['CNT_article']['title'].'</h2>'.$GLOBALS['CNT_article']['content'];
  	 	  return;
  	 	}

  	 	/* Article Comments */
  		if($_SESSION['SES_ART_master']->val['use_comments'] && $this->allow_comments && !$this->mainarticle && isset($_SESSION['PHPWS_CommentManager'])) 
		    $_SESSION['PHPWS_CommentManager']->listCurrentComments('article', $this->id, $this->allow_anonymous);

  		/* FatCat "What's Related" */
  		if($_SESSION['OBJ_fatcat'])
  			$_SESSION['OBJ_fatcat']->whatsRelated($this->id,'article'); 
   	}
	}


	/**
	* Displays this article in a format that allows the user to make edits to the content. SECT_index
	* id provided to specify editing of a specific section within this article.
	*
	* @author Eloi George <eloi@NOSPAM.bygeorgeware.com>
	* @module Article Manager
	* @param int SECT_id : Database id of the section within this article to be edited.
	* @return none
	* @access private
	*/
	function edit_article ($SECT_id = NULL)
	{
		include(PHPWS_SOURCE_DIR . 'mod/article/inc/A_edit.php');
	}

	/**
	* Updates this article's information in memory
	*
  * @author Eloi George <eloi@NOSPAM.bygeorgeware.com>
	* @module Article Manager
	* @param none
	* @return none
	*/
	function update_title ()
	{
    $this->updated_username = $_SESSION['OBJ_user']->username;
    $this->updated_date = date("Y-m-d H:i:s");
    if (isset($_POST['ARTICLE_title']))
		  $this->title = stripslashes($_POST['ARTICLE_title']);
    if (isset($_POST['ARTICLE_summary']))
		  $this->summary = stripslashes($_POST['ARTICLE_summary']);
	}

	/**
	* Updates this article's information in memory
	*
  * @author Eloi George <eloi@NOSPAM.bygeorgeware.com>
	* @module Article Manager
	* @param none
	* @return none
	*/
	function update_settings ()
	{
		include(PHPWS_SOURCE_DIR . 'mod/article/inc/A_update.php');
	}

	/**
	* Saves this article to the database.
	*
	* @author Adam Morton <adam@NOSPAM.tux.appstate.edu>
	* @module Article Manager
	* @param none
	* @return On success, id of article; on failure, FALSE
	*
	*/
	function save ()
	{
		return include(PHPWS_SOURCE_DIR . 'mod/article/inc/A_save.php');
	}

	/**
	* Deletes this article and all associated sections from the database. Images remain untouched
	* and should be manually removed if you are sure no other article is using them.
	*
	* @author Adam Morton <adam@NOSPAM.tux.appstate.edu>
  * @author Eloi George <eloi@NOSPAM.bygeorgeware.com>
	* @module Article Manager
	* @param none
	* @return none
	*/
	function delete_article ()
	{
		include(PHPWS_SOURCE_DIR . 'mod/article/inc/A_delete.php');
	}

	/**
	* Adds a section to the sections array and order array in this article.
	*
  * @author Eloi George <eloi@NOSPAM.bygeorgeware.com>
	* @module Article Manager
	* @param none
	* @return none
	*/
	function add_section ()
	{
		$this->sections[$_SESSION['SES_ART_section']->id] = $_SESSION['SES_ART_section'];
		$this->sections[$_SESSION['SES_ART_section']->id]->edited = 1;
		$this->order[] = $_SESSION['SES_ART_section']->id;
	}

	/**
	* Updates a section's data in the sections array of this article.  SECT_id is the id
	* of the section to be updated
	*
	* @author Adam Morton <adam@NOSPAM.tux.appstate.edu>
  * @author Eloi George <eloi@NOSPAM.bygeorgeware.com>
	* @module Article Manager
	* @param int SECT_id : Database id of the section to be updated.
	* @return none
	*/
	function update_section ($SECT_id)
	{
		$this->sections[$SECT_id] = $_SESSION['SES_ART_section'];
		$this->sections[$SECT_id]->edited = 1;
	}

	/**
	* Moves a section up or down in this article. SECT_id = database id of section to be
	* moved. direction = direction to move section, "up" or "down".
	*
  * @author Eloi George <eloi@NOSPAM.bygeorgeware.com>
	* @module Article Manager
    * @param  bool   $moveup  : Direction to move. Defaults to moving up (true).
	* @param string direction : Direction to move section ("up" or "down")
	* @return none
	*/
	function move_section ($SECT_id, $moveup=true)
	{
    $this->order = PHPWS_Array::moveElement($this->order, NULL, $SECT_id, $moveup, false);
	}

	/**
	* Removes the section at SECT_id from this article.
	*
  * @author Eloi George <eloi@NOSPAM.bygeorgeware.com>
	* @module Article Manager
	* @param int SECT_id : Database id of the section to be removed.
	* @return none
	*/
	function remove_section ($SECT_id)
	{
		include(PHPWS_SOURCE_DIR . 'mod/article/inc/A_remove_section.php');
	}

	/**
	* Immediately expires this article, hiding it from users.
	*
	* @author Eloi George <eloi@NOSPAM.bygeorgeware.com>
	* @module Article Manager
	* @param none
	* @return none
	*/
	function expire ()
	{
		/* Setup the query data for the current article.  Update info. */
		$query_data['expiration_date'] = date('Y-m-d H:i:s');
		$GLOBALS['core']->sqlUpdate($query_data, 'mod_article', 'id', $this->id);
    PHPWS_Fatcat::deactivate($this->id, 'article');
		$this->expiration_date = $query_data['expiration_date'];
	}

	/**
	* Immediately publishes this article so users can see it.
	*
	* @author Eloi George <eloi@NOSPAM.bygeorgeware.com>
	* @module Article Manager
	* @param none
	* @return none
	*/
	function publish ()
	{
		include(PHPWS_SOURCE_DIR . 'mod/article/inc/A_publish.php');
	}


	/**
	* Toggles the mainarticle flag for this article, either using this article on the main/home article
	* or removing it from the main/home article.
	*
	* This function will only work if this is the current mainpage or a viewable article.
	*
  * @author Eloi George <eloi@NOSPAM.bygeorgeware.com>
	* @module Article Manager
	* @param none
	* @return none
	*/
	function toggle_mainarticle ()
	{
		$now = date("Y-m-d H:i:s");
		if ($this->mainarticle == 1 
        || ($this->publication_date <= $now 
			&& $this->expiration_date > $now))
		{
      PHPWS_WizardBag::toggle($this->mainarticle);
	    $this->update();
		}
	}

	/**
	* Increments the article counter by one to keep track of article accesses
	*
  * @author Eloi George <eloi@NOSPAM.bygeorgeware.com>
	* @module Article Manager
	* @param none
	* @return none
	*/
	function increment_counter($id=null) 
	{
		if (!$id)
		  $id = $this->id;
		$this->hits++;
    $GLOBALS['core']->query('UPDATE mod_article SET hits=hits+1 WHERE id='.$id, true);
	}

	/**
	* Sets/Removes a time-sensitive edit lock on this article.
	*
	* This function helps prevent the possibility of concurrent edits.  
	*
  * @author Eloi George <eloi@NOSPAM.bygeorgeware.com>
	* @module Article Manager
	* @param int action true=set false=remove the lock.
	* @return none
	*/
    function setEditLock ($action=true)
    {
      if ($this->id)
      {
        if ($action)
        {
          $query_data['editlock'] = time();
          $query_data['edituser']= $_SESSION['OBJ_user']->user_id;
        }
        else 
          $query_data['editlock'] = 0;
    		$GLOBALS['core']->sqlUpdate($query_data, "mod_article", 'id', $this->id);
    		$this->editlock = $query_data['editlock'];
    	}
    }
    
    
	/**
	* Checks to see if someone else is currently editing this page
	*
	* This function prevents the possibility of concurrent edits.  
	* If a lock is found that hasn't expired, you're presented with an error message. 
	*
  * @author Eloi George <eloi@NOSPAM.bygeorgeware.com>
	* @module Article Manager
	* @param int lock : Unix time that the lock was created. 
	* @param int ownerid : Id of the user that created the lock. 
	* @return FALSE or number of seconds before the lock expires.
	*/
    function isLocked ($lock=0, $ownerid=0)
    {
      /* Calculate time remaining */
      $timeleft = $lock + $_SESSION['SES_ART_master']->val['lock_expiration_time'] - time();
      /* If there is a lock in place... */
      if ($timeleft > 0)
        /* If it doesn't belong to the user... */
        if (!$_SESSION['OBJ_user']->user_id || $_SESSION['OBJ_user']->user_id!=$ownerid)
          return $timeleft;
	    return FALSE;
    }

	/**
	* Displays this article's row in a fatCat category display
	*
  * @author Eloi George <eloi@NOSPAM.bygeorgeware.com>
	* @module Article Manager
	* @param id : Id of the article to show. 
	*/
  function fatCatview ($id)
  {
    $this = new PHPWS_Article($id);
    return $this->view(false, true);
  }


	/**
	* Tests to see if this article can be viewed
	*
  * @author Eloi George <eloi@NOSPAM.bygeorgeware.com>
	* @module Article Manager
	* @param bool saving : Whether this is being called by function save() or not. 
	*/
  function is_viewable ($saving = false)
  {
		$now = date('Y-m-d H:i:s');
		return ($this->mainarticle 
	        || ($this->approved 
				    && $this->publication_date <= $now 
				    && ($this->expiration_date > $now || $this->expiration_date==NULL))
			    || ($_SESSION['SES_ART_master']->can('edit',$this->created_username)
			      && $GLOBALS['module']=='article'
			      && !$saving) 
			    || $GLOBALS['module']=='approval');
  }

	/**
	* Emails this article to a friend.
	*
	* @author Eloi George <eloi@NOSPAM.bygeorgeware.com>
	* @module Article Manager
	* @param id : Id of the article to show. 
	* @return none
	*/
	function mail ()
	{
  	include(PHPWS_SOURCE_DIR . 'mod/article/inc/A_mail.php');
	}

	/**
	* Prepares the article for serialization & archival storage
	*
	* @author Eloi George <eloi@NOSPAM.bygeorgeware.com>
	* @module Article Manager
	* @param none 
	* @return none
	*/
	function _sleep ()
	{
  	unset($this->oldversion);
  	return $this;
	}

	/**
	* Displays the revision history of this article.
	*
	* @author Eloi George <eloi@NOSPAM.bygeorgeware.com>
	* @module Article Manager
	* @param none 
	* @return none
	*/
	function view_history ()
	{
  	include(PHPWS_SOURCE_DIR . 'mod/article/inc/A_view_history.php');
	}

	/**
	* Displays differences between 2 versions of this article.
	*
	* @author Eloi George <eloi@NOSPAM.bygeorgeware.com>
	* @module Article Manager
	* @param int oldversion : Version # of the older revision. 
	* @param int newversion : Version # of the newer revision. 
	* @return none
	*/
	function view_diff ($oldversion, $newversion)
	{
  	include(PHPWS_SOURCE_DIR . 'mod/article/inc/A_view_diff.php');
	}
}  
?>