Pman/Images.php
[Pman.Base] / Pman.php
index 4f99955..4a79931 100644 (file)
--- a/Pman.php
+++ b/Pman.php
@@ -24,6 +24,7 @@
 class Pman extends HTML_FlexyFramework_Page 
 {
     var $appName= "";
+    var $appLogo= "";
     var $appShortName= "";
     var $appVersion = "1.8";
     var $version = 'dev';
@@ -64,6 +65,8 @@ class Pman extends HTML_FlexyFramework_Page
         // echo'<PRE>';print_R($boot);exit;
         $this->appName= $boot->appName;
         $this->appNameShort= $boot->appNameShort;
+        
+        
         $this->appModules= $boot->enable;
         $this->isDev = empty($boot->Pman['isDev']) ? false : $boot->Pman['isDev'];
         $this->appDisable = $boot->disable;
@@ -77,6 +80,9 @@ class Pman extends HTML_FlexyFramework_Page
             $this->isDev = true;
         }
         
+        
+        
+        
     }
     
     function get($base) 
@@ -118,6 +124,15 @@ class Pman extends HTML_FlexyFramework_Page
         HTML_FlexyFramework::get()->generateDataobjectsCache($this->isDev);
         
         //header('Content-type: application/xhtml+xml; charset=utf-8');
+        
+        
+        
+        if ($this->company->logo_id) {
+            $im = DB_DataObject::Factory('Images');
+            $im->get($this->company->logo_id);
+            $this->appLogo = $this->baseURL . '/Images/Thumb/400x100/'. $this->company->logo_id .'/' . $im->filename;
+        }
+        
         header('Content-type: text/html; charset=utf-8');
          
     }
@@ -125,32 +140,33 @@ class Pman extends HTML_FlexyFramework_Page
         return $this->get($base);
     }
     
+    
+    // --------------- AUTHENTICATION or  system information
     /**
-     * ------------- Authentication and permission info about logged in user!!!
-     * 
-     * 
+     * loadOwnerCompany:
+     * finds the compay with comptype=='OWNER'
+     *
+     * @return {Pman_Core_DataObjects_Companies} the owner company
      */
