Location: PHPKode > projects > PhpScribe Documentation Generator > projects.php
<?php
set_time_limit(0);
ignore_user_abort(TRUE);

require_once("config/scribe.inc");
require_once(PHPSCRIBE_CLASS_HTML);
require_once(PHPSCRIBE_CLASS_FILEDOCPARSER);
require_once(PHPSCRIBE_CLASS_DOCGENERATOR);
require_once(PHPSCRIBE_CLASS_FORMTEMPLATE);
require_once(PHPSCRIBE_CLASS_REPORT);

$HTML = new Html();
$HTML->addStyle("phpscribe.css");
$HTML->addScript("login.js");
$HTML->addBodyCfg("bgColor='#d6d6d6'");
$HTML->makePage();
$HTML->header->setAndPrepare("header.tpl");
$HTML->header->replace(array("imagePath"=>PHPSCRIBE_IMAGE_PATH,
                             "localPath"=>PHPSCRIBE_ABSOLUTE_PATH,
                             "pageTitle"=>"Projects and Documentation"));
$HTML->footer->setAndPrepare("footer.tpl");
$HTML->footer->replace(array("imagePath"=>PHPSCRIBE_IMAGE_PATH,
                             "localPath"=>PHPSCRIBE_ABSOLUTE_PATH));
$HTML->printPage();

function Main() {
     if (($_GET['pageAction'] == "generate") && (!empty($_POST))) {
          GenerateDocumentation($GLOBALS['message']);
     } elseif (!empty($_POST)) {
          SaveData($_POST,$GLOBALS['message']);
     } elseif (isset($_GET['delete'])) {
          DeleteData($_GET['delete'],$GLOBALS['message']);
     }
     $MAIN_TPL = new Template(PHPSCRIBE_TEMPLATE_PATH."main.tpl");
     $MAIN_TPL->prepare();
     $MAIN_TPL->assign("mainContent",GetActContent());
     $MAIN_TPL->assign("imagePath",PHPSCRIBE_IMAGE_PATH);
     $MAIN_TPL->printToScreen();
     if (($_GET['pageSection'] == "project") && ($_GET['pageAction'] == "form")) {
          focus("project","name");
     }
     if (($_GET['pageSection'] == "filetypes") && ($_GET['pageAction'] == "form")) {
          focus("filetype","name");
     }
     if ($GLOBALS['message'] != "") alert($GLOBALS['message']);
}

