Location: PHPKode > projects > Calia > calia/admin.inc
<?php

/**
 * Calia - Administration support functions
 *
 * @package    Calia
 * @author     Andrew Gray <hide@address.com>
 * @version    CVS: $Id: admin.inc,v 1.3 2009/10/09 23:14:46 blargh2015 Exp $
 *
 * Copyright 2001-2009 Andrew Gray
 *
 * This file is part of Calia.
 *
 * Calia is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * Calia is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Calia; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 **/

/***
 * Go through the motions to activate a module.  The module's configuration must have already been prompted for from the user.
 *
 * @param  string  $fileName       Module filename.
 * @param  string  $className      Module class name.
 * @param  mixed   $cfgData        Configuration data for this instance.
 * @param  boolean $isDbMod        Is this to be our database module?  Handled differently.  Should almost always be FALSE.
 *
 * @return mixed                   The resulting GUID of the module.
 */
function AdminEnableModule($fileName, $className, $cfgData, $isDbMod = FALSE)
{
  global $db;
    
  if($isDbMod)
    {
      // Save the data.
      $fp = @fopen('site.inc', 'w');
      if($fp === FALSE)
	{
	  LibHtmlMessageBoxError('Unable to write to file site.inc in the base directory.  You may want to try the following steps from a command prompt:<br>touch site.inc<br>chmod 777 site.inc<br>Refresh this page<br>chmod 440 site.inc');
	  exit;
	}
      fwrite($fp, "<?php\n\n// Calia Site Configuration File.\n// Contains module configuration data.\n// Do not modify by hand.\n\n");
      fwrite($fp, "\$dbInformation = unserialize('".serialize(array($fileName, $className, $cfgData))."');\n?>\n");
      fclose($fp);
      return NULL;
    } else {
      // Start a transaction so any function along the line can rollback module install if an error occurs.
      $db->Begin();

      // Make sure the file is loaded.  Can't use autoloader since the module isn't installed yet.
      require_once $GLOBALS['include-path'].'modules/'.$fileName;

      // Install the module record to get the GUID to pass to install just in case it needs it.
      $db->Insert('core_modules', array('classFilename'=>$fileName, 
					'className'=>$className, 
					'classImplements'=>implode(',',class_implements($className)),
					'cfgData'=>serialize($cfgData)));
      $modGuid = $db->GetInsertGuid('core_modules', 'guid');
      
      $GLOBALS['modules'][] = array('classFilename'=>$fileName,
				    'className'=>$className,
				    'classImplements'=>class_implements($className),
				    'cfgData'=>$cfgData);
      $obj = new $className($modGuid, $cfgData);
      $obj->ModuleInstall();
      $db->Commit();
      
      return $modGuid;
  }
}

function AdminDisableModule($modGuid)
{
  global $db;

  $db->Begin();

  // This will cascade (via fkeys) to removing menus, callbacks, and permgroups.
  $db->Delete('core_modules', array('=','guid','|'.$modGuid));

  $obj = new $GLOBALS['modules'][$modGuid]['className']($modGuid, $GLOBALS['modules'][$modGuid]['cfgData']);
  $obj->ModuleUninstall();

  $db->Commit();
}

function AdminReconfigureModule($modGuid, $newCfgData, $callReconfig = TRUE)
{
  global $db;

  $db->Begin();

  if($callReconfig)
    {
      $obj = ModuleObject($modGuid);
      // We update the database now, in case ModuleReconfigure itself wants to reconfigure.
      $db->Update('core_modules', array('guid'=>$modGuid), array('cfgData'=>serialize($newCfgData)));
      $res = $obj->ModuleReconfigure($newCfgData);
      if($res === TRUE)
	StoreMessage('c', _('Reconfiguration successful.'));
      else
	StoreMessage('e', $res);
    } 
  else 
    {
      $db->Update('core_modules', array('guid'=>$modGuid), array('cfgData'=>serialize($newCfgData)));
    }

  $db->Commit();
}

/***
 * Register a new menu item.
 *
 * @param  mixed   $guid           The GUID of the module assigning this to, or NULL for the core system.
 * @param  array   $permsArray     Permission required to show this menu item, in PermsCheck() format.
 * @param  array   $menuHierarchy  Menu hierarchy to add.  Parents must be created before children.
 * @param  string  $targetUrl      URL to have the menu entry point to.
 *
 * @return void
 */
function AdminRegisterMenu($guid, $permsArray, $menuHierarchy, $targetUrl)
{
  global $db;
  $db->Insert('core_config', array('name'=>'menu-data', 'reg'=>$guid, 'data'=>serialize(array($permsArray, $menuHierarchy, $targetUrl))));
}

