<?php // no direct access
defined('_JEXEC') or die('Restricted access');

$config =& JFactory::getConfig();
$publish_up =& JFactory::getDate($this->article->publish_up);
$publish_up->setOffset($config->getValue('config.offset'));
$publish_up = $publish_up->toFormat();
if (! isset($this->article->publish_down) || $this->article->publish_down == 'Never') {
	$publish_down = JText::_('Never');
} else {
	$publish_down =& JFactory::getDate($this->article->publish_down);
	$publish_down->setOffset($config->getValue('config.offset'));
	$publish_down = $publish_down->toFormat();
}
?>
<?php
		//gmaccess ajout
		
		$db		= & JFactory::getDBO();
		$user	= & JFactory::getUser();
		$access					= new stdClass();
		$access->canPublish		= $user->authorize('com_content', 'publish', 'content', 'all');
		$access->canPublishOwn	= $user->authorize('com_content', 'publishown', 'content', 'all');
		$access->canRight		= $user->authorize('com_content', 'right', 'content', 'all');
		$access->canFront		= $user->authorize('com_frontpage', 'manage');
		$publishgm = fonctionGmaccess::checkallaccess ('com_content', 'publish', 'users',$user->gmid, $this->article->id);
		$publishowngm = fonctionGmaccess::checkallaccess ('com_content', 'publishown', 'users',$user->gmid, $this->article->id);
		$rightgm = fonctionGmaccess::checkallaccess ('com_content', 'right', 'users',$user->gmid, $this->article->id);
		
			if ( $this->article->created_by == $user->id ) {
				$and = "\n AND (aco = 'publish' OR aco = 'publishown')";
			}else{
				$and = "\n AND aco = 'publish'";
			}
			
			$checkfinal = array (0);
			$query = "SELECT axo"
			. "\n FROM #__gmacl"
			. "\n WHERE acosection = 'com_content'"
			. $and
			. "\n AND arosection='users'"
			. "\n AND aro IN (".$user->gmid.")"
			. "\n AND axosection = 'category'"
			;
			$db->setQuery( $query );
			$check = $db->loadResultArray();
			if ( is_array( $check ) && count( $check ) > 0 ){
				$checkfinal = array_merge ($checkfinal,$check);
			}
			$checkfinal = array_unique ($checkfinal);
			$listecat= implode (",",$checkfinal);

			$checkfinal2 = array (0);
			$query = "SELECT axo"
			. "\n FROM #__gmacl"
			. "\n WHERE acosection = 'com_content'"
			. $and
			. "\n AND arosection='users'"
			. "\n AND aro IN (".$user->gmid.")"
			. "\n AND axosection = 'section'"
			;
			$db->setQuery( $query );
			$check2 = $db->loadResultArray();
			if ( is_array( $check2 ) && count( $check2 ) > 0 ){
				$checkfinal2 = array_merge ($checkfinal2,$check2);
			}
			$checkfinal2 = array_unique ($checkfinal2);
			$listesec= implode (",",$checkfinal2);
			
			$checkfinal = array (0);
			$query = "SELECT axo"
			. "\n FROM #__gmacl"
			. "\n WHERE acosection = 'com_content'"
			. "\n AND aco = 'right'"
			. "\n AND arosection='users'"
			. "\n AND aro IN (".$user->gmid.")"
			. "\n AND axosection = 'category'"
			;
			$db->setQuery( $query );
			$check = $db->loadResultArray();
			if ( is_array( $check ) && count( $check ) > 0 ){
				$checkfinal = array_merge ($checkfinal,$check);
			}
			$checkfinal = array_unique ($checkfinal);
			$listecat2= implode (",",$checkfinal);

			$checkfinal2 = array (0);
			$query = "SELECT axo"
			. "\n FROM #__gmacl"
			. "\n WHERE acosection = 'com_content'"
			. "\n AND aco = 'right'"
			. "\n AND arosection='users'"
			. "\n AND aro IN (".$user->gmid.")"
			. "\n AND axosection = 'section'"
			;
			$db->setQuery( $query );
			$check2 = $db->loadResultArray();
			if ( is_array( $check2 ) && count( $check2 ) > 0 ){
				$checkfinal2 = array_merge ($checkfinal2,$check2);
			}
			$checkfinal2 = array_unique ($checkfinal2);
			$listesec2= implode (",",$checkfinal2);
			
			if (( $access->canPublishOwn && $this->article->created_by == $user->id ) || $access->canPublish) {
			
			$query = "SELECT id "
			. "\n FROM #__categories"
			. "\n WHERE published = 1"
			. "\n AND section NOT LIKE 'com_%'"
			
			;
			$query2 = "SELECT count(*)"
			. "\n FROM #__categories"
			. "\n WHERE published = 1"
			. "\n AND section NOT LIKE 'com_%'"
			;
			}else{
				
			
					$query = "SELECT id"
					. "\n FROM #__categories"
					. "\n WHERE published = 1"
					. "\n AND section NOT LIKE 'com_%'"
					. "\n AND (id IN (" . $listecat . ") OR section IN (" . $listesec . "))"
			
					;
					$query2 = "SELECT count(*)"
					. "\n FROM #__categories"
					. "\n WHERE published = 1"
					. "\n AND section NOT LIKE 'com_%'"
					. "\n AND (id IN (" . $listecat . ") OR section IN (" . $listesec . "))"
					;

			}
			$mycat = array (0);
			$db->setQuery( $query );
			$mycatpublish = $db->loadResultArray();
			if ( is_array( $mycatpublish ) && count( $mycatpublish ) > 0 ){
				$mycatpublish = array_merge ($mycatpublish,$mycat);
			} else {
				$mycatpublish = array (0);
			}
			$db->setQuery( $query2 );
			$nbpublish = $db->loadResult();

			
			
			if ($access->canRight) {
			
			$query = "SELECT id "
			. "\n FROM #__categories"
			. "\n WHERE published = 1"
			. "\n AND section NOT LIKE 'com_%'"
			
			;
			$query2 = "SELECT count(*)"
			. "\n FROM #__categories"
			. "\n WHERE published = 1"
			. "\n AND section NOT LIKE 'com_%'"
			;
			}else{
				
			
					$query = "SELECT id"
					. "\n FROM #__categories"
					. "\n WHERE published = 1"
					. "\n AND section NOT LIKE 'com_%'"
					. "\n AND (id IN (" . $listecat2 . ") OR section IN (" . $listesec2 . "))"
			
					;
					$query2 = "SELECT count(*)"
					. "\n FROM #__categories"
					. "\n WHERE published = 1"
					. "\n AND section NOT LIKE 'com_%'"
					. "\n AND (id IN (" . $listecat2 . ") OR section IN (" . $listesec2 . "))"
					;

			}
			$mycat2 = array (0);
			$db->setQuery( $query );
			$mycatright = $db->loadResultArray();
			if ( is_array( $mycatright ) && count( $mycatright ) > 0 ){
				$mycatright = array_merge ($mycatright,$mycat2);
			} else {
				$mycatright = array (0);
			}
			$db->setQuery( $query2 );
			$nbright = $db->loadResult();
			$test  = ($access->canPublishOwn  && $this->article->created_by == $user->id) ? 1 : 0;
			$test2  = $access->canPublish ? 1 : 0;
			$test3  = $access->canRight ? 1 : 0;

			if ( $access->canPublish || ($access->canPublishOwn  && $this->article->created_by == $user->id) || ($publishowngm  && $this->article->created_by == $user->id) || $publishgm ) {
					$affichepub = "block";
			} else {
					$affichepub = "none";
			}
			if ( $access->canRight  || $rightgm ) {
					$afficheri = "block";
			} else {
					$afficheri = "none";
			}
			if ( $access->canFront) {
					$affichefront = "block";
			} else {
					$affichefront = "none";
			}
			//fingmaccess
