ruạṛ
<?php /** * Testimonials classes file * @version 0.10 * @author Robert Urquhart <programmer@activatedesign.co.nz> * @package WEP-CMS * @since braziers.co.nz */ /** * option_list object * contains list_option objects */ class testimonial { /** * define variables matched to to database fields * may not have identical names, see $this->load_from_data for conversions */ var $testimonial_id , $type_id , $witness , $testimony , $active , $position ; /* * lists * @var object $types */ var $types; /** * object construction function * @param int $id unique database record id */ function __construct($id=0) { /* * set minimum visibility */ $this->testimonial_id = 0; /** * defaults */ $this->types = new options_list(TESTIMONIALS_LIST); $this->types->load_options(false); /** * if no id supplied simply prepare object to be populated from dataset eg in creation script */ if(!$id) { return; } /** * else get object data * assumes database connection already established at global level * @var resource $category mysql dataset */ $record = mysql_query("select * from testimonials where testimonial_id = '$id' "); if($record && mysql_num_rows($record)>0) { $this->load_from_data(mysql_fetch_assoc($record)); mysql_free_result($record); // clean up } return; } /** * populate object - can be done on init or manually * @param array $d data; */ function load_from_data($d=array()) { if(!empty($d)) { $this->testimonial_id = $d['testimonial_id']; $this->type_id = $d['type_id']; $this->witness = $d['witness']; $this->testimony = $d['testimony']; $this->active = $d['active']; $this->position = $d['position']; } return; } /** * output text for type */ function type() { return $this->types->get_option_by_id($this->type_id)->text; } /** * add record to the database * @return bool */ function create() { global $message; /** * get from POST */ $this->type_id = is_numeric_id($_POST['type_id'],0); $this->witness = clean_plain_data($_POST['witness']); $this->testimony = clean_plain_data($_POST['testimony']); $this->active = (isset($_POST['active'])) ? 1 : 0; $this->position = is_numeric_id($_POST['position'],0); /** * validate required fields */ $m = ''; /* if($this->witness==''){ $m .= 'Please enter a witness name <br />'; } if($this->testimony==''){ $m .= 'Please enter testimony <br />'; } if($m != '') { $message .= $m; return false; } */ /* * defaults if not supplied */ //$message .= var_dump($this->types->get_option_by_id($this->type_id)); if(!$this->types->get_option_by_id($this->type_id)) { $this->type_id = $this->types->options[0]->option_id; } if(!$this->position) { $this->position = select_one('testimonials','max(position)') + 10; } $fields = 'type_id , witness , testimony , active , position '; $values = "'$this->type_id' , '$this->witness' , '$this->testimony' , '$this->active' , '$this->position' "; $query = "insert into testimonials ( $fields ) values ( $values )"; if(!mysql_query($query)) { $message .= mysql_error(); return false; } $this->testimonial_id = mysql_insert_id(); $message .= 'Testimonial record created <br />'; return true; } /** * update record in the database * @return bool */ function update() { global $message; /** * san check */ if(!$this->testimonial_id) { $message .= 'Invalid list <br />'; return false; } /** * get from POST */ $this->type_id = is_numeric_id($_POST['type_id'],0); $this->witness = clean_plain_data($_POST['witness']); $this->testimony = clean_plain_data($_POST['testimony']); /** * validate required fields */ $m = ''; if($this->witness==''){ $m .= 'Please enter a witness name <br />'; } if($this->testimony==''){ $m .= 'Please enter testimony <br />'; } if($m != '') { $message .= $m; return false; } //$message .= var_dump($this->type_id,$this->types,$this->types->get_option_by_id($this->type_id),true); if(!$this->types->get_option_by_id($this->type_id)) { $this->type_id = $this->types->options[0]->option_id; } /** * @var string $query update query */ $query = "update testimonials set type_id = '$this->type_id' , witness = '$this->witness' , testimony = '$this->testimony' where testimonial_id = '$this->testimonial_id'"; $update = mysql_query($query); if(!$update) { $message .= mysql_error(); return false; } $message .= 'Testimonial record updated <br />'; return true; } /* * remove all-the-things * @return bool */ function delete() { global $message; /** * san check */ if(!is_numeric_id($this->testimonial_id,0)) { $message .= 'Unable to delete testimonial: invalid id <br />'; return false; } /** * remove (delete) from database */ return $this->remove_from_database(); } /** * remove record from database * @return bool */ function remove_from_database() { global $message; /** * san check */ if(!is_numeric_id($this->testimonial_id,0)) { $message .= 'Unable to remove testimonial from database: invalid id <br />'; return false; } /** * delete record * @var string $query delete query * @return bool */ $query = "delete from testimonials where testimonial_id = '$this->testimonial_id'"; $result = mysql_query($query); if(!$result || mysql_affected_rows() != 1) { $message .= 'There was an error '.mysql_error().'<br />Affected rows: '.mysql_affected_rows(); return false; } // $message .= 'Testimonial deleted <br />'; return true; } } ?>
cải xoăn