ruạṛ
<?php /** * List classes file * @version 0.10 * @author Robert Urquhart <programmer@activatedesign.co.nz> * @package WEP-CMS * @since edgewaremowers.co.nz */ /** * option_list object * contains list_option objects */ class options_list { /** * define variables matched to to database fields * may not have identical names, see $this->load_from_data for conversions */ var $list_id , $name , $description ; /** * containers * @var array $options */ var $options = array(); /** * object construction function * @param int $id unique database record id */ function __construct($id=0) { /* * set minimum visibility */ $this->list_id = 0; /** * 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 lists where list_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->list_id = $d['list_id']; $this->name = $d['list_name']; $this->description = $d['list_description']; } return; } /** * load options * @var bool $all * @return bool */ function load_options($all=false) { global $message; /* * reset */ $this->options = array(); /* * san check */ if(!$this->list_id) { return false; } //else $query = "select * from list_options where list_id='".$this->list_id ."'"; $query .= ($all) ? '' : " and active = '1'"; $query .= " order by position"; if(!$options = mysql_query($query)) { $message .= mysql_error(); return false; } //else while ($row = mysql_fetch_assoc($options)) { $o = new list_option(); $o->load_from_data($row); $this->options[] = $o; unset($o); } return true; } /** * count the number of options in this list; load if necessary * @param bool $all flag to switch between all/active if loading; by default only load active * @return int */ function count_options($all=false) { if(empty($this->options)) { $this->load_options($all); } return count($this->options); } /** * extract a particular option based on ID * @var int $id; * @return mixed; */ function get_option_by_id($id=0) { /* * san check */ if(!is_numeric_id($id,0)) { return false; } //else foreach($this->options as $o) { if($o->option_id == $id) { return $o; } } //else return false; } /** * extract a particular option based on text * @var string $text; * @return mixed; */ function get_option_by_text($text = '') { /* * no san check - no easy way to tell if $text has been cleaned or not * if it hasn't been cleaned (clean_plain_data) and contains ', " etc function will not match it */ foreach($this->options as $o) { if($o->text == $text) { return $o; } } //else return false; } /** * add record to the database * @param array $d data to use if not POST * @return bool */ function create($d=array()) { global $message; if(!is_array($d) || empty($d)) { /** * get from POST */ $d = $_POST; } $this->name = clean_plain_data($d['list_name']); $this->description = clean_plain_data($d['list_description']); /** * validate required fields */ $m = ''; if($this->name==''){ $m .= 'Please enter a list name <br />'; } if($m != '') { $message .= $m; return false; } $fields = 'list_name , list_description '; $values = "'$this->name' , '$this->description' "; $query = "insert into lists ( $fields ) values ( $values )"; if(!mysql_query($query)) { $message .= mysql_error(); return false; } $this->list_id = mysql_insert_id(); $message .= 'List record created <br />'; return true; } /** * update record in the database * @param array $d data to use if not POST * @return bool */ function update($d=array()) { global $message; /** * san check */ if(!$this->list_id) { $message .= 'Invalid list <br />'; return false; } if(!is_array($d) || empty($d)) { /** * get from POST */ $d = $_POST; } $this->name = clean_plain_data($d['list_name']); $this->description = clean_plain_data($d['list_description']); /** * validate required fields */ $m = ''; if($this->name == ''){ $m .= 'Please enter a list name <br />'; } if($m != '') { $message .= $m; return false; } /** * @var string $query update query */ $query = "update lists set list_name = '$this->name' , list_description = '$this->description' where list_id = '$this->list_id'"; $update = mysql_query($query); if(!$update) { $message .= mysql_error(); return false; } $message .= 'List record updated <br />'; return true; } /** * add a new option * @var array $d values to give new option * @return bool */ function new_option($d=array()) { if(empty($d)) { $d = array( 'option_text' => clean_plain_data($_POST['option_text']) , 'position' => clean_plain_data($_POST['position']) ); } $o = new list_option; $o->load_from_data($d); $o->list_id = $this->list_id; return $o->create(); unset($o); } /** * switch list option from active to inactive or vice versa * @param int $option_id * @return bool */ function toggle_option($option_id) { global $message; $this->load_options(true); if($o = $this->get_option_by_id($option_id)) { $o->active = ($o->active) ? 0 : 1; $o->update = true; $o->update(); return $o->active; } //else $message .= 'Option not found'; return; } /** * update options from form * @var $d array containing update values; if empty use $_POST data * @return */ function update_all_options($d=array()) { global $message; $this->load_options(true); if(empty($d)) { $d = $_POST; } $edited = array(); /* * option text */ if(isset($d['option_texts']) && is_array($d['option_texts'])) { foreach($this->options as $o) { if(isset($d['option_texts'][$o->option_id])) { $text = clean_plain_data($d['option_texts'][$o->option_id]); if($text != '' && $text != $o->text) { $o->text = $text; $o->update = true; } } } } /** * option order */ if(isset($_POST['option_positions']) && is_array($_POST['option_positions'])) { $p = $_POST['option_positions']; asort($p); $i = 10; foreach($p as $id => $pos){ //actual entered value ($pos) is irrelevant $o = $this->get_option_by_id($id); if(is_object($o)) { if($i != $o->position) { $o->position = $i; $o->update = true; } $i+=10; } } } $count = 0; foreach($this->options as $o) { if($o->update(true)) { $count++; } } $message .= ($count == 1) ? '1 option was altered' : $count.' options were altered'; return $count; } /** * remove one option from list * @param int $option_id * @return bool */ function remove_option($option_id=0) { global $message; /** * san check */ if(!is_numeric_id($this->list_id,0) || !is_numeric_id($option_id,0)) { $message .= 'Unable to remove list option from database: invalid id <br />'; return false; } /* * options */ $this->load_options(true); $o = $this->get_option_by_id($option_id); return $o->delete(); } /* * remove all-the-things * @return bool */ function delete() { global $message; /** * san check */ if(!is_numeric_id($this->list_id,0)) { $message .= 'Unable to delete list: invalid id <br />'; return false; } /** * remove options */ if(!$this->remove_options()) { return false; } /** * remove (delete) from database */ return $this->remove_from_database(); } /** * remove all option records from database * @return bool */ function remove_options() { global $message; /** * san check */ if(!is_numeric_id($this->list_id,0)) { $message .= 'Unable to remove list options from database: invalid id <br />'; return false; } /* * options */ $this->load_options(true); foreach($this->options as $o) { $o->delete(); } return true; } /** * remove record from database * @return bool */ function remove_from_database() { global $message; /** * san check */ if(!is_numeric_id($this->list_id,0)) { $message .= 'Unable to remove list from database: invalid id <br />'; return false; } /** * delete list record * @var string $query delete query * @return bool */ $query = "delete from lists where list_id = '$this->list_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 .= $this->name.' database record deleted <br />'; return true; } /** * output as <option>s for a <select>; load if necessary * @param mixed $selected ids of selected items * @param bool $all flag to switch between all/active if loading; by default only load active * @return string */ function html_select_options($selected=array(), $all=true) { if(!is_array($selected)) { $selected = array($selected); } if(empty($this->options)) { $this->load_options($all); } $return = ''; foreach($this->options as $o) { $id = $o->option_id; $return .= '<option value="'.$id.'"'; $return .= (in_array($id,$selected)) ? ' selected="selected"' : ''; $return .= ">$o->text</option>\n"; } return $return; } /** * output as <option>s for a <select>; load if necessary * - this version puts the plain text in the values instead of the option_ids * @param mixed $selected text values of selected items * @param bool $all flag to switch between all/active if loading; by default only load active * @return string */ function html_select_text($selected=array(), $all=true) { if(!is_array($selected)) { $selected = array($selected); } if(empty($this->options)) { $this->load_options($all); } $return = ''; foreach($this->options as $o) { $return .= '<option value="'.$o->text.'"'; $return .= (in_array($o->text,$selected)) ? ' selected="selected"' : ''; $return .= ">$o->text</option>\n"; } return $return; } /** * output as radio <inputs>s; load if necessary * @param string $name of radio group * @param mixed $selected ids of selected items * @param bool $all flag to switch between all/active if loading; by default only load active * @return string */ function html_radio_buttons($name='radio', $selected=array(), $all=true) { if(!is_array($selected)) { $selected = array($selected); //of course being a radio set there should only be one selected value anyway } if(empty($this->options)) { $this->load_options($all); } $return = ''; foreach($this->options as $o) { $id = $o->option_id; $return .= '<label for="'.$name.$id.'"><input type="radio" name="'.$name.'" id="'.$name.$id.'" value="'.$id.'"'; $return .= (in_array($id,$selected)) ? ' checked="checked"' : ''; $return .= " /> $o->text</label>\n"; } return $return; } /** * output as checkbox <inputs>s; load if necessary; * note generated ids have different form to radio just in case both are included on one page for some reason * @param string $name of checkbox group * @param mixed $selected ids of selected items * @param bool $all flag to switch between all/active if loading; by default only load active * @return string */ function html_checkboxes($name='checkbox', $selected=array(), $all=true) { if(!is_array($selected)) { $selected = array($selected); } if(empty($this->options)) { $this->load_options($all); } $return = ''; foreach($this->options as $o) { $id = $o->option_id; $return .= '<label for="'.$name.'_'.$id.'"><input type="checkbox" name="'.$name.'['.$id.']" id="'.$name.'_'.$id.'" value="'.$id.'"'; $return .= (in_array($id,$selected)) ? ' checked="checked"' : ''; $return .= " /> $o->text</label>\n"; } return $return; } /** * output text only as <li><a>s for a <ul> or <ol>; load if necessary * @param string $link base link eg /path/to/module/?listitem= * @param string $class class name for selected items * @param mixed $selected ids of selected items * @param bool $all flag to switch between all/active if loading; by default only load active * @return string */ function html_link_list($link='',$class='',$selected=array(), $all=true) { if(!is_array($selected)) { $selected = array($selected); } if(empty($this->options)) { $this->load_options($all); } $return = ''; foreach($this->options as $o) { $id = $o->option_id; $return .= '<li '; $return .= (in_array($id,$selected)) ? ' class="'.$class.'"' : ''; $return .= '><a href="'.$link.$id.'">'.$o->text.'</a></li>'; $return .= "\n"; } return $return; } } ?>
cải xoăn