?>

<script language="javascript" type="text/javascript">
<!--
function setgood() {
	// TODO: Put setGood back
	return true;
}

var sectioncategories = new Array;
<?php
$i = 0;
foreach ($this->lists['sectioncategories'] as $k=>$items) {
	foreach ($items as $v) {
		echo "sectioncategories[".$i++."] = new Array( '$k','".addslashes( $v->id )."','".addslashes( $v->title )."' );\n\t\t";
	}
}
?>


function submitbutton(pressbutton) {
	var form = document.adminForm;
	if (pressbutton == 'cancel') {
		submitform( pressbutton );
		return;
	}
	try {
		form.onsubmit();
	} catch(e) {
		alert(e);
	}

	// do field validation
	var text = <?php echo $this->editor->getContent( 'text' ); ?>
	if (form.title.value == '') {
		return alert ( "<?php echo JText::_( 'Article must have a title', true ); ?>" );
	} else if (text == '') {
		return alert ( "<?php echo JText::_( 'Article must have some text', true ); ?>");
	//ajout gmacces
	} else if (form.catid.value == "-1"){
		return alert( "<?php echo JText::_( 'You must select a Category', true ); ?>" );
	} else if (form.catid.value == ""){
		return alert( "<?php echo JText::_( 'You must select a Category', true ); ?>" );
	//fin gmaccess
	} else if (parseInt('<?php echo $this->article->sectionid;?>')) {
		// for articles
		if (form.catid && getSelectedValue('adminForm','catid') < 1) {
			return alert ( "<?php echo JText::_( 'Please select a category', true ); ?>" );
		}
	}
	<?php echo $this->editor->save( 'text' ); ?>
	submitform(pressbutton);
}
//ajout gmaccess
		function gmscript9(element, element2) {
			var xhr_object = null; 
			var cat = element.value;
			var id = element2.value;
			var url="index.php?option=com_gmajax&tmpl=component&cati="+cat+"&id="+id; 
			
 			if(window.XMLHttpRequest) // Firefox   
   				 xhr_object = new XMLHttpRequest();   
 			else if(window.ActiveXObject) // Internet Explorer   
   				 xhr_object = new ActiveXObject("Microsoft.XMLHTTP");   
			 else { // XMLHttpRequest non supporté par le navigateur   
   				 alert("Votre navigateur ne supporte pas les objets XMLHTTPRequest...");   
   			 return;   
 			}   
   
 			xhr_object.open("GET", url, true);
			xhr_object.onreadystatechange = function() {
			if(xhr_object.readyState == 1) {

					document.getElementById("access").innerHTML = "Chargement en cours";
			}   
   			if(xhr_object.readyState == 4) document.getElementById("access").innerHTML = xhr_object.responseText; 
			}
				   
 			xhr_object.send(null); 
			
			//gmaccess suite du script à prendre pour la gestion des droits
			if ((<?php echo $nbpublish;?>>0) && (<?php echo $test;?>==0) && (<?php echo $test2;?>==0)) {
				var montre = 0;
				<?php
     			 $js = fonctionGmaccess::conv_gma($mycatpublish,"id");
				 echo $js;//où id est le nom de mon tableau javascript
 	 			?>
				
				for (var i=0; i < <?php echo $nbpublish;?>; i++) {
						if (id[i] == element.options[element.selectedIndex].value) {
							
							montre = 1;
						}
				}
				
				if (montre == 1) {
					document.getElementById("publishgm").style.display = "block";
					
				}
				else
				{
					document.getElementById("publishgm").style.display = "none";
			
				}
			} 
			else if ((<?php echo $test;?>!=0) || (<?php echo $test2;?>!=0) ) 
			{
			document.getElementById("publishgm").style.display = "block";
	
			}
			else
			{
			document.getElementById("publishgm").style.display = "none";
		
			}  
			if ((<?php echo $nbright;?>>0) && (<?php echo $test3;?>==0)) {
				var montre = 0;
				<?php
     			 $js = fonctionGmaccess::conv_gma($mycatright,"id");
				 echo $js;//où id est le nom de mon tableau javascript
 	 			?>
				
				for (var i=0; i < <?php echo $nbright;?>; i++) {
						if (id[i] == element.options[element.selectedIndex].value) {
							
							montre = 1;
						}
				}
				
				if (montre == 1) {
					document.getElementById("rightgm").style.display = "block";
					
				}
				else
				{
					document.getElementById("rightgm").style.display = "none";
					
				}
			} 
			else if (<?php echo $test3;?>!=0) 
			{
			document.getElementById("rightgm").style.display = "block";
			
			}
			else
			{
			document.getElementById("rightgm").style.display = "none";
			
			}  
		}
	//fin gmaccess
