<?php
/*------------------------------------------------------------*/
class OhadResume extends Mcontroller {
    
/*------------------------------------------------------------*/
    
public function index() {
        
$this->showTxt("summary""Summary""CV");
    }
    
/*------------------------------------------------------------*/
    
public function viewSource() {
        
$file $_REQUEST['file'];
        
highlight_file($file);
    }
    
/*------------------------------------------------------------*/
    
public function showTxt($name null$title null$sectionName null) {
        if ( ! 
$name ) {
            
$name = @$_REQUEST['name'];
            
$title = @$_REQUEST['title'];
            
$sectionName = @$_REQUEST['sectionName'];
        }
        
$tpl "$name.tpl";
        
$txt $this->Mview->render($tpl);
        
$txt "<pre>\n$txt</pre>\n";
        
$this->Mview->msg("$sectionName/$title");
        
$this->Mview->pushOutput($txt);
    }
    
/*------------------------------------------------------------*/
    
public function run() {
        
$class $_REQUEST['class'];
        
$classFile "$class.class.php";
        require_once(
$classFile);
        
$instance = new $class;
        
$instance->index();
        echo 
"<pre>\n";
        
highlight_file($classFile);
        echo 
"</pre>\n";
    }
    
/*------------------------------------------------------------*/
    
public function printVersion() {
        
$menu $this->menu();
        
$CV $menu['CV'];
        
$summary $CV[0];
        
$summary['title'] = "Ohad Aloni's CV";
        
$education $CV[1];
        
$printVersion $menu['Experience'];
        
array_unshift($printVersion$education);
        
array_unshift($printVersion$summary);
        
$txt "";
        foreach ( 
$printVersion as $key => $item ) {
            
$name $item['name'];
            
$title $item['title'];
            
$titleTxt "$title\n\n";
            if ( 
$key )
                
$tpl "$name.tpl";
            else
                
$tpl "experience/$name.tpl";
            if ( 
$key == )
                
$txt .= "\n\n\nExperience\n\n";
            
$rendered $this->Mview->render($tpl);
            
$postfix "\n\n\n\n\n";
            
$txt   .= "$titleTxt$rendered$postfix";
        }
        
$txtFile "../tmp/ohadResume.txt";
        
$pdfFile "../tmp/ohadResume.pdf";
        
file_put_contents($txtFile$txt);

        
system("rm $pdfFile");
        
/*    system("paps <  $txtFile | ps2pdf - $pdfFile");    */
        
system("pandoc $txtFile -o $pdfFile");
        
$pdf file_get_contents($pdfFile);

        
/*    $filesize = strlen($txt);    */
        /*    header("Content-type: text/plain");    */
        /*    header("Content-Disposition: attachment; filename=ohadResume.txt");    */
        /*    header("Content-Length: $filesize");    */
        /*    echo $txt;    */
        
$filesize strlen($pdf);
        
header("Content-type: application/pdf");
        
header("Content-Disposition: attachment; filename=ohadResume.pdf");
        
header("Content-Length: $filesize");
        echo 
$pdf;
    }
    
/*------------------------------------------------------------*/
    
public function experience() {
        
$this->Mview->msg("CV/Experience");
        
$menu $this->menu();
        
$Experience $menu['Experience'];
        
$txt "<pre>\n";
        foreach ( 
$Experience as $item ) {
            
$name $item['name'];
            
$title $item['title'];
            
$titleTxt "<h4 style=\"color:blue\">$title</h4>\n";
            
$tpl "experience/$name.tpl";
            
$rendered $this->Mview->render($tpl);
            
$postfix "\n\n\n";
            
$txt   .= "$titleTxt$rendered$postfix";
        }
        
$txt .= "</pre>\n";
        
$this->Mview->pushOutput($txt);
    }
    
/*------------------------------------------------------------*/
    
public function phpinfo() {
        
phpinfo();
    }
    
/*------------------------------------------------------------*/
    /*------------------------------------------------------------*/
    
private $startTime;
    
/*------------------------------*/
    
protected function before() {
        
ini_set('max_execution_time'10);
        
ini_set("memory_limit""5M");

        
$this->startTime microtime(true);
        
$this->Mview->assign(array(
            
'controller' => $this->controller,
            
'action' => $this->action,
        ));
        if ( 
$this->showMargins()) {
            
$this->Mview->showTpl("head.tpl");
            
$this->Mview->showTpl("header.tpl");
            
$this->Mview->showTpl("menuDriver.tpl", array(
                
'menu' => $this->menu(),
            ));
        }
    }
    
/*------------------------------*/
    
protected function after() {
        if ( ! 
$this->showMargins())
            return;
        
$endTime microtime(true);
        
$time $endTime $this->startTime ;
        
$millis $time 1000;
        
$millis round($millis3);
        if ( 
$this->action == 'run' )
            
$this->Mview->msg("Running Time: $millis milliseconds");
        
$this->Mview->showTpl("footer.tpl");
        
$this->Mview->showTpl("foot.tpl");
    }
    
/*------------------------------------------------------------*/
    /*------------------------------------------------------------*/
    
private function isAjax() {
        
$http_x_requested_with = @$_SERVER['HTTP_X_REQUESTED_WITH'];
        
$isAjax =
            
$http_x_requested_with &&
            
strtolower($http_x_requested_with) == "xmlhttprequest" ;
        return(
$isAjax);
    }
    
/*------------------------------*/
    
private function showMargins() {
        if ( 
$this->isAjax() ) {
            return(
false);
        }
        if( 
in_array($this->action, array(
                    
'phpinfo',
                    
'viewsource',
                    
'printversion',
                ))) {
            return(
false);
        }
        return(
true);
    }
    
/*------------------------------------------------------------*/
    
private function menu() {
        
$menu = array(
             
'CV' => array(
                array(
                    
'name' => 'summary',
                    
'title' => 'Summary',
                    
// target, if any
                    
'target' => null,
                    
// tpl to show, source file to highlight, func to call, or url to visit
                    
'txt' => 'summary',
                    
'source' => null,
                    
'func' => null,
                    
'url' => null,
                ),
                array(
                    
'name' => 'education',
                    
'title' => 'Education',
                    
'txt' => 'education',
                ),
                array(
                    
'name' => 'Experience',
                    
'title' => 'Experience',
                    
'func' => 'experience',
                ),
                array(
                    
'name' => 'printVersion',
                    
'title' => 'Print Version',
                    
/*    'target' => 'printVersion',    */
                    
'func' => 'printVersion',
                ),
             ),
             
'Experience' => array(
                array(
                    
'name' => 'bidder',
                    
'title' => '2013-2017 Bidder (KeypointMedia/PLYmedia/Edge226)',
                    
'txt' => 'experience/bidder',
                ),
                array(
                    
'name' => 'ironsource',
                    
'title' => '2011-2012 - Ironsource',
                    
'txt' => 'experience/ironsource',
                ),
                array(
                    
'name' => 'whitesmoke',
                    
'title' => '2009-2011 - Whitesmoke',
                    
'txt' => 'experience/whitesmoke',
                ),
                array(
                    
'name' => 'freelance',
                    
'title' => '2001-2009 - Freelance',
                    
'txt' => 'experience/freelance',
                ),
                array(
                    
'name' => 'theora.com',
                    
'title' => '1999-2001 - Theora.com',
                    
'txt' => 'experience/theora.com',
                ),
                array(
                    
'name' => 'softwareEngines',
                    
'title' => '1990-1999 - Sotware Engines',
                    
'txt' => 'experience/softwareEngines',
                ),
                array(
                    
'name' => 'objectiveTech',
                    
'title' => '1990-1990 - Objective Technologies',
                    
'txt' => 'experience/objectiveTech',
                ),
                array(
                    
'name' => 'wallStreet',
                    
'title' => '1987-1990 - Wall Street',
                    
'txt' => 'experience/wallStreet',
                ),
             ),
             
'Software Engines' => array(
                array(
                    
'name' => 'softwareEngine',
                    
'title' => 'The Software Engine Product',
                    
'txt' => 'softwareEngines/softwareEngine',
                ),
                array(
                    
'name' => 'wallStreet',
                    
'title' => 'Wall Street',
                    
'txt' => 'softwareEngines/wallStreet',
                ),
                array(
                    
'name' => 'articles',
                    
'title' => 'Articles',
                    
'txt' => 'softwareEngines/articles',
                ),
                array(
                    
'name' => 'ericsson',
                    
'title' => 'Ericsson Telecommunications',
                    
'txt' => 'softwareEngines/ericsson',
                ),
                array(
                    
'name' => 'jCrew',
                    
'title' => 'J.Crew',
                    
'txt' => 'softwareEngines/jCrew',
                ),
                array(
                    
'name' => 'catalogFactory',
                    
'title' => 'Catalog Factory',
                    
'txt' => 'softwareEngines/catalogFactory',
                ),
             ),
             
'M - An MVC Framework' => array(
                array(
                    
'name' => 'concept',
                    
'title' => 'Concept',
                    
'txt' => 'm/concept',
                ),
                array(
                    
'name' => 'examples',
                    
'title' => 'Examples',
                    
'txt' => 'm/examples',
                ),
                array(
                    
'name' => 'demo',
                    
'title' => 'Demo',
                    
'url' => "http://theora.com/Mdemo",
                    
'target' => "mDemo",
                ),
             ),
             
'Code Samples' => array(
                array(
                    
'name' => 'thisCV',
                    
'title' => 'This CV',
                    
'source' => 'OhadResume.class.php',
                ),
                array(
                    
'name' => 'menuDriver',
                    
'title' => 'The Menu Template',
                    
'source' => 'tpl/menuDriver.tpl',
                ),
                array(
                    
'name' => 'primes',
                    
'title' => 'Primes',
                    
'run' => 'Primes',
                ),
                array(
                    
'name' => 'linkedLists',
                    
'title' => 'Linked Lists',
                    
'run' => 'Ll',
                ),
                array(
                    
'name' => 'abstracts',
                    
'title' => 'Abstracts',
                    
'run' => 'Abstracts',
                ),
             ),
             
'Android Apps' => array(
                array(
                    
'name' => 'medLog',
                    
'title' => 'Med Log',
                    
'txt' => 'android/medLog',
                ),
                array(
                    
'name' => 'knowThyStuff',
                    
'title' => 'Know Thy Stuff',
                    
'txt' => 'android/knowThyStuff',
                ),
                array(
                    
'name' => 'spots',
                    
'title' => 'Spots',
                    
'txt' => 'android/spots',
                ),
             ),
             
'Eve' => array(
                array(
                    
'name' => 'concept',
                    
'title' => 'The EKG Visualization Environment',
                    
'txt' => 'eve/concept',
                ),
                array(
                    
'name' => 'systemOverview',
                    
'title' => 'System Overview',
                    
'txt' => 'eve/systemOverview',
                ),
                array(
                    
'name' => 'arduino',
                    
'title' => 'Arduino Parallelism Micro Kernel',
                    
'source' => 'tpl/eve/kernel.cpp',
                ),
                array(
                    
'name' => 'prototype',
                    
'title' => 'Prototype',
                    
'target' => 'evePrototype',
                    
'url' => '/images/eve.jpg',
                ),
             ),
             
'Misc' => array(
                array(
                    
'name' => 'rft',
                    
'title' => 'Rock Family Trees',
                    
'txt' => "misc/rockFamilyTreees",
                 ),
                array(
                    
'name' => 'wawi',
                    
'title' => 'A Guitar Thing',
                    
'url' => "https://www.youtube.com/watch?v=ZM6ZIBXU07Q",
                    
'target' => "wawi",
                 ),
             ),
        );
        return(
$menu);
    }
    
/*------------------------------------------------------------*/
    
private function errorLog($msg$file null$line null) {
        
$pfx $line "$file:$line: " "" ;
        
error_log("$pfx$msg");
    }
    
/*------------------------------------------------------------*/
}