function GetActContent() {
     $DB = new Db();
     if ($_GET['pageSection'] == "project") {
          if ($_GET['pageAction'] == "form") {
               if (isset($_GET['edit'])) {
                    $DB->toGlobals("SELECT * FROM project WHERE cod_project = ".$_GET['edit']);
               }
               $FORM = new formTemplate(PHPSCRIBE_XML_PATH."project.xml",PHPSCRIBE_TEMPLATE_PATH."project.tpl","project",$_SERVER['REQUEST_URI'],"POST");
               $FORM->Document = &$GLOBALS['HTML'];
               $FORM->inputStyle = "normal";
               $FORM->labelStyle = "subnav";
               $FORM->buttonStyle = "button";
               $FORM->jsPath = PHPSCRIBE_JAVASCRIPT_PATH;
               $FORM->processXML();
               return $FORM->getContents();
          } else {
               $REPORT = new Report("report.tpl",TRUE);
               $REPORT->fields = "p.name AS \"Name\",
                                  IF(LENGTH(p.brief_description) > 20,CONCAT(LEFT(p.brief_description,20),'...'),p.brief_description) AS \"Brief Description\",
                                  IF(r.number_revision IS NULL,'None',MAX(r.number_revision)) AS \"Last Revision\",
                                  CONCAT('<A HREF=\"projects.php?pageSection=project&pageAction=form&edit=',p.cod_project,'\" CLASS=normalblue>Edit</A>') AS ' ',
                                  CONCAT('<A CLASS=normalblue HREF=\"projects.php?pageSection=project&pageAction=list&delete=',p.cod_project,'\">Remove</A>') AS ' '";
               $REPORT->tables = "project p LEFT JOIN revision r USING(cod_project)";
               $REPORT->groupby = "p.cod_project";
               $REPORT->orderby = "p.name";
               $REPORT->jsPath = PHPSCRIBE_JAVASCRIPT_PATH;
               $REPORT->noRegTpl = "noproject.tpl";
               $REPORT->assignValue("link","<a href='projects.php?pageSection=project&pageAction=form' class='normalblue' ".mouseOver("Add a Project").">&nbsp;Add a Project</a>");
               $REPORT->setColumnSizes(array(40,26,16,6,12));
               $REPORT->setMaxRows(5);
               $REPORT->setLinkCss("subnav");
               $REPORT->appendExtraVars("&pageSection=project&pageAction=list");
               $REPORT->buildPage();
               return $REPORT->getContents();
          }
     } else if ($_GET['pageSection'] == "documentation") {
          if ($_GET['pageAction'] == "setup") {
               $FORM = new formTemplate(PHPSCRIBE_XML_PATH."gendoc.xml",PHPSCRIBE_TEMPLATE_PATH."gendoc.tpl","gendoc",$_SERVER['PHP_SELF']."?pageSection=documentation&pageAction=generate","POST");
               $FORM->Document = &$GLOBALS['HTML'];
               $FORM->inputStyle = "normal";
               $FORM->labelStyle = "subnav";
               $FORM->buttonStyle = "button";
               $FORM->jsPath = PHPSCRIBE_JAVASCRIPT_PATH;
               $FORM->processXML();
               return $FORM->getContents();
          } elseif ($_GET['pageAction'] == "generate") {
               $TEMPLATE = new Template(PHPSCRIBE_TEMPLATE_PATH."gendocresult.tpl");
               $TEMPLATE->prepare();
               $TEMPLATE->assign("result",$_GET['result']);
               return $TEMPLATE->getOutputContent();
          } else {
               return "<BR><CENTER><SPAN CLASS='subnav'>Bad Parameter Combination</SPAN><BR><BR><A HREF='javascript:history.go(-1);' ".mouseOver("Back")." CLASS='normalblue'>« Back</A></CENTER>";
          }
     } else if ($_GET['pageSection'] == "filetypes") {
          if ($_GET['pageAction'] == "form") {
               if (isset($_GET['edit'])) {
                    $DB->toGlobals("SELECT * FROM file_type WHERE cod_file_type = ".$_GET['edit']);
               }
               $FORM = new formTemplate(PHPSCRIBE_XML_PATH."filetype.xml",PHPSCRIBE_TEMPLATE_PATH."filetype.tpl","filetype",$_SERVER['REQUEST_URI'],"POST");
               $FORM->Document = &$GLOBALS['HTML'];
               $FORM->inputStyle = "normal";
               $FORM->labelStyle = "subnav";
               $FORM->buttonStyle = "button";
               $FORM->jsPath = PHPSCRIBE_JAVASCRIPT_PATH;
               $FORM->processXML();
               return $FORM->getContents();
          } else {
               $paging = ($_GET['page'] && $_GET['firstpage'] && $_GET['lastpage']);
               $nopaging = (!$_GET['page'] && !$_GET['firstpage'] && !$_GET['lastpage']);
               if (!$paging && !$nopaging) {
                    return "<BR><CENTER><SPAN CLASS='subnav'>Bad Parameter Combination</SPAN><BR><BR><A HREF='javascript:history.go(-1);' ".mouseOver("Back")." CLASS='normalblue'>« Back</A></CENTER>";
               }
               $REPORT = new Report("report.tpl",TRUE);
               $REPORT->fields = "ft.extension AS \"Extension\",
                                  ft.name AS \"Description\",
                                  ft.language AS \"Language\",
                                  CONCAT('<A HREF=\"projects.php?pageSection=filetypes&pageAction=form&edit=',ft.cod_file_type,'\" CLASS=normalblue>Edit</A>') AS ' ',
                                  CONCAT('<A CLASS=normalblue HREF=\"projects.php?pageSection=filetypes&pageAction=list&delete=',ft.cod_file_type,'\">Remove</A>') AS ' '";
               $REPORT->tables = "file_type ft";
               $REPORT->orderby = "ft.name";
               $REPORT->jsPath = PHPSCRIBE_JAVASCRIPT_PATH;
               $REPORT->noRegTpl = "noproject.tpl";
               $REPORT->assignValue("link","<a href='projects.php?pageSection=filetypes&pageAction=form' class='normalblue' ".mouseOver("Add a File Type").">&nbsp;Add a File Type</a>");
               $REPORT->setColumnSizes(array(13,55,14,6,12));
               $REPORT->setMaxRows(5);
               $REPORT->setLinkCss("subnav");
               $REPORT->appendExtraVars("&pageSection=filetypes&pageAction=list");
               $REPORT->buildPage();
               return $REPORT->getContents();
          }
     } else {
          $OVERVIEW = new Template(PHPSCRIBE_TEMPLATE_PATH."overview.tpl");
          $OVERVIEW->prepare();
          $OVERVIEW->assignGlobal("imagePath",PHPSCRIBE_IMAGE_PATH);
          $OVERVIEW->assignGlobal("absolutePath",PHPSCRIBE_ABSOLUTE_PATH);
          $RS = $DB->Query("SELECT * FROM project");
          $OVERVIEW->assign("nProjects",($DB->affectedRows() > 0 ? $DB->affectedRows() : "no"));
          if ($DB->affectedRows() > 0) {
               $OVERVIEW->newBlock("list_projects");
               $OVERVIEW->newBlock("docs");
          }
          $RS = $DB->Query("SELECT * FROM file_type");
          if ($RS && $RS->RecordCount() > 0) {
               $OVERVIEW->newBlock("list_filetypes");
          }
          return $OVERVIEW->getOutputContent();
     }
}

