[ Index ]
 

Code source de eGroupWare 1.2.106-2

Accédez au Source d'autres logiciels libresSoutenez Angelica Josefina !

title

Body

[fermer]

/workflow/inc/engine/src/ProcessManager/ -> InstanceManager.php (source)

   1  <?php
   2  require_once(GALAXIA_LIBRARY.SEP.'src'.SEP.'ProcessManager'.SEP.'BaseManager.php');
   3  //!! InstanceManager
   4  //! A class to maniplate instances
   5  /*!
   6    This class is used to add,remove,modify and list
   7    instances.
   8  */
   9  class InstanceManager extends BaseManager {
  10    
  11    /*!
  12      Constructor takes a PEAR::Db object to be used
  13      to manipulate roles in the database.
  14    */
  15    function InstanceManager(&$db) 
  16    {
  17      parent::BaseManager($db);
  18      $this->child_name = 'InstanceManager';
  19    }
  20  
  21    /*!
  22    * @public
  23    * @param $iid is the instance Id
  24    * @return an associative array describing activities and their relation with the instance
  25    */
  26    function get_instance_activities($iid)
  27    {
  28      $query = 'select ga.wf_type,ga.wf_is_interactive,ga.wf_is_autorouted,ga.wf_activity_id,ga.wf_name,
  29              gi.wf_p_id,gi.wf_instance_id,gi.wf_status,gi.wf_started,
  30              gia.wf_activity_id,gia.wf_user,gia.wf_status as wf_act_status 
  31              from '.GALAXIA_TABLE_PREFIX.'activities ga,
  32              INNER JOIN '.GALAXIA_TABLE_PREFIX.'instance_activities gia ON ga.wf_activity_id=gia.wf_activity_id
  33              INNER JOIN '.GALAXIA_TABLE_PREFIX.'instances gi ON gia.wf_instance_id=gi.wf_instance_id,
  34              where gi.wf_instance_id=?';
  35      $result = $this->query($query, array($iid));
  36      $ret = Array();
  37      if (!(empty($result)))
  38      {
  39        while($res = $result->fetchRow()) 
  40        {
  41          // Number of active instances
  42          $ret[] = $res;
  43        }
  44      }
  45      return $ret;
  46    }
  47  
  48    /*!
  49    * @public
  50    * @param $iid is the instance Id
  51    * @return an associative array describing the instance
  52    */
  53    function get_instance($iid)
  54    {
  55      $query = 'select * from '.GALAXIA_TABLE_PREFIX.'instances gi where wf_instance_id=?';
  56      $result = $this->query($query, array($iid));
  57      $res = Array();
  58      if (!(empty($result)))
  59      {
  60        $res = $result->fetchRow();
  61        $res['wf_next_activity']=unserialize(base64_decode($res['wf_next_activity']));
  62        $res['wf_workitems']=$this->getOne('select count(*) from '.GALAXIA_TABLE_PREFIX.'workitems where wf_instance_id=?', array($iid));
  63      }
  64      return $res;
  65    }
  66  
  67    /*!
  68    * @public
  69    * @param $iid is the instance Id
  70    * @return an associative array describing the instance properties
  71    */
  72    function get_instance_properties($iid)
  73    {
  74      $prop = unserialize(base64_decode($this->getOne('select wf_properties from '.GALAXIA_TABLE_PREFIX.'instances gi where wf_instance_id=?',array($iid))));
  75      return $prop;
  76    }
  77  
  78    /*!
  79    * @private
  80    * Start a transaction and lock the instance table on the given instance row.
  81    * It can lock as weel the instance-activities table.
  82    * @param $instanceId is the instance id
  83    * @param $activityId is the activityId, 0 b default, if 0 the we do not lock
  84    * the instance-activities table, ellese the instance-activities table will
  85    * be locked on the corresponding instance-activity row
  86    */
  87    function lockAndStartTrans($instanceId, $activityId=0)
  88    {
  89      //do it in a transaction, for activities running
  90      $this->db->StartTrans();
  91      //we need to make a row lock now, first on the instance table (always first!)
  92      $where = 'wf_instance_id='.(int)$instanceId;
  93      if (!($this->db->RowLock(GALAXIA_TABLE_PREFIX.'instances', $where)))
  94      {
  95        $this->error[] = 'Process Manager: '.tra('failed to obtain lock on %1 table', 'instances');
  96        $this->db->FailTrans();
  97      }
  98      if ($activityId)
  99      {
 100        //we need to make a row lock now, on the instance_activities table (always second!)
 101        $where = 'wf_instance_id='.(int)$instanceId.' and wf_activity_id='.(int)$activityId;
 102        if (!($this->db->RowLock(GALAXIA_TABLE_PREFIX.'instance_activities', $where)))
 103        {
 104          $this->error[] = 'Process Manager: '.tra('failed to obtain lock on %1 table','instances_activities');
 105          return false;
 106        }
 107      }
 108     } 
 109  
 110    
 111    /*!
 112    * @public
 113    * Save the given instance properties
 114    * @param $iid is the instance Id
 115    * @param $prop is an associative array describing the instance properties
 116    * @return true or false
 117    */
 118    function set_instance_properties($iid,&$prop)
 119    {
 120      $this->lockAndStartTrans($iid);
 121      //no more serialize, done by the core security_cleanup, empty array and bad properties names handled
 122      $prop = $this->security_cleanup($prop, false);
 123      $query = 'update '.GALAXIA_TABLE_PREFIX.'instances set wf_properties=? where wf_instance_id=?';
 124      $this->query($query, array($prop,$iid));
 125      return $this->db->CompleteTrans();
 126    }
 127    
 128    /*!
 129    * @public
 130    * Save the given instance name
 131    * @param $iid is the instance Id
 132    * @param $name is the name of the instance
 133    * @return true or false
 134    */
 135    function set_instance_name($iid,$name)
 136    {
 137      $this->lockAndStartTrans($iid);
 138      $query = 'update '.GALAXIA_TABLE_PREFIX.'instances set wf_name=? where wf_instance_id=?';
 139      $this->query($query, array($name,$iid));
 140      return $this->db->CompleteTrans();
 141    }
 142  
 143    /*!
 144    * @public
 145    * Save the given instance priority
 146    * @param $iid is the instance Id
 147    * @param $priority is the instance priority
 148    * @return true or false
 149    */
 150    function set_instance_priority($iid,$priority)
 151    {
 152      $this->lockAndStartTrans($iid);
 153      $query = 'update '.GALAXIA_TABLE_PREFIX.'instances set wf_priority=? where wf_instance_id=?';
 154      $this->query($query, array((int)$priority, (int)$iid));
 155      return $this->db->CompleteTrans();
 156    }
 157  
 158    /*!
 159    * @public
 160    * Save the given instance category
 161    * @param $iid is the instance Id
 162    * @param $category is the instance category
 163    * @return true or false
 164    */
 165    function set_instance_category($iid,$category)
 166    {
 167      $this->lockAndStartTrans($iid);
 168      $query = 'update '.GALAXIA_TABLE_PREFIX.'instances set wf_category=? where wf_instance_id=?';
 169      $this->query($query, array((int)$category, (int)$iid));
 170      return $this->db->CompleteTrans();
 171    }
 172  
 173    /*!
 174    * @public
 175    * Save the given instance owner
 176    * @param $iid is the instance Id
 177    * @param $owner is the owner id of the instance
 178    * @return true or false
 179    */
 180    function set_instance_owner($iid,$owner)
 181    {
 182      $this->lockAndStartTrans($iid);
 183      $query = 'update '.GALAXIA_TABLE_PREFIX.'instances set wf_owner=? where wf_instance_id=?';
 184      $this->query($query, array($owner, $iid));
 185      return $this->db->CompleteTrans();
 186    }
 187    
 188    /*!
 189    * @public
 190    * Save the given instance status
 191    * @param $iid is the instance Id
 192    * @param $status is the instance status, should be one of 'running', 'completed', 'exception' or 'aborted
 193    * @return true or false
 194    */
 195    function set_instance_status($iid,$status)
 196    {
 197      if (!(($status=='completed') || ($status=='active') || ($status=='aborted') || ($status=='exception')))
 198      {
 199        $this->error[] = tra('unknown status');
 200        return false;
 201      }
 202      $this->lockAndStartTrans($iid);
 203      $query = 'update '.GALAXIA_TABLE_PREFIX.'instances set wf_status=? where wf_instance_id=?';
 204      $this->query($query, array($status,$iid));
 205      return $this->db->CompleteTrans();
 206    }
 207    
 208    /*!
 209    * @public 
 210    * Remove all previous activities on this instance and create a new activity on the activity given
 211    * @param $iid is the instance id
 212    * @param $activityId is the activity id
 213    * @param $user is '*' by default and could be an user id
 214    * @param $status is 'running' by default but you could send 'completed' as well
 215    * @return false if any problems was encoutered (the database is then intact). Return true if everything was ok.
 216    * WARNING: if they were multiple activities ALL previous activities avaible on this instance are deleted
 217    */
 218    function set_instance_destination($iid,$activityId, $user='*', $status='running')
 219    {
 220      $this->lockAndStartTrans($iid, $activityId);
 221      $query = 'delete from '.GALAXIA_TABLE_PREFIX.'instance_activities where wf_instance_id=?';
 222      $this->query($query, array($iid));
 223      $query = 'insert into '.GALAXIA_TABLE_PREFIX.'instance_activities(wf_instance_id,wf_activity_id,wf_user,wf_status, wf_started, wf_ended)
 224      values(?,?,?,?,?,?)';
 225      $this->query($query, array($iid,$activityId,'*','running',date('U'),0));
 226      // perform commit (return true) or Rollback (return false)
 227      return $this->db->CompleteTrans();
 228    }
 229   
 230    /*!
 231    * @public
 232    * set $user as the new user of activity $activityId if this activity is really related to the instance.
 233    * @param $iid is the instance Id
 234    * @param $activityId is the activity Id
 235    * @param $user is the new user id
 236    * @return true or false
 237    */
 238    function set_instance_user($iid,$activityId,$user)
 239    {
 240      $this->lockAndStartTrans($iid, $activityId);
 241      $query = "update ".GALAXIA_TABLE_PREFIX."instance_activities set wf_user=? where wf_instance_id=? and wf_activity_id=?";
 242      $this->query($query, array($user, $iid, $activityId));
 243      return $this->db->CompleteTrans();
 244    }
 245    
 246    //! Removes an user from all fields where he could be on every instances
 247    /*!
 248    * This function delete all references on the given user on all instances.
 249    * It will concern: wf_user, wf_owner and wf_next_user fields
 250    * @param $user is the user id to remove
 251    * @return true or false
 252    */
 253    function remove_user($user)  
 254    {
 255      //TODO: add a global lock on the whole tables
 256      // user=id => user='*'
 257      $query = 'update '.GALAXIA_TABLE_PREFIX.'instance_activities set wf_user=? where wf_user=?';
 258      $this->query($query,array('*',$user));
 259      // owner=id => owner=0
 260      $query = 'update '.GALAXIA_TABLE_PREFIX.'instances set wf_owner=? where wf_owner=?';
 261      $this->query($query,array(0,$user));
 262      // next_user=id => next_user=NULL
 263      $query = 'update '.GALAXIA_TABLE_PREFIX.'instances set wf_next_user=? where wf_next_user=?';
 264      $this->query($query,array(NULL,$user));
 265      return true;
 266    }
 267    
 268    //! Transfer all references to one user to another one
 269    /*!
 270    * This function transfer all references concerning one user to another user
 271    * It will concern: wf_user, wf_owner and wf_next_user fields
 272    * This function will not check access on the instance for the new user, it is the task
 273    * of the admin to ensure the new user will have the necessary access rights
 274    * @param $user_array is an associative arrays, keys are:
 275    *     * 'old_user' : the actual user id
 276    *     * 'new_user' : the new user id
 277    * @return true or false
 278    */
 279    function transfer_user($user_array)  
 280    {
 281      $new_user = $user_array['new_user'];
 282      $old_user = $user_array['old_user'];
 283      //TODO: add a global lock on the whole tables
 284      // user
 285      $query = 'update '.GALAXIA_TABLE_PREFIX.'instance_activities set wf_user=? where wf_user=?';
 286      $this->query($query,array($new_user,$old_user));
 287      // owner
 288      $query = 'update '.GALAXIA_TABLE_PREFIX.'instances set wf_owner=? where wf_owner=?';
 289      $this->query($query,array($new_user,$old_user));
 290      // next_user
 291      $query = 'update '.GALAXIA_TABLE_PREFIX.'instances set wf_next_user=? where wf_next_user=?';
 292      $this->query($query,array($new_user,$old_user));
 293      return true;
 294    }
 295    
 296    /*!
 297    * @public
 298    * Normalizes a property name
 299    * @param $name is the name you want to normalize
 300    * @return the property name
 301    */
 302    function normalize_name($name)
 303    {
 304      $name = trim($name);
 305      $name = str_replace(" ","_",$name);
 306      $name = preg_replace("/[^0-9A-Za-z\_]/",'',$name);
 307      return $name;
 308    }
 309  
 310  }    
 311  
 312  ?>


Généré le : Sun Feb 25 17:20:01 2007 par Balluche grâce à PHPXref 0.7