/***
 * Unregister a (possibly non-existing) menu item.  Not needed for module uninstall - they will be unreigstered automatically by the database via foreign keys.
 *
 * @param  mixed   $guid           The GUID of the module assigning this to, or NULL for the core system.
 * @param  array   $permsArray     Permission required to show this menu item, in PermsCheck() format.
 * @param  array   $menuHierarchy  Menu hierarchy to add.  Parents must be created before children.
 * @param  string  $targetUrl      URL to have the menu entry point to.
 *
 * @return void
 */
function AdminUnregisterMenu($guid, $permsArray, $menuHierarchy, $targetUrl)
{
  global $db;
  $db->Delete('core_config', array('name'=>'menu-data','reg'=>$guid,'data'=>serialize(array($permsArray, $menuHierarchy, $targetUrl))));
}

/***
 * Register a callback function.  Callback functions are called with 1 or 2 parameters - the first being an array of data for the function,
 * depending on the callback, the second being any optional userData as supplied here.
 *
 * @param  mixed   $guid           The GUID of the module assigning this to, or NULL for the core system.
 * @param  string  $callbackName   The name of the callback to append an item onto.
 * @param  string  $functionFile   Filename of the function to add.
 * @param  string  $functionName   Name of the function to add.
 * @param  mixed   $userData       Any sort of additional data to hand off to the function.  
 *
 * @return void
 */
function AdminRegisterCallbackFunction($guid, $callbackName, $functionFile, $functionName, $userData = NULL)
{
  global $db;
  $db->Insert('core_config', array('name'=>'callbacks', 'reg'=>$guid, 'data'=>serialize(array($callbackName, array($functionFile, $functionName, $userData)))));
}

/***
 * Unregister a (possibly non-existing) callback function.  Not needed for module uninstall - they will be unreigstered automatically by the database via foreign keys.
 *
 * @param  mixed   $guid           The GUID of the module assigning this to, or NULL for the core system.
 * @param  string  $callbackName   The name of the callback to append an item onto.
 * @param  string  $functionFile   Filename of the function to add.
 * @param  string  $functionName   Name of the function to add.
 * @param  mixed   $userData       Any sort of additional data to hand off to the function.  
 *
 * @return void
 */
function AdminUnregisterCallbackFunction($guid, $callbackName, $functionFile, $functionName, $userData = NULL)
{
  global $db;
  $db->Delete('core_config', array('name'=>'callbacks', 'reg'=>$guid, 'data'=>serialize(array($callbackName, $functionFile, $functionName, $userData))));
}

/**
 * Register a permission group.
 *
 * @param  mixed   $guid           The GUID of the module assigning this to, or NULL for the core system.
 * @param  string  $humanName      The name of this permission group.
 * @param  mixed   $systemGuid     The guid of the system registering it (usually the module GUID).
 * @param  string  $itemsTable     Table to query for sub-items with grantable permissions, or NULL if N/A.
 * @param  string  $itemsNamePre   Prefix before $itemsNameCol information.
 * @param  mixed   $itemsNameCol   The column in $itemsTable with the human-readable data for an item.
 * @param  mixed   $perms          Permissions array (char=>helptext).
 *
 * @return void
 */
function AdminRegisterPermissionGroup($guid, $humanName, $systemGuid, $itemsTable, $itemsNamePre, $itemsNameCol, $perms)
{
  global $db;
  $db->Insert('core_config', array('name'=>'perm-group', 'reg'=>$guid, 'data'=>serialize(array($humanName, $systemGuid, $itemsTable, $itemsNamePre, $itemsNameCol, $perms))));
  $GLOBALS['authz-object']->Init($guid, $itemsTable, 'guid', $itemsNameCol, array_keys($perms));
}

/**
 * Unregister a permission group.  Not needed for module uninstall - they will be unreigstered automatically by the database via foreign keys.
 *
 * @param  mixed   $guid           The GUID of the module assigning this to, or NULL for the core system.
 * @param  string  $humanName      The name of this permission group.
 * @param  mixed   $systemGuid     The guid of the system registering it (usually the module GUID).
 * @param  string  $itemsTable     Table to query for sub-items with grantable permissions, or NULL if N/A.
 * @param  string  $itemsNamePre   Prefix before $itemsNameCol information.
 * @param  mixed   $itemsNameCol   The column in $itemsTable with the human-readable data for an item.
 * @param  mixed   $perms          Permissions array (char=>helptext).
 *
 * @return void
 */
function AdminUnregisterPermissionGroup($guid, $humanName, $systemGuid, $itemsTable, $itemsNamePre, $itemsNameCol, $perms)
{
  global $db;
  $db->Delete('core_config', array('name'=>'perm-group','reg'=>$guid,'data'=>serialize(array($humanName, $systemGuid, $itemsTable, $itemsNamePre, $itemsNameCol, $perms))));
  $GLOBALS['authz-object']->Uninit($itemsTable);
}