function SaveData(&$postData,&$message) {
     $DB = new Db();
     if ($postData['pageSection'] == "project") {
          if ((!isset($postData['cod_project'])) || ($postData['cod_project'] == "")) {
               $postData['server_path'] = str_replace("\\\\","\\",$postData['server_path']);
               $GLOBALS['server_path'] = $postData['server_path'];
               $DB->Insert("project",$postData);
               if ($DB->AffectedRows() > 0) {
                    $message = "Project created successfully!";
                    $_GET['pageAction'] = "list"; // switch to project list
                    return TRUE;
               } else {
                    $message = "Project couldn't be created!";
                    return FALSE;
               }
          } else {
               $postData['server_path'] = str_replace("\\\\","\\",$postData['server_path']);
               $GLOBALS['server_path'] = $postData['server_path'];
               $DB->Update("project",$postData,"cod_project = ".$postData['cod_project']);
               $message = "Project updated successfully!";
               $_GET['pageAction'] = "list"; // switch to project list
               return TRUE;
          }
     } elseif ($postData['pageSection'] == "filetypes") {
          if ((!isset($postData['cod_file_type'])) || ($postData['cod_file_type'] == "")) {
               $RS = $DB->Query("SELECT * FROM file_type WHERE extension = '".allTrim($postData['extension'])."'");
               if ($RS && $RS->RecordCount() == 0) {
                    $DB->Insert("file_type",$postData);
                    $message = "File Type inserted successfully!";
                    $_GET['pageAction'] = "list";
                    return TRUE;
               } else {
                    $message = "There is another file type with the extension ".allTrim($postData['extension']." in the database!");
                    return FALSE;
               }
          }
     }
}

