Source for file MED_CATEGORY_NODE.phpclass

Documentation is available at MED_CATEGORY_NODE.phpclass

  1. <?php
  2. /**
  3.   * Class file MED_CATEGORY_NODE.phpclass
  4.   *
  5.   * @project    Open CSP-Management
  6.   * @package    media
  7.   * @category   category-tree
  8.   *
  9.   * @author     Peter Krebs (pk) <pitlinz@users.sourceforge.net>
  10.   * @license    http://opensource.org/licenses/gpl-license.php GNU Public License
  11.   *
  12.   * @version  $Id: MED_CATEGORY_NODE.phpclass,v 1.3 2008/06/27 13:59:23 pitlinz Exp $
  13.   */
  14. require_once __OCSP_PHPINCPATH__."default_config" _OCSP_DIRSEP_ "media.conf.phpinc";
  15.  
  16. require_once __OCSP_PHPINCPATH__.'common/pcf_file.phpinc';
  17. require_once __OCSP_PHPINCPATH__.'common/pcf_directory.phpinc';
  18.  
  19. require_once __OCSP_PHPINCPATH__.'db/DBMS_TREE_NODE.phpclass';
  20.  
  21. require_once __OCSP_PHPINCPATH__."media/MED_MIMETYPE.phpclass";
  22.  
  23. /**
  24.  * media category tree node
  25.  *
  26.  * @project    Open CSP-Management
  27.  * @package    media
  28.  * @category   category-tree
  29.  *
  30.  * @author     Peter Krebs (pk) <pitlinz@users.sourceforge.net>
  31.  * @license    http://opensource.org/licenses/gpl-license.php GNU Public License
  32.  * 
  33.  * @version $Id: MED_CATEGORY_NODE.phpclass,v 1.3 2008/06/27 13:59:23 pitlinz Exp $
  34.  */
  35. class MED_CATEGORY_NODE extends DBMS_TREE_NODE {
  36.     /**
  37.       * @var  string  $myGlobalTreeKey      index in $GLOABLS['DBMS_TREE'][]
  38.       ***/
  39.     var $myGlobalTreeKey = "T_MED_CATEGORY";
  40.  
  41.     /**
  42.       * @staticvar string $myTable 
  43.       */
  44.     var $myTable="T_MED_CATEGORY";
  45.  
  46.     /**
  47.       * @staticvar  string  $myIdFld        id column = root
  48.       ***/
  49.     var $myIdFld        ="MEC_ID";
  50.  
  51.     /**
  52.       * @staticvar  string  $myParentFld    name of the parent column
  53.       ***/
  54.     var $myParentFld    ="MEC_PARENT";
  55.  
  56.     /**
  57.       * @staticvar  string  $mySortFld      name of the sort field
  58.       ***/
  59.     var $mySortFld      ="MEC_NAME";
  60.  
  61.     /**
  62.       * @var string  $myChildClass 
  63.       ***/
  64.     var $myChildClass = "MED_CATEGORY_NODE";
  65.  
  66.     /**
  67.       * @var double $hasChildrenToShowTS 
  68.       * @since pk-06-11-13
  69.       ***/
  70.     var $hasChildrenToShowTS=0// time when we found children to show
  71.  
  72.     /**
  73.       * @var boolean $offPath the category dir is not under $GLOBALS['MEDIA']['ROOTPATH']
  74.       * @since pk-05-10-13
  75.       ***/
  76.     var $offPath=FALSE;
  77.  
  78.     /**
  79.       * @var int $projId CMS_PROJECT (-1 == all)
  80.       * @since pk-06-03-03
  81.       ***/
  82.     var $projId=-1;
  83.  
  84.     /**
  85.       * @var boolean $addDefaultProj show project and default project (PROJ_ID=0)
  86.       * @since pk-06-03-05
  87.       ***/
  88.     var $addDefaultProj=FALSE;
  89.  
  90.     // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  91.     // constructor
  92.     // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  93.  
  94.     /**
  95.       * constructor
  96.       *
  97.       * @param int $aId id of the node (if set the node is poplulated)
  98.       * @param string $dDBIDX index of the database connection in $GLOBALS
  99.       * @param boolean $debug 
  100.       *
  101.       ***/
  102.     function MED_CATEGORY_NODE($aId=0,$debug=FALSE
  103.     {
  104.         if ($debugecho "<p><b>MED_CATEGORY_NODE::MED_CATEGORY_NODE($aId,$debug)</b> (".get_class($this).")</p>";
  105.         $this->init();
  106.         if (intval($aId)) {
  107.             $this->{$this->myIdFld}=$aId;
  108.             $this->dbPopulate($debug);
  109.         }
  110.     }
  111.  
  112.     // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  113.     // object var access methods
  114.     // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  115.  
  116.     /**
  117.       * returns the Title
  118.       **/
  119.  
  120.     function getTitle({
  121.         return $this->getDBField('MEC_NAME');
  122.     }
  123.  
  124.     /**
  125.       * sets the Title
  126.       * @param string $aTitle; 
  127.       ***/
  128.     function setTitle($aTitle{
  129.         $this->setDBField('MEC_NAME',$aTitle);
  130.     }
  131.  
  132.  
  133.     /**
  134.       * sets offPath
  135.       *
  136.       * @param boolean $onOff 
  137.       *
  138.       * @since pk-05-10-13
  139.       *
  140.       ***/
  141.     function setOffPath($onOff{
  142.         $this->offPath=$onOff;
  143.     }
  144.  
  145.     /**
  146.       * @return boolean 
  147.       * @since pk-05-10-13
  148.       ***/
  149.     function isOffPath({
  150.         return $this->offPath;
  151.     }
  152.  
  153.     /**
  154.       * sets the projectID
  155.       *
  156.       * @param int $aProjId 
  157.       *
  158.       * @since pk-06-03-03
  159.       *
  160.       ***/
  161.     function set_projId($aProjId{
  162.         $this->projId = $aProjId;
  163.     }
  164.  
  165.     /**
  166.       * returns if we have set a projId
  167.       *
  168.       * @returns boolean
  169.       *
  170.       ***/
  171.     function projId_IsSet({
  172.         if ($this->projId > -1return TRUE;
  173.         return FALSE;
  174.     }
  175.  
  176.     /**
  177.       * sets $addDefaultProj
  178.       *
  179.       * @param boolean $onOff on TRUE off =FALSE
  180.       *
  181.       * @since pk-06-03-05
  182.       *
  183.       ***/
  184.     function setAddDefaulProj_OnOff($onOff{
  185.         $this->addDefaultProj=$onOff;
  186.     }
  187.  
  188.  
  189.     /**
  190.       * sets child class vars
  191.       *
  192.       * @param &DBMS_TREE_NODE   $child 
  193.       *
  194.       * @since pk-06-03-05
  195.       *
  196.       ***/
  197.     function setChildClassVar(&$child{
  198.         parent::setChildClassVar($child);
  199.         $child->set_projId($this->projId);
  200.         $child->setAddDefaulProj_OnOff($this->addDefaultProj);
  201.     }
  202.  
  203.  
  204.     // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  205.     // database access methods
  206.     // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  207.  
  208.     /**
  209.       * inserts a new category and creates the category directory
  210.       *
  211.       * @param string $gDBIDX Index of $GLOBALS to the db object
  212.       * @param bool   $debug  show debug info
  213.       *
  214.       * @return int   returns the autoIncFld ID if exists or 1 on success
  215.       *
  216.       * @version pk-05-10-13
  217.       *
  218.       ***/
  219.     function dbInsert($debug=FALSE{
  220.         if ($debug{
  221.             echo "<hr><p><b>MED_CATEGORY_NODE::dbInsert($gDBIDX,$debug)</b></p>";
  222.             echo "<blockquote>";$this->writeDebugInfo();echo "</blockquote>";
  223.         }
  224.  
  225.         $this->MEC_PATH=trim($this->MEC_PATH);
  226.         if (($this->MEC_PATH==trim($GLOBALS['MED']['ROOTPATH'])) || ($this->MEC_PATH=="/")) {
  227.             if (!$GLOBALS['MED']['ALLOW_ROOTPATH']{
  228.                 $this->MEC_PATH=NULL;
  229.             }
  230.         }
  231.  
  232.  
  233.  
  234.         if ((!empty($this->MEC_PATH)) && (is_dir($this->MEC_PATH)) && (!strstr($this->MEC_PATH,$GLOBALS['MED']['ROOTPATH']))) {
  235.             $this->offPath=TRUE;
  236.         }
  237.  
  238.         if ($this->MEC_ID=parent::dbInsert($debug)) {   // insert row and set id
  239.             /* <pk-05-04-19> */
  240.             if ((!empty($this->MEC_PATH)) && (is_dir($this->getRootDir($debug)))) {     // we already have a valid category directory
  241.                 if (empty($this->MEC_WEBDIR)) {                                         // we need to update the webpath
  242.                     if (strstr($this->MEC_PATH,$_SERVER['DOCUMENT_ROOT'])) {            // path is in DOCUMENT_ROOT direct access possible
  243.                         $this->MEC_WEBDIR=str_replace($_SERVER['DOCUMENT_ROOT'],"",$this->MEC_PATH);
  244.                         $this->dbReplace($gDBIDX,$debug)// update database values
  245.                     else if ($GLOBALS['MED']['WEBDIR']{
  246.                         $this->MEC_WEBDIR=$GLOBALS['MED']['WEBDIR'].$this->getId()."/";
  247.                         $this->dbReplace($debug)// update database values
  248.                     }
  249.                     return $this->getId();
  250.                 }
  251.                 return $this->getId();
  252.             }
  253.  
  254.             if (!$this->offPath || empty($this->MEC_PATH)) // <pk-05-10-13>
  255.                 require_once __OCSP_PHPINCPATH__."common/pcf_directory.phpinc";
  256.  
  257.                 $path=str_replace('//','/',$GLOBALS['MED']['ROOTPATH']."/".$this->getId());
  258.                 if ($debugecho "<blockquote>MEC_PATH$path</blockquote>";
  259.                 pcf_mkdirs($path,0775,$debug);
  260.                 if (is_dir($path)) {
  261.                     $this->MEC_PATH=str_replace($GLOBALS['PROJECT']['PATH'],"\$@PROJECT|PATH\$",$path."/")// </pk-05-10-13>
  262.                     if ($GLOBALS['MED']['WEBDIR']{
  263.                         $this->MEC_WEBDIR=$GLOBALS['MED']['WEBDIR'].$this->getId()."/";
  264.                     }
  265.                     $this->dbReplace($gDBIDX,$debug);
  266.                     return $this->getId();
  267.                 else {
  268.                     $cmd="DELETE FROM ".$this->myTable." WHERE ".$this->myIdFld."=".$this->getId();
  269.                     $GLOBALS[$gDBIDX]->executeCmd($cmd);
  270.                     return FALSE;
  271.                 }
  272.             else {
  273.                 return $this->getId();
  274.             // </pk-05-10-13>
  275.         else {
  276.             return FALSE;
  277.         }
  278.     }
  279.  
  280.  
  281.     /**
  282.       * echos a tree intend called by write children
  283.       *
  284.       * @since pk-06-03-05
  285.       *
  286.       ***/
  287.     function getChildQueryString($cols="*",$debug=FALSE{
  288.         if ($debugecho "<p><b>DBMS_TREE_NODE::getChildQueryString($cols)</b> (".get_class($this).")</p>\n";
  289.  
  290.         $query ="SELECT ".$cols." FROM ".$this->myTable;
  291.         $query.=" WHERE ".$this->myParentFld."=".$this->getId();        // IDs are integer COLUMNS and VALUES
  292.  
  293.         if ((!empty($this->myChildKeys)) && ($keyCols=explode(",",$this->myChildKeys))) {
  294.             foreach($keyCols as $kcol{
  295.                 $query.=" AND ".$kcol." = ".$GLOBALS[$this->gDBIDX]->qs_getSlashedValue($this->getKey($kcol));
  296.             }
  297.         }
  298.  
  299.         if ($this->projId_IsSet()) {
  300.             $s_query.=" AND (PROJ_ID=".intval($this->projId);
  301.             if (intval($this->projId&& $this->addDefaultProj{
  302.                 $s_query.=" OR PROJ_ID=0";
  303.             }
  304.             $s_query.=")";
  305.         }
  306.  
  307.         if (!empty($this->mySortFld)) $query.=" ORDER BY ".$this->mySortFld;
  308.         if (sizeof($this->myChildren)) {
  309.             echo "<tr><td><table width='90%' align='right' border='0'>";
  310.             foreach($this->myChildren as $orderNr => $id{
  311.                 $GLOBALS['DBMS_TREE'][$this->getGlobalTreeKey()][$id]->writeTableRowThis($debug);
  312.                 if ($expandLevel 0{
  313.                     $GLOBALS['DBMS_TREE'][$this->getGlobalTreeKey()][$id]->writeTableRowChildren($expandLevel-1,$useCache,$debug);
  314.                 }
  315.             }
  316.             echo "</table></td></tr>";
  317.         }
  318.  
  319.         if ($debugecho "<blockquote><p>ChildQuery: <br /><nobr>$query</nobr></p></blockquote>";
  320.         return $query;
  321.     }
  322.  
  323.     // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  324.     // user rights methods
  325.     // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  326.  
  327.     /**
  328.       * returns if the current user can open the category
  329.       * (is group member of MEC_DWNLGRP or has higher rights)
  330.       *
  331.       * @param boolean $debug 
  332.       *
  333.       * @returns boolean
  334.       *
  335.       * @since pk-06-11-13
  336.       *
  337.       ***/
  338.     function curUserCanOpen($debug=FALSE{
  339.         if ($debugechoDebugMethod(__FILE__,get_class($this),"MED_CATEGORY_NODE::curUserCanOpen()");
  340.  
  341.         if (!intval($this->getDBField('MEC_DWNLGRP'))) {
  342.             // public download
  343.             return TRUE;
  344.         else if ($GLOBALS['USER']->isGroupMember(intval($this->getDBField('MEC_DWNLGRP')))) {
  345.             return TRUE;
  346.         }
  347.         return $this->curUserCanUpload($debug);
  348.     }
  349.  
  350.     /**
  351.       * returns if the current user can upload a file to the category
  352.       * (is group member of MEC_UPLOADGRP or has higher rights)
  353.       *
  354.       * @param boolean $debug 
  355.       *
  356.       * @returns boolean
  357.       *
  358.       * @since pk-06-11-13
  359.       *
  360.       ***/
  361.     function curUserCanUpload($debug=FALSE{
  362.         if ($debugechoDebugMethod(__FILE__,get_class($this),"MED_CATEGORY_NODE::curUserCanUpload()");
  363.  
  364.         if (!intval($this->getDBField('MEC_UPLOADGRP'))) {
  365.             // public upload
  366.             return TRUE;
  367.         else if ($GLOBALS['USER']->isGroupMember(intval($this->getDBField('MEC_UPLOADGRP')))) {
  368.             return TRUE;
  369.         }
  370.         return $this->curUserIsAdmin($debug);
  371.     }
  372.  
  373.     /**
  374.       * returns if the current user is admin of the category
  375.       * (is group member of MEC_ADMINGRP or has higher rights)
  376.       *
  377.       * @param boolean $debug 
  378.       *
  379.       * @returns boolean
  380.       *
  381.       * @since pk-06-11-13
  382.       *
  383.       ***/
  384.     function curUserIsAdmin($debug=FALSE{
  385.         if ($debugechoDebugMethod(__FILE__,get_class($this),"MED_CATEGORY_NODE::curUserIsAdmin()");
  386.  
  387.         if (intval($this->getDBField('MEC_ADMINGRP'))) {
  388.             return ($GLOBALS['USER']->isGroupMember(intval($this->getDBField('MEC_ADMINGRP'))));
  389.         }
  390.         return FALSE;
  391.     }
  392.  
  393.     /**
  394.       * returns if current user can add subcategories or files (checks MEC_UPLOADGRP)
  395.       *
  396.       * @param boolean $debug 
  397.       *
  398.       * @returns boolean
  399.       *
  400.       * @since pk-04-08-13
  401.       * @version pk-06-03-05
  402.       *
  403.       ***/
  404.     function userCanAdd($debug=FALSE{
  405.         if ($debugechoDebugMethod(__FILE__,get_class($this),"MED_CATEGORY_NODE::userCanAdd()","ID: ".$this->getId(),0);
  406.         if ($GLOBALS['USER']->isAdmin())                            return TRUE;
  407.         if ($GLOBALS['USER']->isGroupMember($this->MEC_ADMINGRP))   return TRUE;
  408.         if ($GLOBALS['USER']->isGroupMember($this->MEC_UPLOADGRP))  return TRUE;
  409.         if (intval($this->PROJ_ID&& ($GLOBALS['USER']->isProjectAdmin($this->PROJ_ID))) return TRUE;
  410.         return FALSE;
  411.     }
  412.  
  413.  
  414.     // #############################################
  415.     //  show checks
  416.     // #############################################
  417.  
  418.     /**
  419.       * returns the state id of the node (MEC_STATE)
  420.       *
  421.       * @returns int
  422.       *
  423.       * @since pk-06-11-13
  424.       *
  425.       ***/
  426.     function getStateId({
  427.         return $this->getDBField('MEC_STATE');
  428.     }
  429.  
  430.     /**
  431.       * checks if the node is on
  432.       *
  433.       * @param boolean $checkState 
  434.       * @param boolean $checkDate 
  435.       * @param boolean $checkGroup 
  436.       * @param boolean $debug 
  437.       *
  438.       * @returns bool
  439.       *
  440.       * @version pk-06-11-13
  441.       *
  442.       ***/
  443.     function isToShow($checkState=TRUE,$checkDate=TRUE,$checkGroup=TRUE,$debug=FALSE{
  444.         if ($debugechoDebugMethod(__FILE__,get_class($this),"MED_CATEGORY_NODE::isToShow(...)",$this->getTitle());
  445.  
  446.         if ($checkState && is_array($GLOBALS['OCSP_MED']['CATSTATES'][$this->getStateId()])) {
  447.             if (!$GLOBALS['OCSP_MED']['CATSTATES'][$this->getStateId()]['SHOW']{
  448.                 if ($debugechoDebug(__FILE__,"<p>state not to show</p>");
  449.                 return FALSE;
  450.             }
  451.         }
  452.  
  453.         if (($checkDate&& ($this->checkShowDate($debug!= 1)) {
  454.             if ($debugechoDebug(__FILE__,"<p>outdated</p>");
  455.             return FALSE;
  456.         }
  457.  
  458.         if (($checkGroup&& (!$this->curUserCanOpen())) {
  459.             if ($debugechoDebug(__FILE__,"<p>no rights</p>");
  460.             return FALSE;
  461.         }
  462.         return TRUE;
  463.     }
  464.  
  465.     /**
  466.       * checks if the node has children which are on
  467.       *
  468.       * @param boolean $checkState 
  469.       * @param boolean $checkDate 
  470.       * @param boolean $checkGroup 
  471.       * @param boolean $debug 
  472.       *
  473.       * @returns bool
  474.       *
  475.       * @version pk-06-11-13
  476.       *
  477.       ***/
  478.     function hasChildrenToShow($checkState=TRUE,$checkDate=TRUE,$checkGroup=TRUE,$debug=FALSE){
  479.         if ($debugechoDebugMethod(__FILE__,get_class($this),"MED_CATEGORY_NODE::hasChildrenToShow(...)",$this->getTitle());
  480.  
  481.         if ((time()-$this->myChildCacheTTL$this->hasChildrenToShowTS{
  482.             if ($debugechoDebug(__FILE,"<blockquote>Cached</blockquote>\n");
  483.             return TRUE;
  484.         }
  485.  
  486.         if ($childArr=$this->getChildrenList(TRUE,$debug,$filtered)) {
  487.             foreach($childArr as $child{
  488.                 if ($child->isToShow($checkState,$checkDate,$checkGroup,$debug)) {
  489.                     $this->hasChildrenToShowTS=time();
  490.                     if ($debugechoDebug(__FILE__,"<blockquote style=\"background-color: green;\">MED_CATEGORY_NODE::hasChildrenToShow returns TRUE</blockquote>");
  491.                     return TRUE;
  492.                 }
  493.             }
  494.         }
  495.         return FALSE;
  496.     }
  497.  
  498.     /**
  499.       * checks if the menu is to show from date
  500.       *
  501.       * return values are:
  502.       *
  503.       * - 1: yes
  504.       * - 0: showtill is outdate
  505.       * - -2: showfrom is not reached
  506.       *
  507.       * @returns int
  508.       *
  509.       * @since pk-06-11-13
  510.       *
  511.       ***/
  512.     function checkShowDate($debug=FALSE{
  513.         if ($debugechoDebugMethod(__FILE__,get_class($this),"MED_CATEGORY_NODE::checkShowDate()");
  514.         require_once __OCSP_PHPINCPATH__."common/pcf_Date.phpclass";
  515.  
  516.         $now=new pcf_Date(time());
  517.         $aDate=new pcf_Date();
  518.         $aDate->setDateStr($this->MEC_SHOWFROM);
  519.         if ($aDate->getLinuxTime($now->getLinuxTime()) return -2;
  520.  
  521.         $aDate->setDateStr($this->MEC_SHOWTILL);
  522.         if ($aDate->getLinuxTime($now->getLinuxTime()) return 0;
  523.  
  524.         return 1;
  525.     }
  526.  
  527.     // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  528.     // media directory methods
  529.     // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  530.  
  531.     /**
  532.       * returns the root dir of this category
  533.       * pk-07-02-23:
  534.       * - if none has been set till now (MEC_PATH is empty) and we have an id a new one is set
  535.       * - replace MEC_PATH absolute pathes with template values for
  536.       * -- $GLOBALS[OCSP_MED][ROOTPATH]
  537.       * -- $GLOBALS[PROJECT][PATH]
  538.       * -- $_SERVER[DOCUMENT_ROOT]
  539.       *  by calling setRootDir()
  540.       *
  541.       * @param bool $debug 
  542.       *
  543.       * @return string (NULL in case of an error)
  544.       *
  545.       * @version pk-07-02-23
  546.       *
  547.       ***/
  548.     function getRootDir($debug=FALSE{
  549.         if ($debug{
  550.             echoDebugMethod(__FILE__,get_class($this),"MED_CATEGORY_NODE::getRootDir()");
  551.         }
  552.  
  553.         $s_mecPath=trim($this->getDBField('MEC_PATH'));
  554.         if (strlen($s_mecPath)>2// avoid "/" to be checked
  555.             if (strstr($s_mecPath,"$")) {
  556.                 // we have a template -> parse it
  557.                 require_once($GLOBALS['PROJECT']['PHPINCPATH']."common/pcf_templates.phpinc");
  558.                 $s_mecPath=pcf_tmpl_parse($s_mecPath,$this->getDBVal());
  559.             }
  560.             if (substr($s_mecPath,-1== "/"$s_mecPath=substr($s_mecPath,0,-1)// cut final slash as it will be returned later
  561.  
  562.             if (!is_dir($s_mecPath)) {
  563.                 // the template failed or we do not have a template
  564.                 if (is_dir($GLOBALS['OCSP_MED']['ROOTPATH'].$s_mecPath)) {
  565.                     $s_mecPath=$GLOBALS['OCSP_MED']['ROOTPATH'].$s_mecPath;
  566.                     if (!($this->setRootDir($s_mecPath,FALSE,FALSE,$debug))) {
  567.                         // ensure we use a template to be able to copy the database to another location by calling setRootDir
  568.                         // if this fails we are in truble
  569.                         if ($debugechoDebug(__FILE__,"<p style=\"padding-left:20px;\">ERROR setting RootDir from MEDIA ROOT <br/>to $s_mecPath</p>");
  570.                         return NULL;
  571.                     }
  572.                 else if (is_dir($GLOBALS['PROJECT']['PATH'].$s_mecPath)) {
  573.                     $s_mecPath=$GLOBALS['PROJECT']['PATH'].$s_mecPath;
  574.                     if (!($this->setRootDir($s_mecPath,FALSE,FALSE,$debug))) {
  575.                         // ensure we use a template to be able to copy the database to another location by calling setRootDir
  576.                         // if this fails we are in truble
  577.                         if ($debugechoDebug(__FILE__,"<p style=\"padding-left:20px;\">ERROR setting RootDir from PROJECT-PATH <br/>to $s_mecPath</p>");
  578.                         return NULL;
  579.                     }
  580.                 else if (is_dir($_SERVER['DOCUMENT_ROOT']."/".$s_mecPath)) {
  581.                     $s_mecPath=$_SERVER['DOCUMENT_ROOT']."/".$s_mecPath;
  582.                     if (!($this->setRootDir($s_mecPath,FALSE,FALSE,$debug))) {
  583.                         // ensure we use a template to be able to copy the database to another location by calling setRootDir
  584.                         // if this fails we are in truble
  585.                         if ($debugechoDebug(__FILE__,"<p style=\"padding-left:20px;\">ERROR setting RootDir from DOCUMENT_ROOT <br/>to $s_mecPath</p>");
  586.                         return NULL;
  587.                     }
  588.                 else {
  589.                     if ($debugechoDebug(__FILE__,"<p style=\"padding-left:20px;\">ERROR ROOTDIR not found</p>");
  590.                 }
  591.             }
  592.  
  593.         else {
  594.             // we do not have a root dir set right now
  595.             if (!empty($GLOBALS['OCSP_MED']['ROOTPATH']&& intval($this->getId())) {
  596.                 // we can create the dir
  597.                 require_once($GLOBALS['PROJECT']['PHPINCPATH']."common/pcf_templates.phpinc");
  598.                 $GLOBALS['OCSP_MED']['ROOTPATH']=trim($GLOBALS['OCSP_MED']['ROOTPATH']);
  599.                 if (substr(trim($GLOBALS['OCSP_MED']['ROOTPATH']),-1!= "/"$GLOBALS['OCSP_MED']['ROOTPATH'].="/";
  600.                 $s_mecPath=pcf_tmpl_parse($GLOBALS['OCSP_MED']['ROOTPATH'].$this->getId());
  601.             else {
  602.                 $s_mecPath=$GLOBALS['PROJECT']['PATH']."media/".$this->getId();
  603.             }
  604.  
  605.             if (!$this->setRootDir($s_mecPath,TRUE,FALSE,$debug)) {
  606.                 return NULL;
  607.             }
  608.         }
  609.         $s_mecPath=trim($s_mecPath);
  610.         if (substr($s_mecPath,-1!= "/"$s_mecPath.="/";
  611.  
  612.         if (is_dir($s_mecPath)) {
  613.             return $s_mecPath;
  614.         else {
  615.             return NULL;
  616.         }
  617. /*
  618.  
  619.         if (strstr($this->MEC_PATH,"$") !== FALSE) {
  620.             if
  621.             require_once($GLOBALS['PROJECT']['PHPINCPATH']."common/pcf_templates.phpinc");
  622.             $ret = pcf_tmpl_parse($this->MEC_PATH,$this->getDBVal(),$debug);
  623.         } else {
  624.             if (is_dir($this->MEC_PATH)) {
  625.  
  626.             }
  627.             if (!strstr($this->MEC_PATH,$GLOBALS['MED']['ROOTPATH'])) {
  628.                 $ret = $GLOBALS['MED']['ROOTPATH'].$this->MEC_PATH;
  629.             } else {
  630.                 $ret=$this->MEC_PATH;
  631.             }
  632.         }
  633.  
  634.         $ret=trim(str_replace("//","/",$ret));
  635.         if (substr($ret,-1) != "/") $ret.="/";
  636.         if ($debug) echo "<blockquote>Returns: $ret</blockquote>";
  637.         return $ret;
  638. */
  639.     }
  640.  
  641.     /**
  642.       * sets the root dir an returns if the
  643.       * directory exists
  644.       *
  645.       * @param string $absPath including Project Root
  646.       * @param boolean $create create the directory if not exists
  647.       * @param boolean $noCheck 
  648.       * @param boolean $debug 
  649.       *
  650.       * @return int 
  651.       *
  652.       * @since pk-05-10-13
  653.       * @version pk-07-02-24
  654.       *
  655.       ***/
  656.     function setRootDir($absPath,$create=FALSE,$noCheck=FALSE,$debug=FALSE{
  657.         if ($debugecho "<p><b>MED_CATEGORY_NODE::setRootDir($absPath,".($create "CREATE" "").")</b> (".get_class($this)."</p>\n";
  658.  
  659.         $absPath=trim($absPath);
  660.         if (empty($absPath|| ($absPath=="/")) return FALSE// error no path
  661.  
  662.         $absPath=pcf_checkDirName($absPath,TRUE)// make a valid dir name
  663.  
  664.         if (is_dir($absPath)) {
  665.             // every thing seams ok
  666.             // replace pathes by global settings
  667.  
  668.             $s_mecPath=str_replace($GLOBALS['OCSP_MED']['ROOTPATH'],"\$@OCSP_MED|ROOTPATH\$",$absPath);
  669.             $s_mecPath=str_replace($GLOBALS['PROJECT']['PATH'],"\$@PROJECT|PATH\$",$s_mecPath);
  670.             $s_mecPath=str_replace($_SERVER['DOCUMENT_ROOT'],"\$�DOCUMENT_ROOT\$",$s_mecPath);
  671.             $this->setDBField('MEC_PATH',$s_mecPath);
  672.  
  673.             if ($debugechoDebug(__FILE__,"<blockquote><p>DIR (".$s_mecPath.")<br />EXISTS -&gt; RETURN TRUE</p></blockquote>\n");
  674.             return TRUE;
  675.         }
  676.  
  677.         if ($create{
  678.             if (pcf_mkdirs($absPath0775$debug)) {
  679.                 return $this->setRootDir($absPath,FALSE,$noCheck,$debug);
  680.             }
  681.         }
  682.  
  683.         if ($noCheck && (strlen($absPath1)) // avoid "/" with strlen(>1)
  684.             $s_mecPath=str_replace($GLOBALS['OCSP_MED']['ROOTPATH'],"\$@OCSP_MED|ROOTPATH\$",$absPath);
  685.             $s_mecPath=str_replace($GLOBALS['PROJECT']['PATH'],"\$@PROJECT|PATH\$",$s_mecPath);
  686.             $s_mecPath=str_replace($_SERVER['DOCUMENT_ROOT'],"\$�DOCUMENT_ROOT\$",$s_mecPath);
  687.             $this->setDBField('MEC_PATH',$s_mecPath);
  688.             if ($debugechoDebug(__FILE__,"<blockquote><p>DIR (".$s_mecPath.")<br /><b>DOES NOT EXISTS</b> -&gt; RETURN TRUE</p></blockquote>\n");
  689.             return FALSE// as the dir does not exist otherwise is_dir($absPath) would have been tree
  690.         }
  691.  
  692.         return FALSE;
  693.     }
  694.  
  695.  
  696.  
  697.     /**
  698.       * returns the path where to place upload files
  699.       *
  700.       * @param bool $debug 
  701.       *
  702.       * @return string 
  703.       *
  704.       ***/
  705.     function getUploadDir($debug=FALSE{
  706.         $aDir str_replace('//','/',$this->getRootDir().$GLOBALS['MED']['UPLOADTMP']);
  707.         pcf_checkDir($aDir,TRUE,0770);
  708.         return $aDir;
  709.     }
  710.  
  711.  
  712.     // #############################################
  713.     //  MIME_TYPE METHODS
  714.     // #############################################
  715.  
  716.  
  717.     // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  718.     // media file methods
  719.     // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  720.  
  721.     /**
  722.       * returns a file name which is not used
  723.       * and save to use
  724.       *
  725.       * @param string $fileName 
  726.       * @param bool $debug 
  727.       *
  728.       * @return string 
  729.       *
  730.       ***/
  731.     function getNewFileName($fileName,$debug=FALSE{
  732.         if ($debugecho "<p><b>MED_CATEGORY_NODE::getNewFileName($fileName,$debug)</b></p><blockquote>";
  733.         $fileName=pcf_checkFileName($fileName);
  734.         $retName=$fileName;
  735.         $i=0;
  736.         while (pcf_fileExists($retName)) {
  737.             $nameParts=expolode(".",$fileName);
  738.             if (is_array($nameParts)) {
  739.                 $retName=$nameParts[0]."_".$i.".".$nameParts[1];
  740.             else {
  741.                 $retName=$fileName."_".$i;
  742.             }
  743.             $i++;
  744.         }
  745.         return $retName;
  746.     }
  747.  
  748.     /**
  749.       * function adds a file to the category
  750.       *
  751.       * @param string $file path to the file
  752.       * @param bool $asLink link the file instead of copy it to the media directory
  753.       * @param bool $debug 
  754.       *
  755.       * @return MED_FILE 
  756.       *
  757.       * @todo handle add if $file not already in media dir
  758.       *
  759.       ***/
  760.     function addNewFile($file,$asLink=FALSE,$debug=FALSE{
  761.         if ($debugecho "<p><b>MED_CATEGORY_NODE::addNewFile($file,$asLink,$debug)</b></p><blockquote>";
  762.         require_once $GLOBALS['MED']['PHPINCPATH']."MED_FILE.phpclass";
  763.         $aName=basename($file);
  764.         $fa=explode(".",$aName);
  765.         if (is_array($fa)) $ext=$fa[sizeof($fa)-1]else $ext=""// just the last elem in the array
  766.         if ((!empty($ext)) && (!is_array($this->tmpMimeClasses[$ext]))) {
  767.             $query="SELECT * FROM T_MED_MIMETYPES WHERE MIM_EXTENSIONS LIKE ".$GLOBALS[$this->get_gDBIDX()]->qs_getSlashedValue("%".$ext."%");
  768.             if ($debugecho "<p>$query</p>";
  769.             $this->tmpMimeClasses[$ext]=$GLOBALS[$this->get_gDBIDX()]->quickQuery($query);
  770.         }
  771.         if ($GLOBALS['MED']['OBJECTCLASS'][$this->tmpMimeClasses[$ext]['MIM_OBJCLASS']]{
  772.             require_once $GLOBALS['MED']['OBJECTCLASS'][$this->tmpMimeClasses[$ext]['MIM_OBJCLASS']]['CLASSFILE'];
  773.             $cmd="\$fileObj=new ".$this->tmpMimeClasses[$ext]['MIM_OBJCLASS']."(0,\"".$this->tmpMimeClasses[$ext]['MIM_MIMETYPE']."\",\$debug);";
  774.             if ($debugecho "<pre>ObjCmd: \n$cmd</pre>";
  775.             eval($cmd);
  776.         else {
  777.             if ($debug{echo "<p>";print_r($this->tmpMimeClasses[$ext]);echo "</p>";}
  778.             $fileObj=new MED_FILE(0,$this->tmpMimeClasses[$ext]['MIM_MIMETYPE'],$debug);
  779.         }
  780.  
  781.         if (is_object($fileObj)) {
  782.             $fileObj->setDefaults();
  783.             $fileObj->setCatId($this->getId());
  784.             $fileObj->setMIMEType($this->tmpMimeClasses[$ext]['MIM_ID']);
  785.  
  786.             if (file_exists($this->getRootDir().$aName)) {
  787.                 if ($debugecho "<p>FILE $aName ALLREADY IN DIRECTORY</p>";
  788.             else {
  789.                 if ($asLink{
  790.                     if (!symlink($file,$this->getRootDir().$aName)) return NULL;
  791.                 else {
  792.                     if (!copy($file,$this->getRootDir().$aName)) return NULL;
  793.                 }
  794.                 $file=$this->getRootDir().$aName;
  795.             }
  796.             $fileObj->setName(basename($aName));
  797.             $fileObj->setCatId($this->MEC_ID);
  798.             $fileObj->setFile($file,FALSE,$debug);
  799.             $fileObj->dbSave($this->get_gDBIDX(),$debug);
  800.             if ($debugecho "</blockquote>";
  801.             return $fileObj;
  802.         }
  803.  
  804.         if ($debugecho "<p>RETURNS NULL</p></blockquote>";
  805.         return NULL;
  806.  
  807.     }
  808.  
  809.     /**
  810.       * adds a file submitted with post
  811.       *
  812.       * @param array $uplFArr 
  813.       * @param boolean $debug 
  814.       * @param boolean $autoSave if true the object is stored to the db
  815.       *
  816.       * @return MED_FILE 
  817.       *
  818.       * @since pk-05-09-09
  819.       *
  820.       * @var MED_FILE $o_mefObj 
  821.       * @var string $s_fName 
  822.       ***/
  823.     function addUploadFile($uplFArr,$debug=FALSE,$autoSave=FALSE{
  824.         if ($debugecho "<p><b>MED_CATEGORY_NODE::addUploadFile()</b> (".get_class($this).")</p><blockquote><pre>".print_r($uplFArr,TRUE)."</pre></blockquote>";
  825.  
  826.         if (!is_uploaded_file($uplFArr['tmp_name'])) {
  827.             // see http://www.php.net/manual/en/function.is-uploaded-file.php
  828.             if ($debugecho "<p>ERROR: </p><pre>".print_r($uplFArr,TRUE)."</pre><p>is not uploaded</p>";
  829.             return NULL;
  830.         }
  831.  
  832.         require_once dirname(__FILE__)."/MED_FILE.phpclass";
  833.  
  834.         $o_mefObj=new MED_FILE(0,$uplFArr['type'],$debug);
  835.         $o_mefObj->setDefaults();
  836.         $o_mefObj->setCatId($this->getId());
  837.  
  838.         $s_fName=pcf_checkFileName(basename($uplFArr['name']),"_")// ($fileName,$replChar="_")
  839.         $s_oriFName=$s_fName;
  840.  
  841.         $i=0;
  842.         while(file_exists($this->getRootDir().$s_fName)) {
  843.             $s_fName=sprintf("%04d%s",$i,"_".$s_oriFName);
  844.             $i++;
  845.         }
  846.         if (!move_uploaded_file ($uplFArr['tmp_name'],$this->getRootDir().$s_fName)) return NULL;
  847.  
  848.         if ($o_mefObj->setFile($s_fName,FALSE,$debug)) // ($aName,$isTmp=FALSE,$debug=FALSE)
  849.             $o_mefObj->setName(basename($s_fName));
  850.             $o_ret=$o_mefObj->chkObjectClass($debug);
  851.             if ($autoSave$o_ret->dbSave();
  852.             return $o_ret;
  853.         }
  854.  
  855.         if ($debugecho "<p>RETURNS NULL</p></blockquote>";
  856.         return NULL;
  857.     }
  858.  
  859.  
  860.     /**
  861.       * adds a sub directory of $this->getRootDir() as child to the node
  862.       * all files in the direcotry are added to files
  863.       *
  864.       * @param string $dirName 
  865.       * @param string $childName 
  866.       * @param boolean $debug 
  867.       *
  868.       * @returns int childId
  869.       *
  870.       * @since pk-04-08-09
  871.       *
  872.       ***/
  873.     function addSubDirAsChild($dirName,$childName="",$debug=FALSE{
  874.         if ($debugecho "<p><b>MED_CATEGORY_NODE::addSubDirAsChild($dirName,$childName ..)</b> (".get_class($this).")</p>";
  875.  
  876.         if (!is_dir($this->getRootDir().$dirName)) return FALSE;
  877.  
  878.         $child=new MED_CATEGORY_NODE();
  879.         $child->MEC_PARENT=$this->getId();
  880.         $child->MEC_PATH=$this->getRootDir().$dirName."/";
  881.         $child->MEC_WEBDIR=$this->MEC_WEBDIR.$dirName."/";
  882.         if (empty($childName)) {
  883.             $child->MEC_NAME=$dirName;
  884.         else {
  885.             $child->MEC_NAME=$childName;
  886.         }
  887.         $child->dbSave($this->gDBIDX,$debug);
  888.         $child->checkDir(TRUE,$debug);
  889.         return $child->getId();
  890.     }
  891.  
  892.     /**
  893.       * scans the category directory for files not in the database
  894.       * if $autoAdd the files are added to the database
  895.       * a array with ['NEW'] and ['BROKEN']
  896.       *
  897.       * @param bool $autoAdd 
  898.       * @param bool $debug 
  899.       *
  900.       * @return array 
  901.       *
  902.       * @todo only unique key in db prevents from inserting the same file twice
  903.       *
  904.       ***/
  905.     function checkDir($autoAdd=FALSE,$debug=TRUE{
  906.         if ($debugecho "<p><b>MED_CATEGORY_NODE::checkDir($autoAdd,$debug)</b></p><blockquote>";
  907.         $retArr=array('NEW'=>array(),'BROKEN'=>array());
  908.         $rootDir=$this->getRootDir($debug);
  909.         $fileList=pcf_getDirList($rootDir,"FL",$debug);
  910.         $query="SELECT * FROM T_MED_FILES WHERE MEC_ID=".$this->getId();
  911.         if ($cursor=$GLOBALS[$this->get_gDBIDX()]->query($query)) {
  912.             while($row=$cursor->fetchArrayFld()) {
  913.                 if ($key=array_search(basename($row['MEF_FILE']),$fileList)) {
  914.                     unset($fileList[$key]);
  915.                 else {
  916.                     $retArr['BROKEN'][]=$row;
  917.                 }
  918.             }
  919.         }
  920.         $retArr['NEW']=$fileList;
  921.         foreach($fileList as $file{
  922.             $this->addNewFile($this->getRootDir().$file,FALSE,$debug);
  923.         }
  924.         if ($debugecho "</blockquote>";
  925.         return $retArr;
  926.     }
  927.  
  928.     // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  929.     // standard IO methods
  930.     // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  931.  
  932.     /**
  933.       * echos a the row in a table  OVERRIDE IN SUB CLASS to change layout
  934.       *
  935.       * @param  bool    $debug 
  936.       *
  937.       ***/
  938.     function writeTableRowThis($debug{
  939.         echo "<tr><td>";
  940.         echo "<a href='".$this->getLinkUrl($debug)."'>";
  941.         echo $this->getDBField('MEC_NAME');
  942.         echo "</a></td></tr>";
  943.     }
  944.  
  945.     /**
  946.       * echos a tree intend called by write children
  947.       *
  948.       * @param  int     $expandLevel 
  949.       * @param  bool    $useChace 
  950.       * @param  bool    $debug 
  951.       *
  952.       ***/
  953.     function writeTableRowChildren($expandLevel=0,$useCache=TRUE,$debug=FALSE{
  954.         if ((!$useCache|| ((time($this->myChildrenPopulateTS$this->myChildCacheTTL)) {
  955.             $this->populateChildren($useCache,$debug);
  956.         }
  957.         if (sizeof($this->myChildren)) {
  958.             echo "<tr><td><table width='90%' align='right' border='0'>";
  959.             foreach($this->myChildren as $orderNr => $id{
  960.                 $GLOBALS['DBMS_TREE'][$this->getGlobalTreeKey()][$id]->writeTableRowThis($debug);
  961.                 if ($expandLevel 0{
  962.                     $GLOBALS['DBMS_TREE'][$this->getGlobalTreeKey()][$id]->writeTableRowChildren($expandLevel-1,$useCache,$debug);
  963.                 }
  964.             }
  965.             echo "</table></td></tr>";
  966.         }
  967.     }
  968.  
  969.  
  970.     // #############################################
  971.     //  CMS METHODS
  972.     // #############################################
  973.  
  974.     /**
  975.       * @returns int
  976.       * @since pk-06-11-13
  977.       ***/
  978.     function getProjId({
  979.         return intval($this->getDBField('PROJ_ID'));
  980.     }
  981.  
  982. }
  983. ?>

Documentation generated on Thu, 08 Jan 2009 17:44:47 +0100 by phpDocumentor 1.4.0a2