//-->
</script>
<?php if ($this->params->get('show_page_title', 1)) : ?>
<div class="componentheading<?php echo $this->escape($this->params->get('pageclass_sfx')); ?>"><?php echo $this->escape($this->params->get('page_title')); ?></div>
<?php endif; ?>
<form action="<?php echo $this->action ?>" method="post" name="adminForm" onSubmit="setgood();">
<fieldset>
<legend><?php echo JText::_('Editor'); ?></legend>
<table class="adminform" width="100%">
<tr>
	<td>
		<div style="float: left;">
			<label for="title">
				<?php echo JText::_( 'Title' ); ?>:
			</label>
			<input class="inputbox" type="text" id="title" name="title" size="50" maxlength="100" value="<?php echo $this->escape($this->article->title); ?>" />
			<input class="inputbox" type="hidden" id="alias" name="alias" value="<?php echo $this->escape($this->article->alias); ?>" />
		</div>
		<div style="float: right;">
			<button type="button" onclick="submitbutton('save')">
				<?php echo JText::_('Save') ?>
			</button>
			<button type="button" onclick="submitbutton('cancel')">
				<?php echo JText::_('Cancel') ?>
			</button>
		</div>
	</td>
</tr>
</table>

<?php
echo $this->editor->display('text', $this->article->text, '100%', '400', '70', '15');
?>
</fieldset>
<fieldset>
<legend><?php echo JText::_('Publishing'); ?></legend>
<table class="adminform">
<tr>
	<td class="key">
		<label for="sectionid">
			<?php echo JText::_( 'Section' ); ?>:
		</label>
	</td>
	<td>
		<?php echo $this->lists['sectionid']; ?>
	</td>
