Skip to content
Snippets Groups Projects
Select Git revision
  • 8c8b4a81f0e6ed2908990c9009f610632430b11b
  • master default
  • labcomm2014_tc31
  • labcomm2014
  • js
  • java_dyn_msg_dec
  • anders.blomdell
  • typeref
  • pragma
  • compiler-refactoring
  • labcomm2013
  • v2014.1
  • v2014.0
  • v2013.0
14 results

labcomm_fd_reader_writer.c

Blame
  • Forked from Anders Blomdell / LabComm
    Source project has a limited visibility.
    class.Group.php 4.35 KiB
    <?php
    
    class Group {
    
      private static $maildir = '/home/leif/slask/maildir';
    
    
      private static $queryList = <<<EOS
        select ID, Gruppnamn, GruppInfo from Grupper order by Gruppnamn
    EOS;
    
      private static $queryMembership = <<<EOS
        select Grupper.ID, Gruppnamn from Grupper,GruppAdress where 
        GruppID = Grupper.ID and Adressid = ? order by Gruppnamn
    EOS;
    
      private static $querySetGroup = <<<EOS
        replace into GruppAdress set AdressID = ?, GruppID = ?
    EOS;
    
      private static $queryUnsetGroup = <<<EOS
        delete from GruppAdress where AdressID = ? and GruppID = ?
    EOS;
    
      private static $queryMail = <<<EOS
        select Epost from Adresser, GruppAdress where
        Adresser.ID = AdressID and GruppID = ?
    EOS;
    
      private static $groupFields = array('ID', 'Gruppnamn', 'GruppInfo');
    
    
      private static $instance;
    
      private static $list = array();
    
      private function __construct($param = NULL) {
        foreach (self::$groupFields as $field) {$this->$field = '';}
        if (is_null($param)) return;
        if (is_array($param)) {
          foreach (self::$groupFields as $field) {
    	$this->$field = $param[$field];
          }
        }
      }
    
      public static function getGroupList() {
        if (count(self::$list) > 0) return $list;
        global $gCms; if (!isset($gCms)) exit;
        $c = __CLASS__;
        $db = AddressDB::getDB();
        $result = $db->Execute(self::$queryList,array());
        if ($result === false) { die("Database error!<br>" . $db->ErrorMsg());}
        while ($row = $result->FetchRow()) {
          self::$list[] = new $c($row);
        }
        return self::$list;
      }
    	
      /*   global $gCms; */
      /*   if (!isset($gCms)) exit; */
      /*   $ADB = AddressDB::getDB(); */
      /*   $result = $ADB->Execute(self::$queryList,array()); */
      /*   while ($row = $result->FetchRow()) { */
      /*     $this->list[$row['Gruppnamn']] = $row['ID']; */
      /*   } */
      /* } */
      
      public static function getMembers($GruppID) {
        global $gCms; if (!isset($gCms)) exit;
        $db = AddressDB::getDB();
        $result = $db->Execute(self::$queryMembership,array($GruppID));
        if ($result === false) { die("Database error!<br>" . $db->ErrorMsg()); }
        $list = array();
        while ($row = $result->FetchRow()) {
          $list[$row['Gruppnamn']] = $row['ID'];
        }
        return $list;
      }
    
      public static function getNonMembers($GruppID) {
        $members = self::getMembers($GruppID);
        $allgroups = array();
        self::getGroupList();
        foreach (self::$list as $group) {
          $allgroups[$group->Gruppnamn] = $group->ID;
        }
        $nonmembers = array_diff_assoc($allgroups,$members);
        return $nonmembers;
      }
        
      public static function setMember($ID,$GroupID) {
        global $gCms; if (!isset($gCms)) exit;
        $db = AddressDB::getDB();
        $values = array($ID, $GroupID);
        $result = $db->Execute(self::$querySetGroup,$values);
        if ($result === false) {die("Database error!<br>" . $db->ErrorMsg()); }
      }  
    
      public static function unsetMember($ID,$GroupID) {
        global $gCms; if (!isset($gCms)) exit;
        $db = AddressDB::getDB();
        $values = array($ID, $GroupID);
        //   echo '<pre>'; print_r($queryUnsetGroup); print_r($values); echo '</pre>';exit;
        
        $result = $db->Execute(self::$queryUnsetGroup,$values);
        if ($result === false) {die("Database error!<br>" . $db->ErrorMsg()); }
      }  
    
      /* public static function getGroup() { */
      /*   if (!isset(self::$instance)) { */
      /*     $c = __CLASS__; */
      /*     self::$instance = new $c; */
      /*   } */
      /*   return self::$instance->list; */
      /* } */
    
      public static function setMailLists() {
        global $gCms; if (!isset($gCms)) exit;
        $db = AddressDB::getDB();
        $groups = self::getGroupList();
        if(!is_dir(self::$maildir)) mkdir(self::$maildir,'0755',true);
        foreach ($groups as $group) {
          $pos = strrpos($group->Gruppnamn, '.dis');
          if ($pos === false) continue;
          $epost = array();
          $values = array($group->ID);
          $result = $db->Execute(self::$queryMail,$values);
          if ($result === false) { die("Database error!<br>" . $db->ErrorMsg()); }
          while ($row = $result->FetchRow()) {
    	if (!$row['Epost']) continue;
    	$epost[] = $row['Epost'];
          }
          if (count($epost) > 0) {
    	//	echo '<pre>'; print_r($epost); echo '</pre>';
    	$filename  = self::$maildir . '/' . $group->Gruppnamn;
    	$file = fopen($filename,'w');
    	if ($file === false) die('Cannot write '.$filename);
    	foreach ($epost as $line) fwrite($file,$line.PHP_EOL);
    	fclose($file);
          }
        }
        //  exit;
      }
    }
    ?>