User:Hummel-riegel/SRF Process/SRF SpecialCreateProcess.i18n.php

<?php

global $wgSpecialPages;
$wgSpecialPages['SRF_SpecialCreateProcess'] = 'SRF_SpecialCreateProcess';

$wgExtensionMessagesFiles['SRF_SpecialCreateProcess'] = dirname(__FILE__) . '/SRF_SpecialCreateProcess.i18n.php';


class SRF_SpecialCreateProcess extends SpecialPage {
	
	protected $_title 	= '';
	protected $_content = '';
	
	public function __construct() {
		parent::__construct('SRF_SpecialCreateProcess', '', true);
		wfLoadExtensionMessages('SRF_SpecialCreateProcess');
		
	}

	// TODO MWUtil?
	public static function articleExists($titleString){
		$res = false;
		
		$title = new Title();
		$tpage = $title->newFromDBkey($titleString);
		if (isset($tpage)){
			$res = $tpage->exists();
		}
		
		return $res;
		
	}
	
	// Here the inline output of the Special page will be created
	function execute($par) {
		global $wgOut;
		
		$this->setHeaders();
		$returntitle = Title::makeTitle(NS_SPECIAL, 'SRF_SpecialCreateProcess');
		
		$errors = array();
		$steps = array();
		$ppage;
		
		if( $par == 'send' ) { 
			
			if(strlen($_POST['content']) > 0) {
			
				$steps = explode("\n", $_POST['content']);
				
				foreach ($steps as $step){
					if (self::articleExists($step)) $errors['content'] = 'Page ' . $step . ' exists already';
				}
				
			} else {
				$errors['content'] = 'No content specified';
			}
			
			if(strlen($_POST['title']) > 0) {
				$ppage = trim($_POST['title']);
				if (self::articleExists($ppage)) $errors['content'] = 'Title page ' . $ppage . ' exists already';
			} else {
				$errors['title'] = 'No title specified';
			}
			
			
			
			if( sizeof($errors) == 0 ) {
				
  				// create steps
  				$count = count($steps);
				for ($i = 0; $i < $count; $i++) {

  					$step = trim($steps[$i]);
  					
  					$scontent = '
  					{{Process Step
					|Belongs to process=' . $ppage;
  					
  					if ( $i < ($count -1) ){ 
  						$scontent .= '|has Successor=' . trim($steps[$i+1]);
  					}
  					
					$scontent .= '}}';
  					
  					$sArticle = new Article( Title::newFromText($step) );
  					$sArticle->doEdit($scontent, 'SpecialCreateProcess', 'EDIT_NEW');//, EDIT_UPDATE);
  					
  					//$wgOut->addHTML( '<pre>' . $scontent . '</pre>');
  				}
				
  				// create process
				$pcontent = '{{Process}}'; // TODO add owner				
				$pArticle = new Article( Title::newFromText($ppage) );
				// http://svn.wikimedia.org/doc/classArticle.html#44882bb1533f07bf67baca3e65821bb3
  				$pArticle->doEdit($pcontent, 'SpecialCreateProcess', 'EDIT_NEW');//, EDIT_UPDATE);
				
  				
  				$wgOut->addWikiText(wfMsg('srf_specialcreateprocess-processsucc', $ppage));
  				
				return;
			} 
				
				/*
				$t_go 		= new Title();
				$togo_page 	= $this->searchString;
				$tt_go 		= $t_go->newFromDBkey($togo_page);
				
				// check if there is an article matching the value of the "go" parameter
				if (isset($tt_go) && $tt_go->exists())
				*/
				
				/*
				 $article->getContent()
				 // save (and update history)
				$article->doEdit($newContent, 'validated'); 
				 
				   $mArticle = new Article( $rev->getTitle()  );
  					$tmp = $mArticle->doEdit($newPageText, '', EDIT_UPDATE);
				 */
				
				/*
				 
				  {{Process Step
				|Belongs to process=Projektstrukturplan Beispiel
				|has Successor=Beispielschritt 2, Beispielschritt 3
}}
				 
				 */
				
		
		}
		
		include 'helpers/process_form.php';
		
		$wgOut->addHTML( $html);
		
	}
	
	
}
	

?>