</tr>
<tr>
	<td class="key">
		<label for="catid">
			<?php echo JText::_( 'Category' ); ?>:
		</label>
	</td>
	<td>
		<?php echo $this->lists['catid']; ?>
	</td>
</tr>
<?php //if ($this->user->authorize('com_content', 'publish', 'content', 'all')) :
//modif gmaccess ?>
<tr id="publishgm" style="display:<?php echo $affichepub; ?>">
	<td class="key">
		<label for="state">
			<?php echo JText::_( 'Published' ); ?>:
		</label>
	</td>
	<td>
		<?php echo $this->lists['state']; ?>
	</td>
</tr>
<?php //endif;
//modif gmaccess ?>
<tr style="display:<?php echo $affichefront; //ajout gmaccess?>">
	<td width="120" class="key">
		<label for="frontpage">
			<?php echo JText::_( 'Show on Front Page' ); ?>:
		</label>
	</td>
	<td>
		<?php echo $this->lists['frontpage']; ?>
	</td>
</tr>
<tr>
	<td class="key">
		<label for="created_by_alias">
			<?php echo JText::_( 'Author Alias' ); ?>:
		</label>
	</td>
	<td>
		<input type="text" id="created_by_alias" name="created_by_alias" size="50" maxlength="100" value="<?php echo $this->escape($this->article->created_by_alias); ?>" class="inputbox" />
	</td>