-    
     function loadOwnerCompany()
     {
+         
         $this->company = DB_DataObject::Factory('Companies');
-        if ($this->company) { // non-core pman projects
-            return; 
+        if (!is_a($this->company, 'DB_DataObject')) { // non-core pman projects
+            return false
         }
         $this->company->get('comptype', 'OWNER');
-        
-    }
-    function staticGetAuthUser()
-    {
-        $ff = HTML_FlexyFramework::get();
-        $tbl = empty($ff->Pman['authTable']) ? 'Person' : $ff->Pman['authTable'];
-        
-        $u = DB_DataObject::factory($tbl);
-        if (!$u->isAuth()) {
-            return false;
-        }
-        return $u->getAuthUser();
+        return $this->company;
     }
+    
+    
+    
+    /**
+     * getAuthUser: - get the authenticated user..
+     *
+     * @return {DB_DataObject} of type Pman[authTable] if authenticated.
+     */
+    
     function getAuthUser()
     {
         if (!empty($this->authUser)) {
@@ -166,45 +182,54 @@ class Pman extends HTML_FlexyFramework_Page
         $this->authUser =$u->getAuthUser();
         return $this->authUser ;
     }
+    /**
+     * hasPerm:
+     * wrapper arround authuser->hasPerm
+     * @see Pman_Core_DataObjects_User::hasPerm
+     *
+     * @param {String} $name  The permission name (eg. Projects.List)
+     * @param {String} $lvl   eg. (C)reate (E)dit (D)elete ... etc.
+     * 
+     */
     function hasPerm($name, $lvl)  // do we have a permission
     {
         static $pcache = array();
         $au = $this->getAuthUser();
-        return $au->hasPerm($name,$lvl);
+        return $au && $au->hasPerm($name,$lvl);
         
     }
-    
-    function modules()
+   
+    /**
+     * modulesList:  List the modules in the application
+     *
+     * @return {Array} list of modules
+     */
+    function modulesList()
     {
-        // appModules/appDisable contain a comma limited list of
-        // both modules and components that can be enabled/disabled..
-         $boot = HTML_FlexyFramework::get();
-
-        // the modules call just lists the modules
-        $enabled =  array('Core' => true);
-         $am = !empty($boot->enable) ? explode(',',  $boot->enable) : array();
-        foreach($am as $k) {
-            if (strpos( $k ,'.') ) {
-                continue;
-            }
-            $enabled[$k] = true;
-        }
+        $this->init();
         
+        $mods = explode(',', $this->appModules);
+        if (in_array('Core',$mods)) { // core has to be the first  modules loaded as it contains Pman.js
+            array_unshift($mods,   'Core');
+        }
         
-        $disabled =  !empty($boot->disable) ?  explode(',', $boot->disable) : array();
-        foreach($disabled as $k) {
-            if ( strpos( $k ,'.') ) {
+        $mods = array_unique($mods);
+         
+        $disabled =  explode(',', $this->appDisable ? $this->appDisable: '');
+        $ret = array();
+        foreach($mods as $mod) {
+            // add the css file..
+            if (in_array($mod, $disabled)) {
                 continue;
             }
-            if (isset($enabled[$k])) {
-                unset($enabled[$k]);
-            }   
+            $ret[] = $mod;
         }
-         //echo '<PRE>';       var_Dump($enabled);
-
-        return array_keys($enabled); 
+        return $ret;
     }
     
+     
+    
+    
     function hasModule($name) 
     {
         $this->init();
@@ -221,6 +246,7 @@ class Pman extends HTML_FlexyFramework_Page
         return true;
     }
     
+     
     
     
     
@@ -418,9 +444,16 @@ class Pman extends HTML_FlexyFramework_Page
      */
     function jerrAuth()
     {
+        $au = $this->authUser();
+        if ($au) {
+            // is it an authfailure?
+            $this->jerr("Permission denied to view this resource", array('authFailure' => true));
+        }
         $this->jerr("Not authenticated", array('authFailure' => true));
     }
      
+     
+     
     /**
      * ---------------- Standard JSON outputers. - used everywhere
      */
@@ -533,43 +566,20 @@ class Pman extends HTML_FlexyFramework_Page
     
     
    
-    /**
-     * modulesList:  List the modules in the application
-     *
-     * @return {Array} list of modules
-     */
-      
-    
-    function modulesList()
-    {
-         $mods = explode(',', $this->appModules);
-        if (in_array('Core',$mods)) { // core has to be the first  modules loaded as it contains Pman.js
-            array_unshift($mods,   'Core');
-        }
-        
-        $mods = array_unique($mods);
-         
-        $disabled =  explode(',', $this->appDisable ? $this->appDisable: '');
-        $ret = array();
-        foreach($mods as $mod) {
-            // add the css file..
-            if (in_array($mod, $disabled)) {
-                continue;
-            }
-            $ret[] = $mod;
-        }
-        return $ret;
-    }
-    
     
     /**
-     * ---------------- Page output?!?!?
+     * ---------------- OUTPUT
      */
     function hasBg($fn) // used on front page to check if logos exist..
     {
         return file_exists($this->rootDir.'/Pman/'.$this->appNameShort.'/templates/images/'.  $fn);
     }
-    
+     /**
+     * outputJavascriptIncludes:
+     *
+     * output <script....> for all the modules in the applcaiton
+     *
+     */
     function outputJavascriptIncludes() // includes on devel version..
     {
         
@@ -577,13 +587,10 @@ class Pman extends HTML_FlexyFramework_Page
         
         foreach($mods as $mod) {
             // add the css file..
-            if (in_array($mod, $disabled)) {
-                continue;
-            }
-            
+        
             
             $files = $this->moduleJavascriptList($mod.'/widgets');
-            foreach($files as $f) {
+             foreach($files as $f) {
                 echo '<script type="text/javascript" src="'. $f. '"></script>'."\n";
             }
             
@@ -595,7 +602,12 @@ class Pman extends HTML_FlexyFramework_Page
         }
          
     }
-    
+     /**
+     * outputCSSIncludes:
+     *
+     * output <link rel=stylesheet......> for all the modules in the applcaiton
+     *
+     */
     function outputCSSIncludes() // includes on CSS links.
     {
         
@@ -614,28 +626,7 @@ class Pman extends HTML_FlexyFramework_Page
         }
          
     }
-    /**
-     * packJS:
-     *
-     * @file path to 
-     *
-     *
-     */
-    
-     function packJS($dir)
-    {
-       
-        // target has to be 'aliased'
-        // target filename can be an md5..
-        
-        require_once 'Pman/Core/JsCompile.php';
-        $x = new Pman_Core_JsCompile();
-        $x->packScript(dirname(__FILE__).'/Hex/templates/images', 
-                       array($dir),
-                        $this->rootURL . '/Hex/templates/images');
-        
-        
-    }
+      
     /**
      * Gather infor for javascript files..
      *
@@ -644,11 +635,19 @@ class Pman extends HTML_FlexyFramework_Page
      */
     function moduleJavascriptFilesInfo($mod)
     {
+        
+        static $cache = array();
+        
+        if (isset($cache[$mod])) {
+            return $cache[$mod];
+        }
+        
+        
         $ff = HTML_FlexyFramework::get();
         
         $base = dirname($_SERVER['SCRIPT_FILENAME']);
         $dir =   $this->rootDir.'/Pman/'. $mod;
-            
+        $path = $this->rootURL ."/Pman/$mod/";
         
         $ar = glob($dir . '/*.js');
         
@@ -674,24 +673,31 @@ class Pman extends HTML_FlexyFramework_Page
         $lsort = create_function('$a,$b','return strlen($a) > strlen($b) ? 1 : -1;');
         usort($files, $lsort);
         
-        $output = date('Y-m-d-H-i-s-', $maxtime). str_replace('/','.',$mod).'-'.md5(serialize($arfiles)) .'.js';
-        
-        return (object) array(
-            'files' => $files,
-            'filesmtime' => $arfiles,
-            'maxtime' => $maxtime,
-            'compile' => $compile,
-            'translation_file' =>  realpath($base .'/_translations_/' . str_replace('/','.', $mod) .  '.js'),
-            'output' => $output,
-            'translation_data' => preg_replace('/\.js$/', '.__translation__.js', $output),
-            'translation_base' => dirname($dir) . '/',
-            'basedir' => $basedir,
-            'baseurl' => $baseurl,
+        $smod = str_replace('/','.',$mod);
+        
+        $output = date('Y-m-d-H-i-s-', $maxtime). $smod .'-'.md5(serialize($arfiles)) .'.js';
+        
+        $tmtime = file_exists($this->rootDir.'/_translations_/'. $smod.'.js')
+            ? filemtime($this->rootDir.'/_translations_/'. $smod.'.js') : 0;
+        
+        $cache[$mod]  = (object) array(
+            'smod' =>               $smod, // module name without '/'
+            'files' =>              $files, // list of all files.
+            'filesmtime' =>         $arfiles,  // map of mtime=>file
+            'maxtime' =>            $maxtime, // max mtime
+            'compile' =>            $this->isDev ? false : $compile,
+            'translation_file' =>   $base .'/_translations_/' . $smod .  '.js',
+            'translation_mtime' =>  $tmtime,
+            'output' =>             $output,
+            'translation_data' =>   preg_replace('/\.js$/', '.__translation__.js', $output),
+            'translation_base' =>   $dir .'/', //prefix of filename (without moudle name))
+            'basedir' =>            $basedir,   
+            'baseurl' =>            $baseurl,
+            'module_dir' =>         $dir,  
         );
+        return $cache[$mod];
     }
-    
-    
-    
+     
     
     /**
      *  moduleJavascriptList: list the javascript files in a module
@@ -719,12 +725,15 @@ class Pman extends HTML_FlexyFramework_Page
         
         
         $dir =   $this->rootDir.'/Pman/'. $mod;
+        
+        
         if (!file_exists($dir)) {
+            echo '<!-- missing directory '. htmlspecialchars($dir) .' -->';
             return array();
         }
         
         $info = $this->moduleJavascriptFilesInfo($mod);
-        
+       
         
           
         if (empty($info->files)) {
@@ -734,28 +743,36 @@ class Pman extends HTML_FlexyFramework_Page
         
         // only compile this stuff if public_cache is set..
         
-        $compile = empty($ff->Pman['public_cache_dir']) ? 0 : 1;
-        
+         
         // suggestions...
         //  public_cache_dir =   /var/www/myproject_cache
         //  public_cache_url =   /myproject_cache    (with Alias apache /myproject_cache/ /var/www/myproject_cache/)
-       
-          
+        
+        // bit of debugging
+        if (!$info->compile) {
+            echo "<!-- Javascript compile turned off (isDev on, or public_cache_dir not set) -->\n";
+            return $info->files;
+        }
         // where are we going to write all of this..
         // This has to be done via a 
-        if ( $info->compile && !file_exists($info->basedir.'/'.$info->output)) {
+        if (!file_exists($info->basedir.'/'.$info->output)) {
             require_once 'Pman/Core/JsCompile.php';
             $x = new Pman_Core_JsCompile();
             
             $x->pack($info->filesmtime,$info->basedir.'/'.$info->output, $info->translation_base);
         }
         
-        if ($info->compile && file_exists($info->basedir.'/'.$info->output) && filesize($info->basedir.'/'.$info->output)) {
+        if (file_exists($info->basedir.'/'.$info->output) &&
+                filesize($info->basedir.'/'.$info->output)) {
             
-            return array(
+            $ret =array(
                 $info->baseurl.'/'. $info->output,
-                $this->rootURL."/_translations_/". str_replace('/','.', $mod).".js"
+              
             );
+            if ($info->translation_mtime) {
+                $ret[] = $this->rootURL."/_translations_/". $info->smod.".js?ts=".$info->translation_mtime;
+            }
+            return $ret;
         }
         
         
@@ -763,7 +780,7 @@ class Pman extends HTML_FlexyFramework_Page
         // give up and output original files...
         
          
-        return $files;
+        return $info->files;
 
         
     }
@@ -780,16 +797,17 @@ class Pman extends HTML_FlexyFramework_Page
      * 
      * @param {String} action  - group/name of event
      * @param {DataObject|false} obj - dataobject action occured on.
-     * @param {String} any remarks 
+     * @param {String} any remarks
+     * @return {false|DB_DataObject} Event object.,
      */
     
     function addEventOnce($act, $obj = false, $remarks = '') 
     {
-        $au = $this->getAuthUser();
+        
         $e = DB_DataObject::factory('Events');
         $e->init($act,$obj,$remarks); 
         if ($e->find(true)) {
-            return;
+            return false;
         }
         return $this->addEvent($act, $obj, $remarks);
     }
@@ -800,21 +818,27 @@ class Pman extends HTML_FlexyFramework_Page
      * @param {String} action  - group/name of event
      * @param {DataObject|false} obj - dataobject action occured on.
      * @param {String} any remarks
-     * @return {Number} Event id.,
+     * @return {DB_DataObject} Event object.,
      */
     
     function addEvent($act, $obj = false, $remarks = '') 
     {
         $au = $this->getAuthUser();
+       
         $e = DB_DataObject::factory('Events');
         $e->init($act,$obj,$remarks); 
          
         $e->event_when = date('Y-m-d H:i:s');
         
         $eid = $e->insert();
+        
+        $wa = DB_DataObject::factory('core_watch');
+        $wa->notifyEvent($e); // trigger any actions..
+        
+        
         $ff  = HTML_FlexyFramework::get();
         if (empty($ff->Pman['event_log_dir'])) {
-            return $eid;
+            return $e;
         }
         $file = $ff->Pman['event_log_dir']. date('/Y/m/d/'). $eid . ".php";
         if (!file_exists(dirname($file))) {
@@ -825,12 +849,23 @@ class Pman extends HTML_FlexyFramework_Page
             'GET' => empty($_GET) ? array() : $_GET,
             'POST' => empty($_POST) ? array() : $_POST,
         ), true));
+         
+        return $e;
+        
+    }
+    // ------------------ DEPERCIATED ---
+     
+    function modules() // DEPRECITAED
+    {
+        return $this->modulesList(); 
+    }
+    function staticGetAuthUser() // DEPRECIATED..
+    {
         
-        return $eid;
+        $x = new Pman();
+        return $x->getAuthUser();
         
     }
-
-    
      
     
 }