mid) { $ret = drupal_write_record('sms',$record,'mid'); } else { $ret = drupal_write_record('sms',$record); } $sms->mid = $obj->mid;//set actual MID to $sms break; } } /** * converts the message object to storable object for database. * * @param $sms * the sms object used by sms.module */ function _sms_dbstorage_sms2record($sms) { $record = (object) array( 'mid' => ($sms->mid) ? $sms->mid : NULL, 'direction' => $sms->direction, 'type' => $sms->type, 'text' => $sms->text, 'sender' => $sms->sender, 'recipient' => $sms->recipient, 'sms' => $sms, 'timestamp' => ($sms->timestamp) ? $message->timestamp : time(), ); return $record; } /** * load one or more sms from database * * @param $mid * either an integer for a special message ID * or an array of conditioned columns: key is column, value will be value * value can be an array of values (so they will be ORed on the given key) * @return * a single $sms object - if $mid is integer * or an array of $sms objects * will return FALSE for no sms found */ function sms_dbstorage_load($mid) { //compose the query with ANDed WHEREs and escaped ARGs $query = "SELECT sms FROM {sms} WHERE"; $wheres = array(); $args = array(); //defines the valid columns with its type specifications $specs = array( 'mid' => "%d", 'direction' => "'%s'", 'type' => "'%s'", 'sender' => "'%s'", 'recipient' => "'%s'", 'text' => "'%s'", 'timestamp' => "%d", ); //either array or integer if (is_array($mid)) { foreach ($mid as $key => $val) { //only keys in $specs are valid for search if (!in_array($key, array_keys($specs))) { //nothing } //multiple values for the same column will be ORed elseif (is_array($val)) { $spec = $specs{$key}; $wheres[] = implode(" OR ", array_fill(0, count($val), "$key = $spec")); $args[] += $val; } else { $wheres[] = "$key = $spec"; $args[] = $val; } } } else { $wheres[] = "mid = %d"; $args[] = $mid; } //AND WHEREs $where = implode(" AND ", $wheres); $res = db_query("$query $where", $args); //either return single object or array of objects if (!is_array($mid)) { $sms = db_result($res); if ($sms) { return unserialize($sms); } else { return FALSE; } } else { $return = array(); while ($ret = db_fetch_array($res)) { $return[] = unserialize($ret['sms']); } if (!count($return)) return FALSE; return $return; } }