Changeset 626 in subversion for branches/devel-vnext/program/include/rcube_contacts.inc
- Timestamp:
- Jun 22, 2007 12:51:22 PM (6 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/devel-vnext/program/include/rcube_contacts.inc
r623 r626 20 20 */ 21 21 22 /** 23 * Include rcube_result_set 24 * @ignore 25 */ 26 require_once dirname(__FILE__) . '/rcube/result_set.php'; 27 28 /** 29 * rcube_contacts 30 * 31 * @final 32 */ 22 33 class rcube_contacts 23 34 { 24 var$db = null;25 var$db_name = '';26 var$user_id = 0;27 var$filter = null;28 var$result = null;29 var$search_fields;30 var$search_string;31 var$table_cols = array('name', 'email', 'firstname', 'surname');35 protected $db = null; 36 protected $db_name = ''; 37 protected $user_id = 0; 38 protected $filter = null; 39 protected $result = null; 40 protected $search_fields; 41 protected $search_string; 42 protected $table_cols = array('name', 'email', 'firstname', 'surname'); 32 43 33 44 /** public properties */ 34 var $primary_key = 'contact_id'; 35 var $readonly = false; 36 var $list_page = 1; 37 var $page_size = 10; 38 var $ready = false; 39 45 public $primary_key = 'contact_id'; 46 public $readonly = false; 47 public $list_page = 1; 48 public $page_size = 10; 49 public $ready = false; 40 50 41 51 /** 42 52 * Object constructor 43 53 * 44 * @param object Instance of the rcube_db class 45 * @param integer User-ID 46 */ 47 function __construct($dbconn, $user) 48 { 49 $this->db = $dbconn; 54 * @access public 55 * @param object Instance of the rcube_db class 56 * @param integer User-ID 57 */ 58 public function __construct($dbconn, $user) 59 { 60 $this->db = $dbconn; 50 61 $this->db_name = rc_main::get_table_name('contacts'); 51 62 $this->user_id = $user; 52 $this->ready = $this->db && !$this->db->is_error(); 53 } 54 55 /** 56 * PHP 4 object constructor 57 * 58 * @see rcube_contacts::__construct 59 */ 60 function rcube_contacts($dbconn, $user) 61 { 62 $this->__construct($dbconn, $user); 63 } 64 65 66 /** 67 * Set internal list page 68 * 69 * @param number Page number to list 70 * @access public 71 */ 72 function set_page($page) 73 { 74 $this->list_page = (int)$page; 75 } 76 77 78 /** 79 * Set internal page size 80 * 81 * @param number Number of messages to display on one page 82 * @access public 83 */ 84 function set_pagesize($size) 85 { 86 $this->page_size = (int)$size; 87 } 88 89 90 /** 91 * Save a search string for future listings 92 * 93 * @param string SQL params to use in listing method 94 */ 95 function set_search_set($filter) 96 { 97 $this->filter = $filter; 98 } 99 100 101 /** 102 * Getter for saved search properties 103 * 104 * @return mixed Search properties used by this class 105 */ 106 function get_search_set() 107 { 108 return $this->filter; 109 } 110 111 112 /** 113 * Reset all saved results and search parameters 114 */ 115 function reset() 116 { 117 $this->result = null; 118 $this->filter = null; 119 $this->search_fields = null; 120 $this->search_string = null; 121 } 122 123 124 /** 125 * List the current set of contact records 126 * 127 * @param array List of cols to show 128 * @return array Indexed list of contact records, each a hash array 129 */ 130 function list_records($cols=null, $subset=0) 131 { 132 // count contacts for this user 133 $this->result = $this->count(); 134 $sql_result = NULL; 135 136 // get contacts from DB 137 if ($this->result->count) 138 { 139 $start_row = $subset < 0 ? $this->result->first + $this->page_size + $subset : $this->result->first; 140 $length = $subset != 0 ? abs($subset) : $this->page_size; 141 142 $sql_result = $this->db->limitquery( 143 "SELECT * FROM ".$this->db_name." 144 WHERE del<>1 145 AND user_id=?" . 146 ($this->filter ? " AND (".$this->filter.")" : "") . 147 " ORDER BY name", 148 $start_row, 149 $length, 150 $this->user_id); 151 } 152 153 while ($sql_result && ($sql_arr = $this->db->fetch_assoc($sql_result))) 154 { 155 $sql_arr['ID'] = $sql_arr[$this->primary_key]; 156 // make sure we have a name to display 157 if (empty($sql_arr['name'])) 158 $sql_arr['name'] = $sql_arr['email']; 159 $this->result->add($sql_arr); 160 } 161 162 return $this->result; 163 } 164 165 166 /** 167 * Search contacts 168 * 169 * @param array List of fields to search in 170 * @param string Search value 171 * @param boolean True if results are requested, False if count only 172 * @return Indexed list of contact records and 'count' value 173 */ 174 function search($fields, $value, $select=true) 175 { 176 if (!is_array($fields)) 177 $fields = array($fields); 178 179 $add_where = array(); 180 foreach ($fields as $col) 181 { 182 if ($col == 'ID' || $col == $this->primary_key) 183 { 184 $ids = !is_array($value) ? split(',', $value) : $value; 185 $add_where[] = $this->primary_key." IN (".join(',', $ids).")"; 186 } 187 else 188 $add_where[] = $this->db->quoteIdentifier($col)." LIKE ".$this->db->quote(strlen($value)>2 ? "%$value%" : "$value%"); 189 } 190 191 if (!empty($add_where)) 192 { 193 $this->set_search_set(join(' OR ', $add_where)); 194 if ($select) 195 $this->list_records(); 196 else 63 $this->ready = $this->db && !$this->db->is_error(); 64 } 65 66 /** 67 * Set internal list page 68 * 69 * @param number Page number to list 70 * @access public 71 */ 72 function set_page($page) 73 { 74 $this->list_page = (int)$page; 75 } 76 77 78 /** 79 * Set internal page size 80 * 81 * @param number Number of messages to display on one page 82 * @access public 83 */ 84 function set_pagesize($size) 85 { 86 $this->page_size = (int)$size; 87 } 88 89 90 /** 91 * Save a search string for future listings 92 * 93 * @param string SQL params to use in listing method 94 */ 95 function set_search_set($filter) 96 { 97 $this->filter = $filter; 98 } 99 100 101 /** 102 * Getter for saved search properties 103 * 104 * @return mixed Search properties used by this class 105 */ 106 function get_search_set() 107 { 108 return $this->filter; 109 } 110 111 112 /** 113 * Reset all saved results and search parameters 114 */ 115 function reset() 116 { 117 $this->result = null; 118 $this->filter = null; 119 $this->search_fields = null; 120 $this->search_string = null; 121 } 122 123 124 /** 125 * List the current set of contact records 126 * 127 * @param array List of cols to show 128 * @return array Indexed list of contact records, each a hash array 129 */ 130 function list_records($cols=null, $subset=0) 131 { 132 // count contacts for this user 197 133 $this->result = $this->count(); 198 } 199 200 return $this->result; 201 } 202 203 204 /** 205 * Count number of available contacts in database 206 * 207 * @return Result array with values for 'count' and 'first' 208 */ 209 function count() 210 { 211 // count contacts for this user 212 $sql_result = $this->db->query( 213 "SELECT COUNT(contact_id) AS rows 214 FROM ".$this->db_name." 215 WHERE del<>1 216 AND user_id=?". 217 ($this->filter ? " AND (".$this->filter.")" : ""), 218 $this->user_id); 219 220 $sql_arr = $this->db->fetch_assoc($sql_result); 221 return new rcube_result_set($sql_arr['rows'], ($this->list_page-1) * $this->page_size);; 222 } 223 224 225 /** 226 * Return the last result set 227 * 228 * @return Result array or NULL if nothing selected yet 229 */ 230 function get_result($as_res=true) 231 { 232 return $this->result; 233 } 234 235 236 /** 237 * Get a specific contact record 238 * 239 * @param mixed record identifier(s) 240 * @return Result object with all record fields or False if not found 241 */ 242 function get_record($id, $assoc=false) 243 { 244 // return cached result 245 if ($this->result && ($first = $this->result->first()) && $first[$this->primary_key] == $id) 246 return $assoc ? $first : $this->result; 247 248 $this->db->query( 249 "SELECT * FROM ".$this->db_name." 250 WHERE contact_id=? 251 AND user_id=? 252 AND del<>1", 253 $id, 254 $this->user_id); 255 256 if ($sql_arr = $this->db->fetch_assoc()) 257 { 258 $sql_arr['ID'] = $sql_arr[$this->primary_key]; 259 $this->result = new rcube_result_set(1); 260 $this->result->add($sql_arr); 261 } 262 263 return $assoc && $sql_arr ? $sql_arr : $this->result; 264 } 134 $sql_result = NULL; 135 136 // get contacts from DB 137 if ($this->result->count) { 138 $start_row = $subset < 0 ? $this->result->first + $this->page_size + $subset : $this->result->first; 139 $length = $subset != 0 ? abs($subset) : $this->page_size; 140 141 $_query = "SELECT * FROM " . $this->db_name; 142 $_query.= " WHERE del<>1"; 143 $_qiery.= " AND user_id=?"; 144 $_query.= ($this->filter ? " AND (".$this->filter.")" : ""); 145 $_query.= " ORDER BY name"; 146 147 $sql_result = $this->db->limitquery( 148 $_query, 149 $start_row, 150 $length, 151 $this->user_id 152 ); 153 } 154 155 while ($sql_result && ($sql_arr = $this->db->fetch_assoc($sql_result))) { 156 $sql_arr['ID'] = $sql_arr[$this->primary_key]; 157 // make sure we have a name to display 158 if (empty($sql_arr['name'])) { 159 $sql_arr['name'] = $sql_arr['email']; 160 } 161 $this->result->add($sql_arr); 162 } 163 return $this->result; 164 } 165 166 167 /** 168 * Search contacts 169 * 170 * @param array List of fields to search in 171 * @param string Search value 172 * @param boolean True if results are requested, False if count only 173 * @return Indexed list of contact records and 'count' value 174 */ 175 function search($fields, $value, $select=true) 176 { 177 if (!is_array($fields)) { 178 $fields = array($fields); 179 } 180 $add_where = array(); 181 foreach ($fields as $col) { 182 if ($col == 'ID' || $col == $this->primary_key) { 183 $ids = !is_array($value) ? split(',', $value) : $value; 184 $add_where[] = $this->primary_key." IN (".join(',', $ids).")"; 185 } 186 else { 187 $_where = $this->db->quoteIdentifier($col); 188 $_where.= " LIKE " . $this->db->quote(strlen($value)>2 ? "%$value%" : "$value%"); 189 $add_where[] = $_where; 190 } 191 } 192 193 if (!empty($add_where)) { 194 $this->set_search_set(join(' OR ', $add_where)); 195 if ($select) { 196 $this->list_records(); 197 } 198 else { 199 $this->result = $this->count(); 200 } 201 } 202 return $this->result; 203 } 204 205 206 /** 207 * Count number of available contacts in database 208 * 209 * @return Result array with values for 'count' and 'first' 210 */ 211 function count() 212 { 213 $_query = "SELECT COUNT(contact_id) AS rows"; 214 $_query.= " FROM " . $this->db_name; 215 $_query.= " WHERE del<>1"; 216 $_query.= " AND user_id=?"; 217 $_query.= ($this->filter ? " AND (".$this->filter.")" : ""); 218 219 // count contacts for this user 220 $sql_result = $this->db->query($_query, $this->user_id); 221 $sql_arr = $this->db->fetch_assoc($sql_result); 222 return new rcube_result_set( 223 $sql_arr['rows'], 224 ($this->list_page-1) * $this->page_size 225 ); 226 } 227 228 229 /** 230 * Return the last result set 231 * 232 * @return Result array or NULL if nothing selected yet 233 */ 234 function get_result($as_res=true) 235 { 236 return $this->result; 237 } 238 239 240 /** 241 * Get a specific contact record 242 * 243 * @param mixed record identifier(s) 244 * @return Result object with all record fields or False if not found 245 */ 246 function get_record($id, $assoc=false) 247 { 248 // return cached result 249 if ( 250 $this->result 251 && ($first = $this->result->first()) 252 && $first[$this->primary_key] == $id 253 ) { 254 return $assoc ? $first : $this->result; 255 } 256 257 $_query = "SELECT * FROM ".$this->db_name; 258 $_query.= " WHERE contact_id=?"; 259 $_query.= " AND user_id=?"; 260 $_query.= " AND del<>1"; 261 262 $this->db->query($_query, $id, $this->user_id); 263 264 if ($sql_arr = $this->db->fetch_assoc()) { 265 $sql_arr['ID'] = $sql_arr[$this->primary_key]; 266 $this->result = new rcube_result_set(1); 267 $this->result->add($sql_arr); 268 } 269 return $assoc && $sql_arr ? $sql_arr : $this->result; 270 } 265 271 266 272 … … 302 308 303 309 return $insert_id; 304 } 305 306 307 /** 308 * Insert new contacts for each row in set 309 */ 310 function insert_recset($result, $check=false) 311 { 312 $ids = array(); 313 while ($row = $result->next()) 314 { 315 if ($insert = $this->insert($row, $check)) 316 $ids[] = $insert; 317 } 318 return $ids; 319 } 320 321 322 /** 323 * Update a specific contact record 324 * 325 * @param mixed Record identifier 326 * @param array Assoziative array with save data 327 * @return True on success, False on error 328 */ 329 function update($id, $save_cols) 330 { 331 $updated = false; 332 $write_sql = array(); 333 foreach ($this->table_cols as $col) 334 if (isset($save_cols[$col])) 335 $write_sql[] = sprintf("%s=%s", $this->db->quoteIdentifier($col), $this->db->quote($save_cols[$col])); 336 337 if (!empty($write_sql)) 338 { 339 $this->db->query( 340 "UPDATE ".$this->db_name." 341 SET changed=".$this->db->now().", ".join(', ', $write_sql)." 342 WHERE contact_id=? 343 AND user_id=? 344 AND del<>1", 345 $id, 346 $this->user_id); 347 348 $updated = $this->db->affected_rows(); 349 } 350 351 return $updated; 352 } 353 354 355 /** 356 * Mark one or more contact records as deleted 357 * 358 * @param array Record identifiers 359 */ 360 function delete($ids) 361 { 362 if (is_array($ids)) 363 $ids = join(',', $ids); 364 365 $this->db->query( 366 "UPDATE ".$this->db_name." 367 SET del=1 368 WHERE user_id=? 369 AND contact_id IN (".$ids.")", 370 $this->user_id); 371 372 return $this->db->affected_rows(); 373 } 374 310 } 311 312 313 /** 314 * Insert new contacts for each row in set 315 */ 316 function insert_recset($result, $check=false) 317 { 318 $ids = array(); 319 while ($row = $result->next()) { 320 if ($insert = $this->insert($row, $check)) { 321 $ids[] = $insert; 322 } 323 } 324 return $ids; 325 } 326 327 328 /** 329 * Update a specific contact record 330 * 331 * @param mixed Record identifier 332 * @param array Assoziative array with save data 333 * @return True on success, False on error 334 */ 335 function update($id, $save_cols) 336 { 337 $updated = false; 338 $write_sql = array(); 339 foreach ($this->table_cols as $col) { 340 if (isset($save_cols[$col])) { 341 $write_sql[] = sprintf("%s=%s", $this->db->quoteIdentifier($col), $this->db->quote($save_cols[$col])); 342 } 343 } 344 if (!empty($write_sql)) { 345 $_query= "UPDATE " . $this->db_name; 346 $_query.= " SET changed=" . $this->db->now() . ","; 347 $_query.= " " . join(', ', $write_sql); 348 $_query.= " WHERE contact_id=?"; 349 $_query.= " AND user_id=?"; 350 $_query.= " AND del<>1"; 351 $this->db->query($_query, $id, $this->user_id); 352 353 $updated = $this->db->affected_rows(); 354 } 355 return $updated; 356 } 357 358 359 /** 360 * Mark one or more contact records as deleted 361 * 362 * @param array Record identifiers 363 */ 364 function delete($ids) 365 { 366 if (is_array($ids)) { 367 $ids = join(',', $ids); 368 } 369 $_query = 370 $this->db->query($_query, $this->user_id); 371 372 return $this->db->affected_rows(); 373 } 375 374 } 376 377 378 /**379 * RoundCube result set class.380 * Representing an address directory result set.381 */382 class rcube_result_set383 {384 var $count = 0;385 var $first = 0;386 var $current = 0;387 var $records = array();388 389 function __construct($c=0, $f=0)390 {391 $this->count = (int)$c;392 $this->first = (int)$f;393 }394 395 function rcube_result_set($c=0, $f=0)396 {397 $this->__construct($c, $f);398 }399 400 function add($rec)401 {402 $this->records[] = $rec;403 }404 405 function iterate()406 {407 return $this->records[$this->current++];408 }409 410 function first()411 {412 $this->current = 0;413 return $this->records[$this->current++];414 }415 416 // alias417 function next()418 {419 return $this->iterate();420 }421 422 function seek($i)423 {424 $this->current = $i;425 }426 427 }428 429 430 375 ?>
Note: See TracChangeset
for help on using the changeset viewer.
