Skip to content
Snippets Groups Projects
Select Git revision
  • 118fb412e8d2198bd5dd85eba7bb0a3a1d80d275
  • master default
  • labcomm2006
  • typedefs
  • anders.blomdell
  • typeref
  • pragma
  • compiler-refactoring
  • labcomm2013
  • v2014.4
  • v2006.0
  • v2014.3
  • v2014.2
  • v2014.1
  • v2014.0
  • v2013.0
16 results

labcomm2006.h

Blame
  • Forked from Anders Blomdell / LabComm
    Source project has a limited visibility.
    class.Person.php 5.77 KiB
    <?php
    
    class Person {
    
      private static $queryPersons = <<<EOT
        select distinct Adresser.ID, concat(Fornamn, ' ', Efternamn) as Namn,
        case when SkolaForetag is not null then SkolaForetag
           when Adress is not null then Adress else Hem end as ForetagAdress,
        Avdelning from Adresser,GruppAdress where 
    EOT;
    
      private static $queryOne = <<<EOT
        select * from Adresser where ID=?
    EOT;
    
      private static $queryGroups =  <<<EOT
        SELECT Grupper.ID,Gruppnamn FROM `Grupper`, GruppAdress WHERE 
        Grupper.ID=GruppID and AdressID = ?
    EOT;
    
      private static $queryGroupMembers = <<<EOS
        select distinct *, concat(Fornamn, ' ', Efternamn) as Namn
        from Adresser,GruppAdress where Adresser.ID=AdressID and GruppID=?
    EOS;
      
      public static $firstFields = array(
        'ID','fornamn', 'efternamn', 'foretag','avdelning', 'adress', 'Grupp');
    
      private static $personFields;
    
      public function __construct($param = NULL) {
        global $gCms; if (!isset($gCms)) exit;
        if (!isset(self::$personFields)) {
          self::$personFields = AddressDB::getFields('Adresser');
        }
        foreach(self::$personFields as $field) {
          $this->$field = '';
        }
        $this->Namn = ''; $this->ForetagAdress = '';
        $this->Grupper = array();
        if (is_null($param)) return;
        if (is_array($param)) {
          if (isset($param['ID'])) $this->fromDB($param['ID']);
          foreach (self::$personFields as $field) {
    	if ($field == 'ID') continue;
            if (isset($param[$field])) $this->$field = $param[$field];
          }
          return;
        }
        $this->fromDB($param);
      }
    
      private function fromDB($adressID) {
        global $gCms; if (!isset($gCms)) exit;
        $db = AddressDB::getDB();
        $values = array($adressID);
        $result = $db->Execute(self::$queryOne,$values);
        if ($result === false) { die("Database error!<br>" . $db->ErrorMsg()); }
        $count = $result->RecordCount();
        switch ($count) {
        case 0:
          die(sprintf('Bad address ID %s'.PHP_EOL,$adressID));
          break;
        case 1:
          $row = $result->FetchRow();
          $nn = self::$personFields;
          foreach ($nn as $field) {
    	if (isset($row[$field])) $this->$field = $row[$field];
          }
          break;
        default:
          die(sprintf(
             'Strange error. Exactly one address record expected for key %s',
             $authkey));
        }
        $this->getGroups($adressID);
      }
    
      private function getGroups($adressID) {
        global $gCms; if (!isset($gCms)) exit;
        $db = AddressDB::getDB();
        $values = array($adressID);
        $result = $db->Execute(self::$queryGroups,$values);
        if (!$result) { die("Database error!<br>" . $db->ErrorMsg()); }
        while($row = $result->FetchRow()) {
          $this->Grupper[$row['Gruppnamn']] = $row['ID'];
        }
      }
    
      private function createRecord() {
        $db = AddressDB::getDB();
        $query = 'insert into Adresser set Andrad=null';
        //    $values = array('Förnamn');
        $result = $db->Execute($query,array());
        if ($result === false) {die("Database error!<br>" . $publDB->ErrorMsg()); }
        $this->ID = $db->Insert_ID();
      }
     
    
    
      public function updateRecord() {
        global $gCms; if (!isset($gCms)) exit;
        $db = AddressDB::getDB();
        $query = 'update Adresser set ';
        $qfields = array(); $values = array();
        if ($this->ID == '') { $this->createRecord(); }
        foreach (self::$personFields as $field) {
          if ($field == 'ID') continue;
          if (!isset($this->$field)) $this->field = 'null';
          $val = trim($this->$field);
          if (strlen($val) == 0) $val = null;
          if ($field == 'Andrad') $val = null;
          $qfields[] = sprintf('%s = ?',$field);
          $values[] = $val;
        }
        $values[] = $this->ID;
        $query .= join(', ',$qfields) . ' where ID=?';
        //    echo '<pre>'; print_r($query); echo '</pre>';
        //    echo '<pre>'; print_r($values); echo '</pre>';
        
        $result = $db->Execute($query,$values);
        if ($result === false) {die("Database error!<br>" . $db->ErrorMsg());}
      }
    
      public static function searchPersons($formValues) {
        global $gCms; if (!isset($gCms)) exit;
        $andQuery = array(); $orQuery = ''; 
        $andValues = array(); $orValues = array();
        foreach ($formValues as $key => $value) {
          switch ($key) {
          case 'fornamn' :
    	$andQuery[] = 'Fornamn like ?';
            $andValues[] = '%'.$value.'%';
    	break;
          case 'efternamn':
    	$andQuery[] = 'Efternamn like ?';
    	$andValues[] = '%'.$value.'%';
    	break;
          case 'foretag':
    	$andQuery[] = 'SkolaForetag like ?';
    	$andValues[] = '%'.$value.'%';
    	break;
          case 'avdelning':
    	$andQuery[] = 'Avdelning like ?';
    	$andValues[] = '%'.$value.'%';
    	break;
          case 'adress':
    	$andQuery[] = '(Adress like ? or Hem like ?)';
    	$andValues[] =  '%'.$value.'%';
    	$andValues[] =  '%'.$value.'%';
    	break;
          case 'Grupp':
    	$andQuery[] = 'AdressID=Adresser.ID and GruppID = ?';
    	$andValues[] =  $value;
    	break;
          default:
          }
          $query = self::$queryPersons .' ' . join(' and ', $andQuery);
          $values = $andValues;
        }
        $db = AddressDB::getDB();
        $class = __CLASS__;
        $records = array();
        $result = $db->Execute($query,$values);
        if (!$result) { die("Database error!<br>" . $db->ErrorMsg()); }
        while ($row = $result->FetchRow()) {
          $person = new $class();
          foreach ($row as $key => $value) {
    	$person->$key = $value;
          }
          $records[] = $person;
        }
        return $records;
      }
      public static function getGroupMembers($groupID) {
        global $gCms; if (!isset($gCms)) exit;
        $db = AddressDB::getDB();
        $class = __CLASS__;
        $records = array();
        $values = array($groupID);
        $result = $db->Execute(self::$queryGroupMembers,$values);
        if (!$result) { die("Database error!<br>" . $db->ErrorMsg()); }
        while ($row = $result->FetchRow()) {
          $person = new $class();
          foreach ($row as $key => $value) {
    	$person->$key = $value;
          }
          $records[] = $person;
        }
        return $records;
      }
    }
    ?>