</tr>
<tr>
	<td class="key">
		<label for="publish_up">
			<?php echo JText::_( 'Start Publishing' ); ?>:
		</label>
	</td>
	<td>
	    <?php echo JHTML::_('calendar', $publish_up, 'publish_up', 'publish_up', '%Y-%m-%d %H:%M:%S', array('class'=>'inputbox', 'size'=>'25',  'maxlength'=>'19')); ?>
	</td>
</tr>
<tr>
	<td class="key">
		<label for="publish_down">
			<?php echo JText::_( 'Finish Publishing' ); ?>:
		</label>
	</td>
	<td>
	    <?php echo JHTML::_('calendar', $publish_down, 'publish_down', 'publish_down', '%Y-%m-%d %H:%M:%S', array('class'=>'inputbox', 'size'=>'25',  'maxlength'=>'19')); ?>
	</td>
</tr>
<?php /*
<tr>
	<td valign="top" class="key">
		<label for="access">
			<?php echo JText::_( 'Access Level' ); ?>:
		</label>
	</td>
	<td>
		<?php echo $this->lists['access']; ?>
	</td>
</tr>
*/ ?>
<tr>
	<td class="key">
		<label for="ordering">
			<?php echo JText::_( 'Ordering' ); ?>:
		</label>
	</td>
	<td>
		<?php echo $this->lists['ordering']; ?>
	</td>
</tr>
</table>
</fieldset>
<!--ajout gmaccess -->
            
			<fieldset id="rightgm" style="display:<?php echo $afficheri; ?>">
                    <legend><div id="plusgm" style="vertical-align:middle; display:block" ><a onclick="openaccessgma();"><IMG  alt="" src="<?php echo JURI::root(); ?>administrator/images/expandall.png" /> <?php echo JText::_( 'Access Level' ); ?></a></div>
                    <div id="moinsgm" style="vertical-align:middle; display:none"><a onclick="closeaccessgma();"><IMG  alt="" src="<?php echo JURI::root(); ?>administrator/images/collapseall.png" /> <?php echo JText::_( 'Access Level' ); ?></a></div></legend>
    				
                    <table class="adminform" width="100%" >
                    <tr>
                    	<td id="tabacces" style="display:none">
                        	<div id="access"><?php echo $this->lists['access']; ?></div>
                        </td>
                        <td id="tabaccesf" style="display:block">
                        	<?php echo JText::_( 'GMA_FIELDTEXT' ); ?>
                        </td>
                    </tr>
                    </table>
            </fieldset>
           
			<!--fin gmaccess -->
<fieldset>
<legend><?php echo JText::_('Metadata'); ?></legend>
<table class="adminform">
<tr>
	<td valign="top" class="key">
		<label for="metadesc">
			<?php echo JText::_( 'Description' ); ?>:
		</label>
	</td>
	<td>
		<textarea rows="5" cols="50" style="width:500px; height:120px" class="inputbox" id="metadesc" name="metadesc"><?php echo str_replace('&','&amp;',$this->article->metadesc); ?></textarea>
	</td>
</tr>
<tr>
	<td  valign="top" class="key">
		<label for="metakey">
			<?php echo JText::_( 'Keywords' ); ?>:
		</label>
	</td>
	<td>
		<textarea rows="5" cols="50" style="width:500px; height:50px" class="inputbox" id="metakey" name="metakey"><?php echo str_replace('&','&amp;',$this->article->metakey); ?></textarea>
	</td>
</tr>
</table>
</fieldset>

<input type="hidden" name="option" value="com_content" />
<input type="hidden" name="id" value="<?php echo $this->article->id; ?>" />
<input type="hidden" name="version" value="<?php echo $this->article->version; ?>" />
<input type="hidden" name="created_by" value="<?php echo $this->article->created_by; ?>" />
<input type="hidden" name="referer" value="<?php echo str_replace(array('"', '<', '>', "'"), '', @$_SERVER['HTTP_REFERER']); ?>" />
<?php echo JHTML::_( 'form.token' ); ?>
<input type="hidden" name="task" value="" />
</form>
<?php echo JHTML::_('behavior.keepalive'); ?>