function DeleteData($data,&$message) {
     $DB = new Db();
     if ($_GET['pageSection'] == "project") {
          $RS = $DB->Query("SELECT * FROM project WHERE cod_project = ".$data);
          if ($DB->AffectedRows() > 0) {
               $DB->Delete("project","cod_project = ".$data);
               $RS = $DB->Query("SELECT cod_revision FROM revision WHERE cod_project = ".$data." ORDER BY number_revision DESC");
               if ($DB->AffectedRows() > 0) {
                    list($codRev) = $RS->FetchRow();
                    $DB->Delete("example","revision = ".$codRev);
                    $DB->Delete("file","revision = ".$codRev);
                    $DB->Delete("class","revision = ".$codRev);
                    $DB->Delete("function","revision = ".$codRev);
                    $DB->Delete("param","revision = ".$codRev);
                    $DB->Delete("property","revision = ".$codRev);
                    $DB->Delete("revision","cod_revision = ".$codRev);
                    $DB->Delete("see","revision = ".$codRev);
                    $DB->Delete("uses","revision = ".$codRev);
               }
               $message = "Project deleted successfully!";
               $_GET['pageAction'] = "list"; // switch to project list
               return TRUE;
          } else {
               $message = "Project couldn't be deleted or has already been deleted!";
               return FALSE;
          }
     } elseif ($_GET['pageSection'] == "filetypes") {
          $RS = $DB->Query("SELECT * FROM file_type");
          if ($DB->affectedRows() == 1) {
               $message = "At least one file type must remain in the database. File type couldn't be deleted!";
               return FALSE;
          } else {
               $RS = $DB->Query("SELECT * FROM file WHERE cod_file_type = ".$data);
               if ($DB->affectedRows() == 0) {
                    $DB->Delete("file_type","cod_file_type = ".$data);
                    $message = "File type deleted successfully";
                    return TRUE;
               } else {
                    $message = "File type couldn't be deleted. At least one project file name contains this extension!";
                    return FALSE;
               }
          }
     }
}

function GenerateDocumentation(&$message) {
     $DB = new Db();
     if (($_POST['generation_type'] == 1) && ($_POST['output_directory'] == "")) {
          $message = "To store documentation on the server, you must provide an output directory!";
          $_GET['pageAction'] = "setup";
          return FALSE;
     } elseif (($_POST['generation_type'] == 1) && (!is_dir($_POST['output_directory']))) {
          $message = "The output directory is not valid or doesn't exists!";
          $GLOBALS['output_directory'] = str_replace("\\\\","\\",$_POST['output_directory']);
          $_GET['pageAction'] = "setup";
          return FALSE;
     }
     $RS = $DB->Query("SELECT server_path FROM project WHERE cod_project = ".$_POST['cod_project']);
     if ($DB->AffectedRows() > 0) {
          list($path) = $RS->FetchRow();
          if ($path[strlen($path)-1] != '/') $path .= '/';
          $PARSER = new FileDocParser($_POST['cod_project'],$path);
          $PARSER->onlyClasses = ($_POST['classes_only'] == 1) ? TRUE : FALSE;
          $PARSER->onlyPublic = ($_POST['private_members'] == 1) ? FALSE : TRUE;
          $PARSER->Process();
		  if (!$PARSER->objCounter) {
               $_GET['result'] = "Files parsing ... <B><FONT COLOR='#FF0000'>error</FONT></B><BR>No comment lines matched the phpScribe's pattern!<BR>";
          } else {
               $_GET['result'] = "Files parsing ... <B>ok</B><BR>";
               $_GET['result'] .= "Database update ... <B>ok</B><BR>";
               if (($_POST['generation_type'] == 1) && ($_POST['output_directory'] != "")) {
                    $DOCGEN = new DocGenerator($_POST['cod_project'],$_POST['doc_skin'],$_POST['output_directory']);
                    $DOCGEN->Generate();
                    $_GET['result'] .= "Documentation files stored on server's file system ... <B>ok</B><BR>";
               } elseif ($_POST['generation_type'] == 2) {
                    $_GET['result'] .= "Documentation files generated ... <A HREF='dump.php?cod_project=".$_POST['cod_project']."&doc_skin=".$_POST['doc_skin']."' CLASS='bignormal' TITLE='click to download' onMouseOver='window.status=\"click to download\";return true;' onMouseOut='window.status=\"\";return true;'><B>click to download</B></A><BR>";
               }
          }
     }
}
?>
Return current item: PhpScribe Documentation Generator