/**
 * Register an edit handler.
 *
 * @param  mixed   $guid           The GUID of the module assigning this to, or NULL for the core system.
 * @param  string  $tableName      Table name this is an editor for.
 * @param  string  $includeFile    File that needs to be required to get the class.
 * @param  string  $className      caliaDataInterface class for this.
 * @param  mixed   $consArgs       Optional additional arguments to pass to the constructor of the handler.
 *
 * @return void
 */
function AdminRegisterEditHandler($guid, $tableName, $includeFile, $className, $consArgs = NULL)
{
  global $db;
  $db->Insert('core_config', array('name'=>'edit-handler', 'reg'=>$guid, 'data'=>serialize(array($tableName, $includeFile, $className, $consArgs))));
}

/**
 * Unregister an edit handler.
 *
 * @param  mixed   $guid           The GUID of the module assigning this to, or NULL for the core system.
 * @param  string  $tableName      Table name this is an editor for.
 * @param  string  $includeFile    File that needs to be required to get the class.
 * @param  string  $className      caliaDataInterface class for this.
 * @param  mixed   $consArgs       Optional additional arguments to pass to the constructor of the handler.
 *
 * @return void
 */
function AdminUnregisterEditHandler($guid, $tableName, $includeFile, $className, $consArgs = NULL)
{
  global $db;
  $db->Delete('core_config', array('name'=>'edit-handler', 'reg'=>$guid, 'data'=>serialize(array($tableName, $includeFile, $className, $consArgs))));
}

/**
 * Register an edit chain.  Edit chains set up connections between tables for edit handlers.
 *
 * @param  mixed   $guid           The GUID of the module assigning this to, or NULL for the core system.
 * @param  string  $sourceTable    Source table
 * @param  string  $sourceField    Source field
 * @param  string  $destTable      Destination table
 * @param  string  $destField      Destination field
 *
 * @return void
 */
function AdminRegisterEditChain($guid, $sourceTable, $sourceField, $destTable, $destField)
{
  global $db;
  $db->Insert('core_config', array('name'=>'edit-chain', 'reg'=>$guid, 'data'=>serialize(array($sourceTable, $sourceField, $destTable, $destField))));
}

/**
 * Unregister an edit chain.  Edit chains set up connections between tables for edit handlers.
 *
 * @param  mixed   $guid           The GUID of the module assigning this to, or NULL for the core system.
 * @param  string  $sourceTable    Source table
 * @param  string  $sourceField    Source field
 * @param  string  $destTable      Destination table
 * @param  string  $destField      Destination field
 *
 * @return void
 */
function AdminUnregisterEditChain($guid, $sourceTable, $sourceField, $destTable, $destField)
{
  global $db;
  $db->Delete('core_config', array('name'=>'edit-chain', 'reg'=>$guid, 'data'=>serialize(array($sourceTable, $sourceField, $destTable, $destField))));
}

/**
 * Add a table to watch for changes to trigger the 'Changes need to be committed.' text.
 *
 * @param  mixed   $guid           The GUID of the module assigning this to, or NULL for the core system.
 * @param  string  $table          Table
 */
function AdminRegisterCommitTable($guid, $table)
{
  global $db;
  $db->Insert('core_config', array('name'=>'commit-table', 'reg'=>$guid, 'data'=>serialize(array($table))));
}

/**
 * Remove a table to watch for changes to trigger the 'Changes need to be committed.' text.
 *
 * @param  mixed   $guid           The GUID of the module assigning this to, or NULL for the core system.
 * @param  string  $table          Table
 */
function AdminUnregisterCommitTable($guid, $table)
{
  global $db;
  $db->Delete('core_config', array('name'=>'commit-table', 'reg'=>$guid, 'data'=>serialize(array($table))));
}

/**
 * Add a function to be called with Commit Changes is done.
 *
 * @param  mixed   $guid           The GUID of the module assigning this to, or NULL for the core system.
 * @param  string  $file           File to include
 * @param  string  $func           Function to call.
 */
function AdminRegisterCommitFunc($guid, $file, $func)
{
  global $db;
  $db->Insert('core_config', array('name'=>'commit-func', 'reg'=>$guid, 'data'=>serialize(array($file, $func))));
}

/**
 * Remove a function to be called with Commit Changes is done.
 *
 * @param  mixed   $guid           The GUID of the module assigning this to, or NULL for the core system.
 * @param  string  $file           File to include
 * @param  string  $func           Function to call.
 */
function AdminUnregisterCommitFunc($guid, $file, $func)
{
  global $db;
  $db->Delete('core_config', array('name'=>'commit-func', 'reg'=>$guid, 'data'=>serialize(array($file, $func))));
}

?>
Return current item: Calia