phpgroupware-cvs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Phpgroupware-cvs] CVS: phpgwapi/inc class.hooks.inc.php,1.10,1.10.4.1


From: Ralf Becker <address@hidden>
Subject: [Phpgroupware-cvs] CVS: phpgwapi/inc class.hooks.inc.php,1.10,1.10.4.1
Date: Sun, 27 Apr 2003 17:31:54 -0400

Update of /cvsroot/phpgroupware/phpgwapi/inc
In directory subversions:/tmp/cvs-serv6013

Modified Files:
      Tag: Version-0_9_16-branch
        class.hooks.inc.php 
Log Message:
new type of hooks via methodes instead of separate files:
- allows to parse arguments ot the hook and return content
- all access to the hook-table is now handled by the hook-class (for admin and 
setup too)
- all existing hooks continue to work of cause

Index: class.hooks.inc.php
===================================================================
RCS file: /cvsroot/phpgroupware/phpgwapi/inc/class.hooks.inc.php,v
retrieving revision 1.10
retrieving revision 1.10.4.1
diff -C2 -r1.10 -r1.10.4.1
*** class.hooks.inc.php 2 Jan 2002 14:33:03 -0000       1.10
--- class.hooks.inc.php 27 Apr 2003 21:31:52 -0000      1.10.4.1
***************
*** 36,46 ****
        {
                var $found_hooks = Array();
!               function hooks()
                {
!                       //$GLOBALS['phpgw']->db->query("SELECT hook_appname, 
hook_location, hook_filename FROM phpgw_hooks WHERE 
hook_location='".$location."'",__LINE__,__FILE__);
!                       $GLOBALS['phpgw']->db->query("SELECT hook_appname, 
hook_location, hook_filename FROM phpgw_hooks",__LINE__,__FILE__);
!                       while( $GLOBALS['phpgw']->db->next_record() )
                        {
!                               
$this->found_hooks[$GLOBALS['phpgw']->db->f('hook_appname')][$GLOBALS['phpgw']->db->f('hook_location')]
 = $GLOBALS['phpgw']->db->f('hook_filename');
                        }
                        //echo '<pre>';
--- 36,49 ----
        {
                var $found_hooks = Array();
!               var $db = '';
! 
!               function hooks($db='')
                {
!                       $this->db = $db ? $db : $GLOBALS['phpgw']->db;  // this 
is to allow setup to set the db
! 
!                       $this->db->query("SELECT hook_appname, hook_location, 
hook_filename FROM phpgw_hooks",__LINE__,__FILE__);
!                       while( $this->db->next_record() )
                        {
!                               
$this->found_hooks[$this->db->f('hook_appname')][$this->db->f('hook_location')] 
= $this->db->f('hook_filename');
                        }
                        //echo '<pre>';
***************
*** 51,137 ****
                /*!
                @function process
!               @abstract loads up all the hooks the user has rights to
!               @discussion Someone flesh this out please
                */
!               // Note: $no_permission_check should *ONLY* be used when it 
*HAS* to be. (jengo)
!               function process($location, $order = '', $no_permission_check = 
False)
                {
!                       $SEP = filesystem_separator();
                        if ($order == '')
                        {
!                               settype($order,'array');
!                               $order[] = 
$GLOBALS['phpgw_info']['flags']['currentapp'];
                        }
  
                        /* First include the ordered apps hook file */
!                       reset ($order);
!                       while (list(,$appname) = each($order))
                        {
!                               if 
(isset($this->found_hooks[$appname][$location]))
!                               {
!                                       $f = PHPGW_SERVER_ROOT . $SEP . 
$appname . $SEP . 'inc' . $SEP . $this->found_hooks[$appname][$location];
!                                       if (file_exists($f) &&
!                                               ( 
$GLOBALS['phpgw_info']['user']['apps'][$appname] || (($no_permission_check || 
$appname == 'preferences') && $appname)) )
!                                       {
!                                               include($f);
!                                       }
!                               }
!                               $completed_hooks[$appname] = True;
                        }
  
                        /* Then add the rest */
- 
                        if ($no_permission_check)
                        {
!                               reset($GLOBALS['phpgw_info']['apps']);
!                               while (list(,$p) = 
each($GLOBALS['phpgw_info']['apps']))
!                               {
!                                       $appname = $p['name'];
!                                       if (! isset($completed_hooks[$appname]) 
|| $completed_hooks[$appname] != True)
!                                       {
!                                               if 
(isset($this->found_hooks[$appname][$location]))
!                                               {
!                                                       $f = PHPGW_SERVER_ROOT 
. $SEP . $appname . $SEP . 'inc' . $SEP . 
$this->found_hooks[$appname][$location];
!                                                       if (file_exists($f))
!                                                       {
!                                                               include($f);
!                                                       }
!                                               }
!                                       }
!                               }
                        }
                        else
                        {
!                               reset ($GLOBALS['phpgw_info']['user']['apps']);
!                               while (list(,$p) = 
each($GLOBALS['phpgw_info']['user']['apps']))
                                {
!                                       $appname = $p['name'];
!                                       if (! isset($completed_hooks[$appname]) 
|| $completed_hooks[$appname] != True)
!                                       {
!                                               if 
(isset($this->found_hooks[$appname][$location]))
!                                               {
!                                                       $f = PHPGW_SERVER_ROOT 
. $SEP . $appname . $SEP . 'inc' . $SEP . 
$this->found_hooks[$appname][$location];
!                                                       if (file_exists($f))
!                                                       {
!                                                               include($f);
!                                                       }
!                                               }
!                                       }
                                }
                        }
                }
  
                /*!
                @function single
!               @abstract call the hooks for a single application
!               @param $location hook location - required
!               @param $appname application name - optional
                */
!               // Note: $no_permission_check should *ONLY* be used when it 
*HAS* to be. (jengo)
!               function single($location, $appname = '', $no_permission_check 
= False)
                {
!                       if (! $appname)
                        {
!                               $appname = 
$GLOBALS['phpgw_info']['flags']['currentapp'];
                        }
                        $SEP = filesystem_separator();
--- 54,130 ----
                /*!
                @function process
!               @abstract executes all the hooks (the user has rights to) for a 
given location 
!               @syntax process($args,$order='',$no_permission_check = False)
!               @param $args location-name as string or array:
!               @param $args['location'] location-name
!               @param $order or $args['order'] array of appnames (as value), 
which should be executes first
!               @param $args is passed to the hook, if its a new method-hook
!               @param $no_permission_check if True execute all hooks, not only 
the ones a user has rights to
!               @note $no_permission_check should *ONLY* be used when it *HAS* 
to be. (jengo)
!               @returns array with results of each hook call (with appname as 
key): \
!                       False if no hook exists, True if old hook exists \
!                       and whatever the new methode-hook returns (can be True 
or False too!).
                */
!               function process($args, $order = '', $no_permission_check = 
False)
                {
!                       //echo "<p>hooks::process("; print_r($args); echo 
")</p>\n";
                        if ($order == '')
                        {
!                               $order = is_array($args) && 
isset($args['order']) ? $args['order'] : 
!                                       
array($GLOBALS['phpgw_info']['flags']['currentapp']);
                        }
  
                        /* First include the ordered apps hook file */
!                       foreach($order as $appname)
                        {
!                               $results[$appname] = 
$this->single($args,$appname,$no_permission_check);
                        }
  
                        /* Then add the rest */
                        if ($no_permission_check)
                        {
!                               $apps = $GLOBALS['phpgw_info']['apps'];
                        }
                        else
                        {
!                               $apps = $GLOBALS['phpgw_info']['user']['apps'];
!                       }
!                       foreach($apps as $app)
!                       {
!                               $appname = $app['name'];
!                               if (!isset($results[$appname]))
                                {
!                                       $results[$appname] = 
$this->single($args,$appname,$no_permission_check);
                                }
                        }
+                       return $results;
                }
  
                /*!
                @function single
!               @abstract executes a single hook of a given location and 
application
!               @syntax single($args,$appname='',$no_permission_check = False)
!               @param $args location-name as string or array:
!               @param $args['location'] location-name
!               @param $appname or $args['appname'] name of the app, which's 
hook to execute, if empty the current app is used
!               @param $args is passed to the hook, if its a new method-hook
!               @param $no_permission_check if True execute all hooks, not only 
the ones a user has rights to
!               @note $no_permission_check should *ONLY* be used when it *HAS* 
to be. (jengo)
!               @returns False if no hook exists, True if an old hook exist and 
whatever the new method-hook returns
                */
!               function single($args, $appname = '', $no_permission_check = 
False)
                {
!                       //echo "<p>hooks::single("; print_r($args); echo 
",'$appname')</p>\n";
!                       if (is_array($args))
!                       {
!                               $location = $args['location'];
!                       }
!                       else
!                       {
!                               $location = $args;
!                       }
!                       if (!$appname)
                        {
!                               $appname = is_array($args) && 
isset($args['appname']) ? $args['appname'] : 
$GLOBALS['phpgw_info']['flags']['currentapp'];
                        }
                        $SEP = filesystem_separator();
***************
*** 140,153 ****
                        if (isset($this->found_hooks[$appname][$location]))
                        {
!                               $f = PHPGW_SERVER_ROOT . $SEP . $appname . $SEP 
. 'inc' . $SEP . $this->found_hooks[$appname][$location];
!                               if (file_exists($f) &&
!                                       ( 
$GLOBALS['phpgw_info']['user']['apps'][$appname] || (($no_permission_check || 
$location == 'config' || $appname == 'phpgwapi') && $appname)) )
                                {
!                                       include($f);
!                                       return True;
                                }
!                               else
                                {
!                                       return False;
                                }
                        }
--- 133,155 ----
                        if (isset($this->found_hooks[$appname][$location]))
                        {
!                               $parts = explode('.',$method = 
$this->found_hooks[$appname][$location]);
!                               
!                               if (count($parts) != 3 || ($parts[1] == 'inc' 
&& $parts[2] == 'php'))
                                {
!                                       $f = PHPGW_SERVER_ROOT . $SEP . 
$appname . $SEP . 'inc' . $SEP . $method;
!                                       if (file_exists($f) &&
!                                               ( 
$GLOBALS['phpgw_info']['user']['apps'][$appname] || (($no_permission_check || 
$location == 'config' || $appname == 'phpgwapi') && $appname)) )
!                                       {
!                                               include($f);
!                                               return True;
!                                       }
!                                       else
!                                       {
!                                               return False;
!                                       }
                                }
!                               else    // new style method-hook
                                {
!                                       return ExecMethod($method,$args);
                                }
                        }
***************
*** 165,173 ****
                {
                        $count = 0;
!                       reset($GLOBALS['phpgw_info']['user']['apps']);
!                       $SEP = filesystem_separator();
!                       while ($permission = 
each($GLOBALS['phpgw_info']['user']['apps']))
                        {
!                               if 
(isset($this->found_hooks[$permission[0]][$location]))
                                {
                                                ++$count;
--- 167,173 ----
                {
                        $count = 0;
!                       foreach($GLOBALS['phpgw_info']['user']['apps'] as 
$appname => $data)
                        {
!                               if 
(isset($this->found_hooks[$appname][$location]))
                                {
                                                ++$count;
***************
*** 177,181 ****
                }
                
-               
                /*! 
                @function read()
--- 177,180 ----
***************
*** 189,192 ****
--- 188,250 ----
                        //}
                        return $this->found_hooks;
+               }
+ 
+               /*!
+               @function register_hooks
+               @abstract Register and/or de-register an application's hooks
+               @syntax register_hooks($appname,$hooks='')
+               @param $appname Application 'name' 
+               @param $hooks array with hooks to register, eg 
$setup_info[$app]['hooks'] or not used for only deregister the hooks
+               */
+               function register_hooks($appname,$hooks='')
+               {
+                       if(!$appname)
+                       {
+                               return False;
+                       }
+                       $db_appname = $this->db->db_addslashes($appname);
+                       $this->db->query("DELETE FROM phpgw_hooks WHERE 
hook_appname='$db_appname'",__LINE__,__FILE__);
+ 
+                       if (!is_array($hooks))  // only deregister
+                       {
+                               return True;
+                       }
+                       //echo "<p>ADDING hooks for: $appname</p>";
+                       foreach($hooks as $key => $hook)
+                       {
+                               if (!is_numeric($key))  // new methode-hook
+                               {
+                                       $location = $key;
+                                       $filename = $hook;
+                               }
+                               else
+                               {
+                                       $location = $hook;
+                                       $filename = "hook_$hook.inc.php";
+                               }
+                               $this->db->query("INSERT INTO phpgw_hooks 
(hook_appname,hook_location,hook_filename)".
+                                       " VALUES 
('$appname','$location','$filename');");
+                       }
+                       return True;
+               }
+ 
+               
+               /*!
+               @function register_all_hooks
+               @abstract Register the hooks of all applications (used by admin)
+               */
+               function register_all_hooks()
+               {
+                       $SEP = filesystem_separator();
+                       
+                       foreach($GLOBALS['phpgw_info']['apps'] as $appname => 
$app)
+                       {                       
+                               $f = PHPGW_SERVER_ROOT . $SEP . $appname . $SEP 
. 'setup' . $SEP . 'setup.inc.php';
+                               if(@file_exists($f))
+                               {
+                                       include($f);
+                                       
$this->register_hooks($appname,$setup_info[$appname]['hooks']);
+                               }
+                       }
                }
        }





reply via email to

[Prev in Thread] Current Thread [Next in Thread]