Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Class Members | Related Pages

ActivityManager.php

00001 <?php
00002 include_once(GALAXIA_LIBRARY.'/src/ProcessManager/BaseManager.php');
00005 
00011 class ActivityManager extends BaseManager {
00012   var $error='';
00013       
00018   function ActivityManager($db) {
00019     if(!$db) {
00020       die("Invalid db object passed to activityManager constructor");  
00021     }
00022     $this->db = $db;  
00023   }
00024   
00025   function get_error() {
00026     return $this->error;
00027   }
00028   
00032   function add_activity_role($activityId, $roleId) {
00033     $query = "delete from `".GALAXIA_TABLE_PREFIX."activity_roles` where `activityId`=? and `roleId`=?";
00034     $this->query($query,array($activityId, $roleId));
00035     $query = "insert into `".GALAXIA_TABLE_PREFIX."activity_roles`(`activityId`,`roleId`) values(?,?)";
00036     $this->query($query,array($activityId, $roleId));
00037   }
00038   
00042   function get_activity_roles($activityId) {
00043     $query = "select activityId,roles.roleId,roles.name
00044               from ".GALAXIA_TABLE_PREFIX."activity_roles gar, ".GALAXIA_TABLE_PREFIX."roles roles
00045               where roles.roleId = gar.roleId and activityId=?";
00046     $result = $this->query($query,array($activityId));
00047     $ret = Array();
00048     while($res = $result->fetchRow()) {  
00049       $ret[] = $res;
00050     }
00051     return $ret;
00052   }
00053   
00057   function remove_activity_role($activityId, $roleId)
00058   {
00059     $query = "delete from ".GALAXIA_TABLE_PREFIX."activity_roles
00060               where activityId=$activityId and roleId=$roleId";
00061     $this->query($query);
00062   }
00063   
00067   function transition_exists($pid,$actFromId,$actToId)
00068   {
00069     return($this->getOne("select count(*) from ".GALAXIA_TABLE_PREFIX."transitions where pId=$pid and actFromId=$actFromId and actToId=$actToId"));
00070   }
00071   
00075   function add_transition($pId, $actFromId, $actToId)
00076   {
00077     // No circular transitions allowed
00078     if($actFromId == $actToId) return false;
00079     
00080     // Rule: if act is not spl-x or spl-a it can't have more than
00081     // 1 outbound transition.
00082     $a1 = $this->get_activity($pId, $actFromId);
00083     $a2 = $this->get_activity($pId, $actToId);
00084     if(!$a1 || !$a2) return false;
00085     if($a1['type'] != 'switch' && $a1['type'] != 'split') {
00086       if($this->getOne("select count(*) from ".GALAXIA_TABLE_PREFIX."transitions where actFromId=$actFromId")) {
00087         $this->error = tra('Cannot add transition only split activities can have more than one outbound transition');
00088         return false;
00089       }
00090     }
00091     
00092     // Rule: if act is standalone no transitions allowed
00093     if($a1['type'] == 'standalone' || $a2['type']=='standalone') return false;
00094     // No inbound to start
00095     if($a2['type'] == 'start') return false;
00096     // No outbound from end
00097     if($a1['type'] == 'end') return false;
00098      
00099     
00100     $query = "delete from `".GALAXIA_TABLE_PREFIX."transitions` where `actFromId`=? and `actToId`=?";
00101     $this->query($query,array($actFromId, $actToId));
00102     $query = "insert into `".GALAXIA_TABLE_PREFIX."transitions`(`pId`,`actFromId`,`actToId`) values(?,?,?)";
00103     $this->query($query,array($pId, $actFromId, $actToId));
00104 
00105     return true;
00106   }
00107   
00111   function remove_transition($actFromId, $actToId)
00112   {
00113     $query = "delete from ".GALAXIA_TABLE_PREFIX."transitions where actFromId=$actFromId and actToId=$actToId";
00114     $this->query($query);
00115     return true;
00116   }
00117   
00121   function remove_activity_transitions($pId, $aid)
00122   {
00123     $query = "delete from ".GALAXIA_TABLE_PREFIX."transitions where pId=$pId and (actFromId=$aid or actToId=$aid)";
00124     $this->query($query);
00125   }
00126   
00127   
00131   function get_process_transitions($pId,$actid=0)
00132   {
00133     if(!$actid) {
00134         $query = "select a1.name as actFromName, a2.name as actToName, actFromId, actToId from ".GALAXIA_TABLE_PREFIX."transitions gt,".GALAXIA_TABLE_PREFIX."activities a1, ".GALAXIA_TABLE_PREFIX."activities a2 where gt.actFromId = a1.activityId and gt.actToId = a2.activityId and gt.pId = $pId";
00135     } else {
00136         $query = "select a1.name as actFromName, a2.name as actToName, actFromId, actToId from ".GALAXIA_TABLE_PREFIX."transitions gt,".GALAXIA_TABLE_PREFIX."activities a1, ".GALAXIA_TABLE_PREFIX."activities a2 where gt.actFromId = a1.activityId and gt.actToId = a2.activityId and gt.pId = $pId and (actFromId = $actid)";
00137     }
00138     $result = $this->query($query);
00139     $ret = Array();
00140     while($res = $result->fetchRow()) {  
00141       $ret[] = $res;
00142     }
00143     return $ret;
00144   }
00145   
00149   function activity_is_auto_routed($actid)
00150   {
00151     return($this->getOne("select count(*) from ".GALAXIA_TABLE_PREFIX."activities where activityId=$actid and isAutoRouted='y'"));
00152   }
00153   
00158   function get_process_activities($pId)
00159   {
00160        $query = "select * from ".GALAXIA_TABLE_PREFIX."activities where pId=$pId";
00161     $result = $this->query($query);
00162     $ret = Array();
00163     while($res = $result->fetchRow()) {  
00164       $ret[] = $res;
00165     }
00166     return $ret;
00167   }
00168 
00172   //\todo build the real graph
00173   function build_process_graph($pId)
00174   {
00175     $attributes = Array(
00176     
00177     );
00178     $graph = new Process_GraphViz(true,$attributes);
00179     $pm = new ProcessManager($this->db);
00180     $name = $pm->_get_normalized_name($pId);
00181     $graph->set_pid($name);
00182     
00183     // Nodes are process activities so get
00184     // the activities and add nodes as needed
00185     $nodes = $this->get_process_activities($pId);
00186     
00187     foreach($nodes as $node)
00188     {
00189       if($node['isInteractive']=='y') {
00190         $color='blue';
00191       } else {
00192         $color='black';
00193       }
00194       $auto[$node['name']] = $node['isAutoRouted'];
00195       $graph->addNode($node['name'],array('URL'=>"foourl?activityId=".$node['activityId'],
00196                                       'label'=>$node['name'],
00197                                       'shape' => $this->_get_activity_shape($node['type']),
00198                                       'color' => $color
00199 
00200                                       )
00201                      );    
00202     }
00203     
00204     // Now add edges, edges are transitions,
00205     // get the transitions and add the edges
00206     $edges = $this->get_process_transitions($pId);
00207     foreach($edges as $edge)
00208     {
00209       if($auto[$edge['actFromName']] == 'y') {
00210         $color = 'red';
00211       } else {
00212         $color = 'black';
00213       }
00214         $graph->addEdge(array($edge['actFromName'] => $edge['actToName']), array('color'=>$color));    
00215     }
00216     
00217     
00218     // Save the map image and the image graph
00219     $graph->image_and_map();
00220     unset($graph);
00221     return true;   
00222   }
00223   
00224   
00237   function validate_process_activities($pId)
00238   {
00239     $errors = Array();
00240     // Pre rule no cricular activities
00241     $cant = $this->getOne("select count(*) from ".GALAXIA_TABLE_PREFIX."transitions where pId=$pId and actFromId=actToId");
00242     if($cant) {
00243       $errors[] = tra('Circular reference found some activity has a transition leading to itself');
00244     }
00245 
00246     // Rule 1 must have exactly one start and end activity
00247     $cant = $this->getOne("select count(*) from ".GALAXIA_TABLE_PREFIX."activities where pId=$pId and type='start'");
00248     if($cant < 1) {
00249       $errors[] = tra('Process does not have a start activity');
00250     }
00251     $cant = $this->getOne("select count(*) from ".GALAXIA_TABLE_PREFIX."activities where pId=$pId and type='end'");
00252     if($cant != 1) {
00253       $errors[] = tra('Process does not have exactly one end activity');
00254     }
00255     
00256     // Rule 2 end must be reachable from start
00257     $nodes = Array();
00258     $endId = $this->getOne("select activityId from ".GALAXIA_TABLE_PREFIX."activities where pId=$pId and type='end'");
00259     $aux['id']=$endId;
00260     $aux['visited']=false;
00261     $nodes[] = $aux;
00262     
00263     $startId = $this->getOne("select activityId from ".GALAXIA_TABLE_PREFIX."activities where pId=$pId and type='start'");
00264     $start_node['id']=$startId;
00265     $start_node['visited']=true;    
00266     
00267     while($this->_list_has_unvisited_nodes($nodes) && !$this->_node_in_list($start_node,$nodes)) {
00268       for($i=0;$i<count($nodes);$i++) {
00269         $node=&$nodes[$i];
00270         if(!$node['visited']) {
00271           $node['visited']=true;          
00272           $query = "select actFromId from ".GALAXIA_TABLE_PREFIX."transitions where actToId=".$node['id'];
00273           $result = $this->query($query);
00274           $ret = Array();
00275           while($res = $result->fetchRow()) {  
00276             $aux['id'] = $res['actFromId'];
00277             $aux['visited']=false;
00278             if(!$this->_node_in_list($aux,$nodes)) {
00279               $nodes[] = $aux;
00280             }
00281           }
00282         }
00283       }
00284     }
00285     
00286     if(!$this->_node_in_list($start_node,$nodes)) {
00287       // Start node is NOT reachable from the end node
00288       $errors[] = tra('End activity is not reachable from start activity');
00289     }
00290     
00291     //Rule 3: interactive activities must have a role
00292     //assigned.
00293     //Rule 5: standalone activities can't have transitions
00294     $query = "select * from ".GALAXIA_TABLE_PREFIX."activities where pId = $pId";
00295     $result = $this->query($query);
00296     while($res = $result->fetchRow()) {  
00297       $aid = $res['activityId'];
00298       if($res['isInteractive'] == 'y') {
00299           $cant = $this->getOne("select count(*) from ".GALAXIA_TABLE_PREFIX."activity_roles where activityId=".$res['activityId']);
00300           if(!$cant) {
00301             $errors[] = tra('Activity').': '.$res['name'].tra(' is interactive but has no role assigned');
00302           }
00303       } else {
00304         if( $res['type'] != 'end' && $res['isAutoRouted'] == 'n') {
00305           $cant = $this->getOne("select count(*) from ".GALAXIA_TABLE_PREFIX."activity_roles where activityId=".$res['activityId']);
00306             if(!$cant) {
00307               $errors[] = tra('Activity').': '.$res['name'].tra(' is non-interactive and non-autorouted but has no role assigned');
00308             }
00309         }
00310       }
00311       if($res['type']=='standalone') {
00312         if($this->getOne("select count(*) from ".GALAXIA_TABLE_PREFIX."transitions where actFromId=$aid or actToId=$aid")) {
00313            $errors[] = tra('Activity').': '.$res['name'].tra(' is standalone but has transitions');
00314         }
00315       }
00316 
00317     }
00318     
00319     
00320     //Rule4: roles should be mapped
00321     $query = "select * from ".GALAXIA_TABLE_PREFIX."roles where pId = $pId";
00322     $result = $this->query($query);
00323     while($res = $result->fetchRow()) {      
00324         $cant = $this->getOne("select count(*) from ".GALAXIA_TABLE_PREFIX."user_roles where roleId=".$res['roleId']);
00325         if(!$cant) {
00326           $errors[] = tra('Role').': '.$res['name'].tra(' is not mapped');
00327         }        
00328     }
00329     
00330     
00331     // End of rules
00332 
00333     // Validate process sources
00334     $serrors=$this->validate_process_sources($pId);
00335     $errors = array_merge($errors,$serrors);
00336     
00337     $this->error = $errors;
00338     
00339     
00340     
00341     $isValid = (count($errors)==0) ? 'y' : 'n';
00342 
00343     $query = "update ".GALAXIA_TABLE_PREFIX."processes set isValid='$isValid' where pId=$pId";
00344     $this->query($query);
00345     
00346     $this->_label_nodes($pId);    
00347     
00348     return ($isValid=='y');
00349     
00350     
00351   }
00352   
00361   function validate_process_sources($pid)
00362   {
00363     $errors=Array();
00364     $procname= $this->getOne("select normalized_name from ".GALAXIA_TABLE_PREFIX."processes where pId=$pid");
00365     
00366     $query = "select * from ".GALAXIA_TABLE_PREFIX."activities where pId=$pid";
00367     $result = $this->query($query);
00368     while($res = $result->fetchRow()) {          
00369       $actname = $res['normalized_name'];
00370       $source = GALAXIA_PROCESSES."/$procname/code/activities/$actname".'.php';
00371       if (!file_exists($source)) {
00372           continue;
00373       }
00374       $fp = fopen($source,'r');
00375       $data='';
00376       while(!feof($fp)) {
00377         $data.=fread($fp,8192);
00378       }
00379       fclose($fp);
00380       if($res['type']=='standalone') {
00381           if(strstr($data,'$instance')) {
00382             $errors[] = tra('Activity '.$res['name'].' is standalone and is using the $instance object');
00383           }    
00384       } else {
00385         if($res['isInteractive']=='y') {
00386           if(!strstr($data,'$instance->complete()')) {
00387             $errors[] = tra('Activity '.$res['name'].' is interactive so it must use the $instance->complete() method');
00388           }
00389         } else {
00390           if(strstr($data,'$instance->complete()')) {
00391             $errors[] = tra('Activity '.$res['name'].' is non-interactive so it must not use the $instance->complete() method');
00392           }
00393         }
00394         if($res['type']=='switch') {
00395           if(!strstr($data,'$instance->setNextActivity(')) { 
00396             $errors[] = tra('Activity '.$res['name'].' is switch so it must use $instance->setNextActivity($actname) method');          
00397           }
00398         }
00399       }    
00400     }
00401     return $errors;
00402   }
00403   
00407   function activity_name_exists($pId,$name)
00408   {
00409     $name = addslashes($this->_normalize_name($name));
00410     return $this->getOne("select count(*) from ".GALAXIA_TABLE_PREFIX."activities where pId=$pId and normalized_name='$name'");
00411   }
00412   
00413   
00417   function get_activity($pId, $activityId)
00418   {
00419       $query = "select * from ".GALAXIA_TABLE_PREFIX."activities where pId=$pId and activityId=$activityId";
00420     $result = $this->query($query);
00421     $res = $result->fetchRow();
00422     return $res;
00423   }
00424   
00428   function list_activities($pId,$offset,$maxRecords,$sort_mode,$find,$where='')
00429   {
00430     $sort_mode = str_replace("_"," ",$sort_mode);
00431     if($find) {
00432       $findesc = '%'.$find.'%';
00433       $mid=" where pId=? and ((name like ?) or (description like ?))";
00434       $bindvars = array($pId,$findesc,$findesc);
00435     } else {
00436       $mid=" where pId=? ";
00437       $bindvars = array($pId);
00438     }
00439     if($where) {
00440       $mid.= " and ($where) ";
00441     }
00442     $query = "select * from ".GALAXIA_TABLE_PREFIX."activities $mid order by $sort_mode";
00443     $query_cant = "select count(*) from ".GALAXIA_TABLE_PREFIX."activities $mid";
00444     $result = $this->query($query,$bindvars,$maxRecords,$offset);
00445     $cant = $this->getOne($query_cant,$bindvars);
00446     $ret = Array();
00447     while($res = $result->fetchRow()) {
00448       $res['roles'] = $this->getOne("select count(*) from ".GALAXIA_TABLE_PREFIX."activity_roles where activityId=?",array($res['activityId']));
00449       $ret[] = $res;
00450     }
00451     $retval = Array();
00452     $retval["data"] = $ret;
00453     $retval["cant"] = $cant;
00454     return $retval;
00455   }
00456   
00457   
00458   
00462   function remove_activity($pId, $activityId)
00463   {
00464     $pm = new ProcessManager($this->db);
00465     $proc_info = $pm->get_process($pId);
00466     $actname = $this->_get_normalized_name($activityId);
00467     $query = "delete from ".GALAXIA_TABLE_PREFIX."activities where pId=$pId and activityId=$activityId";
00468     $this->query($query);
00469     $query = "select actFromId,actToId from ".GALAXIA_TABLE_PREFIX."transitions where actFromId=$activityId or actToId=$activityId";
00470     $result = $this->query($query);
00471     while($res = $result->fetchRow()) {  
00472       $this->remove_transition($res['actFromId'], $res['actToId']);
00473     }
00474     $query = "delete from ".GALAXIA_TABLE_PREFIX."activity_roles where activityId=$activityId";
00475     $this->query($query);
00476     // And we have to remove the user and compiled files
00477     // for this activity
00478     $procname = $proc_info['normalized_name'];
00479     unlink(GALAXIA_PROCESSES."/$procname/code/activities/$actname".'.php'); 
00480     if (file_exists(GALAXIA_PROCESSES."/$procname/code/templates/$actname".'.tpl')) {
00481       @unlink(GALAXIA_PROCESSES."/$procname/code/templates/$actname".'.tpl'); 
00482     }
00483     unlink(GALAXIA_PROCESSES."/$procname/compiled/$actname".'.php'); 
00484     return true;
00485   }
00486   
00493   function replace_activity($pId, $activityId, $vars)
00494   {
00495     $TABLE_NAME = GALAXIA_TABLE_PREFIX."activities";
00496     $now = date("U");
00497     $vars['lastModif']=$now;
00498     $vars['pId']=$pId;
00499     $vars['normalized_name'] = $this->_normalize_name($vars['name']);    
00500 
00501     $pm = new ProcessManager($this->db);
00502     $proc_info = $pm->get_process($pId);
00503     
00504     
00505     foreach($vars as $key=>$value)
00506     {
00507       $vars[$key]=addslashes($value);
00508     }
00509   
00510     if($activityId) {
00511       $oldname = $this->_get_normalized_name($activityId);
00512       // update mode
00513       $first = true;
00514       $query ="update $TABLE_NAME set";
00515       foreach($vars as $key=>$value) {
00516         if(!$first) $query.= ',';
00517         if(!is_numeric($value)) $value="'".$value."'";
00518         $query.= " $key=$value ";
00519         $first = false;
00520       }
00521       $query .= " where pId=$pId and activityId=$activityId ";
00522       $this->query($query);
00523       
00524       $newname = $vars['normalized_name'];
00525       // if the activity is changing name then we
00526       // should rename the user_file for the activity
00527       // remove the old compiled file and recompile
00528       // the activity
00529       
00530       $user_file_old = GALAXIA_PROCESSES.'/'.$proc_info['normalized_name'].'/code/activities/'.$oldname.'.php';
00531       $user_file_new = GALAXIA_PROCESSES.'/'.$proc_info['normalized_name'].'/code/activities/'.$newname.'.php';
00532       rename($user_file_old, $user_file_new);
00533 
00534       $user_file_old = GALAXIA_PROCESSES.'/'.$proc_info['normalized_name'].'/code/templates/'.$oldname.'.tpl';
00535       $user_file_new = GALAXIA_PROCESSES.'/'.$proc_info['normalized_name'].'/code/templates/'.$newname.'.tpl';
00536       if ($user_file_old != $user_file_new) {
00537         @rename($user_file_old, $user_file_new);
00538       }
00539 
00540       
00541       $compiled_file = GALAXIA_PROCESSES.'/'.$proc_info['normalized_name'].'/compiled/'.$oldname.'.php';    
00542       unlink($compiled_file);
00543       $this->compile_activity($pId,$activityId);
00544       
00545       
00546     } else {
00547       
00548       // When inserting activity names can't be duplicated
00549       if($this->activity_name_exists($pId, $vars['name'])) {
00550           return false;
00551       }
00552       unset($vars['activityId']);
00553       // insert mode
00554       $first = true;
00555       $query = "insert into $TABLE_NAME(";
00556       foreach(array_keys($vars) as $key) {
00557         if(!$first) $query.= ','; 
00558         $query.= "$key";
00559         $first = false;
00560       } 
00561       $query .=") values(";
00562       $first = true;
00563       foreach(array_values($vars) as $value) {
00564         if(!$first) $query.= ','; 
00565         if(!is_numeric($value)) $value="'".$value."'";
00566         $query.= "$value";
00567         $first = false;
00568       } 
00569       $query .=")";
00570       $this->query($query);
00571       $activityId = $this->getOne("select max(activityId) from $TABLE_NAME where pId=$pId and lastModif=$now"); 
00572       $ret = $activityId;
00573       if(!$activityId) {
00574          print("select max(activityId) from $TABLE_NAME where pId=$pId and lastModif=$now");
00575          die;      
00576       }
00577       // Should create the code file
00578       $procname = $proc_info["normalized_name"];
00579         $fw = fopen(GALAXIA_PROCESSES."/$procname/code/activities/".$vars['normalized_name'].'.php','w');
00580         fwrite($fw,'<'.'?'.'php'."\n".'?'.'>');
00581         fclose($fw);
00582         
00583          if($vars['isInteractive']=='y') {
00584             $fw = fopen(GALAXIA_PROCESSES."/$procname/code/templates/".$vars['normalized_name'].'.tpl','w');
00585             if (defined('GALAXIA_TEMPLATE_HEADER') && GALAXIA_TEMPLATE_HEADER) {
00586               fwrite($fw,GALAXIA_TEMPLATE_HEADER . "\n");
00587             }
00588             fclose($fw);
00589         }
00590 
00591          $this->compile_activity($pId,$activityId);
00592       
00593     }
00594     // Get the id
00595     return $activityId;
00596   }
00597   
00601   function set_interactivity($pId, $actid, $value)
00602   {
00603     $query = "update ".GALAXIA_TABLE_PREFIX."activities set isInteractive='$value' where pId=$pId and activityId=$actid";
00604     $this->query($query);
00605     // If template does not exist then create template
00606     $this->compile_activity($pId,$actid);
00607   }
00608 
00612   function set_autorouting($pId, $actid, $value)
00613   {
00614     $query = "update ".GALAXIA_TABLE_PREFIX."activities set isAutoRouted='$value' where pId=$pId and activityId=$actid";
00615     $this->query($query);
00616   }
00617 
00618   
00622   function compile_activity($pId, $activityId)
00623   {
00624     $act_info = $this->get_activity($pId,$activityId);
00625        $actname = $act_info['normalized_name'];
00626     $pm = new ProcessManager($this->db);
00627     $proc_info = $pm->get_process($pId);
00628     $compiled_file = GALAXIA_PROCESSES.'/'.$proc_info['normalized_name'].'/compiled/'.$act_info['normalized_name'].'.php';    
00629     $template_file = GALAXIA_PROCESSES.'/'.$proc_info['normalized_name'].'/code/templates/'.$actname.'.tpl';    
00630     $user_file = GALAXIA_PROCESSES.'/'.$proc_info['normalized_name'].'/code/activities/'.$actname.'.php';
00631     $pre_file = GALAXIA_LIBRARY.'/compiler/'.$act_info['type'].'_pre.php';
00632     $pos_file = GALAXIA_LIBRARY.'/compiler/'.$act_info['type'].'_pos.php';
00633     $fw = fopen($compiled_file,"wb");
00634     
00635     // First of all add an include to to the shared code
00636     $shared_file = GALAXIA_PROCESSES.'/'.$proc_info['normalized_name'].'/code/shared.php';    
00637     
00638     fwrite($fw, '<'."?php include_once('$shared_file'); ?".'>'."\n");
00639     
00640     // Before pre shared
00641     $fp = fopen(GALAXIA_LIBRARY.'/compiler/_shared_pre.php',"rb");
00642     while (!feof($fp)) {
00643         $data = fread($fp, 4096);
00644         fwrite($fw,$data);
00645     }
00646     fclose($fp);
00647 
00648     // Now get pre and pos files for the activity
00649     $fp = fopen($pre_file,"rb");
00650     while (!feof($fp)) {
00651         $data = fread($fp, 4096);
00652         fwrite($fw,$data);
00653     }
00654     fclose($fp);
00655     
00656     // Get the user data for the activity 
00657     $fp = fopen($user_file,"rb");    
00658     while (!feof($fp)) {
00659         $data = fread($fp, 4096);
00660         fwrite($fw,$data);
00661     }
00662     fclose($fp);
00663 
00664     // Get pos and write
00665     $fp = fopen($pos_file,"rb");
00666     while (!feof($fp)) {
00667         $data = fread($fp, 4096);
00668         fwrite($fw,$data);
00669     }
00670     fclose($fp);
00671 
00672     // Shared pos
00673     $fp = fopen(GALAXIA_LIBRARY.'/compiler/_shared_pos.php',"rb");
00674     while (!feof($fp)) {
00675         $data = fread($fp, 4096);
00676         fwrite($fw,$data);
00677     }
00678     fclose($fp);
00679 
00680     fclose($fw);
00681 
00682     //Copy the templates
00683     
00684     if($act_info['isInteractive']=='y' && !file_exists($template_file)) {
00685       $fw = fopen($template_file,'w');
00686       if (defined('GALAXIA_TEMPLATE_HEADER') && GALAXIA_TEMPLATE_HEADER) {
00687         fwrite($fw,GALAXIA_TEMPLATE_HEADER . "\n");
00688       }
00689       fclose($fw);
00690     }
00691     if($act_info['isInteractive']!='y' && file_exists($template_file)) {
00692       @unlink($template_file);
00693       if (GALAXIA_TEMPLATES && file_exists(GALAXIA_TEMPLATES.'/'.$proc_info['normalized_name']."/$actname.tpl")) {
00694         @unlink(GALAXIA_TEMPLATES.'/'.$proc_info['normalized_name']."/$actname.tpl");
00695       }
00696     }
00697     if (GALAXIA_TEMPLATES && file_exists($template_file)) {
00698       @copy($template_file,GALAXIA_TEMPLATES.'/'.$proc_info['normalized_name']."/$actname.tpl");
00699     }
00700   }
00701   
00706   function _get_activity_id_by_name($pid,$name)
00707   {
00708     $name = addslashes($name);
00709     if($this->getOne("select count(*) from ".GALAXIA_TABLE_PREFIX."activities where pId=$pid and name='$name'")) {
00710       return($this->getOne("select activityId from ".GALAXIA_TABLE_PREFIX."activities where pId=$pid and name='$name'"));    
00711     } else {
00712       return '';
00713     }
00714   }
00715   
00719   function _get_activity_shape($type)
00720   {
00721     switch($type) {
00722       case "start": 
00723           return "circle";
00724       case "end":
00725           return "doublecircle";
00726       case "activity":
00727           return "box";
00728       case "split":
00729           return "triangle";
00730       case "switch":
00731         return "diamond";
00732       case "join":
00733           return "invtriangle";
00734       case "standalone":
00735           return "hexagon";
00736       default:
00737           return "egg";            
00738       
00739     }
00740 
00741   }
00742 
00743   
00748   function _list_has_unvisited_nodes($list) 
00749   {
00750     foreach($list as $node) {
00751       if(!$node['visited']) return true;
00752     }
00753     return false;
00754   }
00755   
00760   function _node_in_list($node,$list)
00761   {
00762     foreach($list as $a_node) {
00763       if($node['id'] == $a_node['id']) return true;
00764     }
00765     return false;
00766   }
00767   
00772   function _normalize_name($name)
00773   {
00774     $name = str_replace(" ","_",$name);
00775     $name = preg_replace("/[^A-Za-z_]/",'',$name);
00776     return $name;
00777   }
00778   
00783   function _get_normalized_name($activityId)
00784   {
00785     return $this->getOne("select normalized_name from ".GALAXIA_TABLE_PREFIX."activities where activityId=$activityId");
00786   }
00787   
00792   function _label_nodes($pId)
00793   {
00794     
00795     
00797     $nodes = Array();
00798     // the end activity id
00799     $endId = $this->getOne("select activityId from ".GALAXIA_TABLE_PREFIX."activities where pId=$pId and type='end'");
00800     // and the number of total nodes (=activities)
00801     $cant = $this->getOne("select count(*) from ".GALAXIA_TABLE_PREFIX."activities where pId=$pId");
00802     $nodes[] = $endId;
00803     $label = $cant;
00804     $num = $cant;
00805     
00806     $query = "update ".GALAXIA_TABLE_PREFIX."activities set flowNum=$cant+1 where pId=$pId";
00807     $this->query($query);
00808     
00809     $seen = array();
00810     while(count($nodes)) {
00811       $newnodes = Array();
00812       foreach($nodes as $node) {
00813         // avoid endless loops
00814         if (isset($seen[$node])) continue;
00815         $seen[$node] = 1;
00816         $query = "update ".GALAXIA_TABLE_PREFIX."activities set flowNum=$num where activityId=$node";
00817         $this->query($query);
00818         $query = "select actFromId from ".GALAXIA_TABLE_PREFIX."transitions where actToId=".$node;
00819         $result = $this->query($query);
00820         $ret = Array();
00821         while($res = $result->fetchRow()) {  
00822           $newnodes[] = $res['actFromId'];
00823         }
00824       }
00825       $num--;
00826       $nodes=Array();
00827       $nodes=$newnodes;
00828       
00829     }
00830     
00831     $min = $this->getOne("select min(flowNum) from ".GALAXIA_TABLE_PREFIX."activities where pId=$pId");
00832     $query = "update ".GALAXIA_TABLE_PREFIX."activities set flowNum=flowNum-$min where pId=$pId";
00833     $this->query($query);
00834     
00835     //$query = "update ".GALAXIA_TABLE_PREFIX."activities set flowNum=0 where flowNum=$cant+1";
00836     //$this->query($query);
00837   }
00838     
00839 }
00840 
00841 
00842 ?>

Generated on Mon Jun 7 16:37:37 2004 for Galaxia by doxygen 1.3.4