- 01
 - 02
 - 03
 - 04
 - 05
 - 06
 - 07
 - 08
 - 09
 - 10
 - 11
 - 12
 - 13
 - 14
 - 15
 - 16
 - 17
 - 18
 - 19
 - 20
 - 21
 - 22
 - 23
 - 24
 - 25
 - 26
 - 27
 - 28
 - 29
 - 30
 - 31
 - 32
 - 33
 - 34
 - 35
 - 36
 - 37
 - 38
 - 39
 - 40
 - 41
 - 42
 - 43
 - 44
 - 45
 - 46
 - 47
 - 48
 - 49
 - 50
 - 51
 - 52
 - 53
 - 54
 - 55
 - 56
 - 57
 - 58
 - 59
 - 60
 - 61
 - 62
 - 63
 - 64
 - 65
 - 66
 - 67
 - 68
 - 69
 - 70
 - 71
 - 72
 - 73
 - 74
 - 75
 - 76
 - 77
 - 78
 - 79
 - 80
 - 81
 - 82
 - 83
 - 84
 - 85
 - 86
 - 87
 - 88
 - 89
 - 90
 - 91
 - 92
 - 93
 - 94
 - 95
 - 96
 - 97
 
                        class Relay {
  protected $_data = array();
  protected $_devices;
  static protected $_db_fields = array();
  function getId() {
    return $this->_data['id'];
  }
  static function load($id) {
    if ($id) {
      $select = db_select('relay', 'r');
      $select->fields('r');
      $select->condition('r.id', $id);
      $relay = $select->execute()->fetchObject(__CLASS__);
      return $relay;
    }
  }
  function save() {
    foreach (array_diff(array_keys($this->_data), self::_getPureDbFields('relay')) as $field) {
      $this->_data['data'] = $this->_data[$field];
    }
    if ($this->getId()) {
      drupal_write_record('relay', $this->_data, array('id'));
    }
    else {
      drupal_write_record('relay', $this->_data);
    }
    $this->_saveDevices();
  }
  protected function _saveDevices() {
    if ($this->getId()) {
      $delete = db_delete('relay_devices');
      $delete->condition('relay_id', $this->getId());
      $delete->execute();
      foreach ((array)$this->_devices as $device) {
        $device = (array) $device;
        foreach (array_diff(array_keys($device), self::_getPureDbFields('relay_devices')) as $field) {
          $device['data'] = $device[$field];
        }
        drupal_write_record('relay_devices', $device);
      }
    }
  }
  function getDateFrom() {
    return $this->_data['date_from'];
  }
  function getDateDuration(){
    return $this->_data['date_duration'];
  }
  function getDateTo(){
    return $this->getDateFrom() + $this->getDateDuration();
  }
  function getDevices(){
    $this->_ensureDevicesLoaded();
    return $this->_devices;
  }
  protected function _ensureDevicesLoaded() {
    if (!is_array($this->_devices)) {
      $select = db_select('relay_devices', 'rd');
      $select->fields('rd');
      $select->condition('rd.relay_id', $this->getId());
      $query = $select->execute();
      $this->_devices = array_map('drupal_unpack', $query->fetchAll());
    }
    return is_array($this->_devices);
  }
  function __construct($data = NULL) {
    if (is_array($data)) {
      foreach ($data as $key => $value) {
        $this->_data[$key] = $value;
      }
    }
    elseif (is_string($this->_data['data']) && !empty($this->_data['data'])) {
      drupal_unpack($this);
    }
  }
  function __set($name, $value) {
    return $this->_data[$name] = $value;
  }
  function __get($name) {
    return $this->_data[$name];
  }
  static protected function _getPureDbFields($table) {
    if (!isset(self::$_db_fields[$table])) {
      $schema = drupal_get_schema($table);
      $fields = $schema['fields'];
      unset($fields['data']);
      self::$_db_fields[$table] = array_keys($fields);
    }
    return self::$_db_fields[$table];
  }
}