/app_old/membershipincludes/classes/class.level.php

  1. <?php 
  2. if(!class_exists('M_Level')) { 
  3.  
  4. class M_Level { 
  5.  
  6. var $id = false; 
  7.  
  8. var $db; 
  9. var $tables = array('membership_levels', 'membership_rules', 'subscriptions_levels', 'membership_relationships', 'levelmeta'); 
  10.  
  11. var $membership_levels; 
  12. var $membership_rules; 
  13. var $subscriptions_levels; 
  14. var $membership_relationships; 
  15. var $levelmeta; 
  16.  
  17. // if the data needs reloaded, or hasn't been loaded yet 
  18. var $dirty = true; 
  19.  
  20. var $level; 
  21. var $ruledetails = array(); 
  22.  
  23. // Active rules 
  24. var $positiverules = array(); 
  25. var $negativerules = array(); 
  26.  
  27. var $lastlevelid; 
  28.  
  29. function __construct( $id = false , $fullload = false, $loadtype = array('public', 'core') ) { 
  30.  
  31. global $wpdb; 
  32.  
  33. $this->db =& $wpdb; 
  34.  
  35. foreach($this->tables as $table) { 
  36. $this->$table = membership_db_prefix($this->db, $table); 
  37.  
  38. $this->id = $id; 
  39.  
  40. if($fullload) { 
  41. $this->load_rules( $loadtype ); 
  42.  
  43.  
  44. function M_Level( $id = false, $fullload = false, $admin = false ) { 
  45. $this->__construct( $id, $fullload, $admin ); 
  46.  
  47. // Fields 
  48.  
  49. function level_title() { 
  50.  
  51. if(empty($this->level)) { 
  52. $level = $this->get(); 
  53.  
  54. if($level) { 
  55. return $level->level_title; 
  56. } else { 
  57. return false; 
  58. } else { 
  59. return $this->level->level_title; 
  60.  
  61.  
  62. function get_shortcode() { 
  63.  
  64. if(empty($this->level)) { 
  65. $level = $this->get(); 
  66.  
  67. if($level) { 
  68. return sanitize_title_with_dashes('level-' . $level->level_title); 
  69. } else { 
  70. return false; 
  71. } else { 
  72. return sanitize_title_with_dashes('level-' . $level->level->level_title); 
  73.  
  74.  
  75. // Gets 
  76.  
  77. function get() { 
  78.  
  79. if($this->dirty) { 
  80. $sql = $this->db->prepare( "SELECT * FROM {$this->membership_levels} WHERE id = %d", $this->id); 
  81.  
  82. $this->level = $this->db->get_row($sql); 
  83.  
  84. $this->dirty = false; 
  85.  
  86. return $this->level; 
  87.  
  88.  
  89. function get_rules($type) { 
  90.  
  91. $sql = $this->db->prepare( "SELECT * FROM {$this->membership_rules} WHERE level_id = %d AND rule_ive = %s ORDER BY rule_order ASC", $this->id, $type ); 
  92.  
  93. $this->ruledetails[$type] = $this->db->get_results( $sql ); 
  94.  
  95. return $this->ruledetails[$type]; 
  96.  
  97.  
  98. function delete($forced = false) { 
  99.  
  100. $sql = $this->db->prepare( "DELETE FROM {$this->membership_levels} WHERE id = %d", $this->id); 
  101.  
  102. $sql2 = $this->db->prepare( "DELETE FROM {$this->membership_rules} WHERE level_id = %d", $this->id); 
  103.  
  104. $sql3 = $this->db->prepare( "DELETE FROM {$this->subscriptions_levels} WHERE level_id = %d", $this->id); 
  105.  
  106. if($this->db->query($sql)) { 
  107.  
  108. $this->db->query($sql2); 
  109. $this->db->query($sql3); 
  110.  
  111. $this->dirty = true; 
  112.  
  113. return true; 
  114.  
  115. } else { 
  116. return false; 
  117.  
  118.  
  119. function update() { 
  120.  
  121. $this->dirty = true; 
  122.  
  123. if($this->id < 0 ) { 
  124. return $this->add(); 
  125. } else { 
  126. $return = $this->db->update($this->membership_levels, array('level_title' => $_POST['level_title'], 'level_slug' => sanitize_title($_POST['level_title'])), array('id' => $this->id)); 
  127.  
  128. // Remove the existing rules for this membership level 
  129. $this->db->query( $this->db->prepare( "DELETE FROM {$this->membership_rules} WHERE level_id = %d", $this->id ) ); 
  130.  
  131. // Process the new rules 
  132. if(!empty($_POST['in-positive-rules'])) { 
  133. $rules = explode(', ', $_POST['in-positive-rules']); 
  134. $count = 1; 
  135. foreach( (array) $rules as $rule ) { 
  136. if(!empty($rule)) { 
  137. // Check if the rule has any information for it. 
  138. if(isset($_POST[$rule])) { 
  139. $ruleval = maybe_serialize($_POST[$rule]); 
  140. // write it to the database 
  141. $this->db->insert($this->membership_rules, array("level_id" => $this->id, "rule_ive" => 'positive', "rule_area" => $rule, "rule_value" => $ruleval, "rule_order" => $count++)); 
  142. // Hit an action - two methods of hooking 
  143. do_action('membership_update_positive_rule', $rule, $_POST, $this->id); 
  144. do_action('membership_update_positive_rule_' . $rule, $_POST, $this->id); 
  145.  
  146.  
  147. if(!empty($_POST['in-negative-rules'])) { 
  148. $rules = explode(', ', $_POST['in-negative-rules']); 
  149. $count = 1; 
  150. foreach( (array) $rules as $rule ) { 
  151. if(!empty($rule)) { 
  152. // Check if the rule has any information for it. 
  153. if(isset($_POST[$rule])) { 
  154. $ruleval = maybe_serialize($_POST[$rule]); 
  155. // write it to the database 
  156. $this->db->insert($this->membership_rules, array("level_id" => $this->id, "rule_ive" => 'negative', "rule_area" => $rule, "rule_value" => $ruleval, "rule_order" => $count++)); 
  157. // Hit an action - two methods of hooking 
  158. do_action('membership_update_negative_rule', $rule, $_POST, $this->id); 
  159. do_action('membership_update_negative_rule_' . $rule, $_POST, $this->id); 
  160.  
  161. do_action('membership_level_update', $this->id); 
  162.  
  163.  
  164. return true; // for now 
  165.  
  166.  
  167. function add() { 
  168.  
  169. $this->dirty = true; 
  170.  
  171. if($this->id > 0 ) { 
  172. return $this->update(); 
  173. } else { 
  174. $return = $this->db->insert($this->membership_levels, array('level_title' => $_POST['level_title'], 'level_slug' => sanitize_title($_POST['level_title']))); 
  175.  
  176. $this->id = $this->db->insert_id; 
  177.  
  178. // Process the new rules 
  179. if(!empty($_POST['in-positive-rules'])) { 
  180. $rules = explode(', ', $_POST['in-positive-rules']); 
  181. $count = 1; 
  182. foreach( (array) $rules as $rule ) { 
  183. if(!empty($rule)) { 
  184. // Check if the rule has any information for it. 
  185. if(isset($_POST[$rule])) { 
  186. $ruleval = maybe_serialize($_POST[$rule]); 
  187. // write it to the database 
  188. $this->db->insert($this->membership_rules, array("level_id" => $this->id, "rule_ive" => 'positive', "rule_area" => $rule, "rule_value" => $ruleval, "rule_order" => $count++)); 
  189. // Hit an action - two methods of hooking 
  190. do_action('membership_add_positive_rule', $rule, $_POST, $this->id); 
  191. do_action('membership_add_positive_rule_' . $rule, $_POST, $this->id); 
  192.  
  193.  
  194. if(!empty($_POST['in-negative-rules'])) { 
  195. $rules = explode(', ', $_POST['in-negative-rules']); 
  196. $count = 1; 
  197. foreach( (array) $rules as $rule ) { 
  198. if(!empty($rule)) { 
  199. // Check if the rule has any information for it. 
  200. if(isset($_POST[$rule])) { 
  201. $ruleval = maybe_serialize($_POST[$rule]); 
  202. // write it to the database 
  203. $this->db->insert($this->membership_rules, array("level_id" => $this->id, "rule_ive" => 'negative', "rule_area" => $rule, "rule_value" => $ruleval, "rule_order" => $count++)); 
  204. // Hit an action - two methods of hooking 
  205. do_action('membership_add_negative_rule', $rule, $_POST, $this->id); 
  206. do_action('membership_add_negative_rule_' . $rule, $_POST, $this->id); 
  207.  
  208.  
  209. do_action('membership_level_add', $this->id); 
  210.  
  211. return true; // for now 
  212.  
  213.  
  214. function toggleactivation($forced = false) { 
  215.  
  216. $this->dirty = true; 
  217.  
  218. $sql = $this->db->prepare( "UPDATE {$this->membership_levels} SET level_active = NOT level_active WHERE id = %d", $this->id); 
  219.  
  220. return $this->db->query($sql); 
  221.  
  222. // UI functions 
  223.  
  224.  
  225. function load_rules( $loadtype = array('public', 'core') ) { 
  226.  
  227. global $M_Rules; 
  228.  
  229. //positiverules 
  230. $positive = $this->get_rules('positive'); 
  231.  
  232. //negativerules 
  233. $negative = $this->get_rules('negative'); 
  234.  
  235. //print_r($positive); 
  236. //print_r($negative); 
  237.  
  238. if(!empty($positive)) { 
  239. $key = 0; 
  240. foreach( (array) $positive as $key => $rule) { 
  241. if(isset($M_Rules[$rule->rule_area]) && class_exists($M_Rules[$rule->rule_area])) { 
  242. $this->positiverules[$key] = new $M_Rules[$rule->rule_area]( $this->id ); 
  243.  
  244. if( in_array($this->positiverules[$key]->rulearea, $loadtype) ) { 
  245. $this->positiverules[$key]->on_positive(maybe_unserialize($rule->rule_value)); 
  246. $key++; 
  247. } else { 
  248. unset($this->positiverules[$key]); 
  249.  
  250.  
  251. if(!empty($negative)) { 
  252. $key = 0; 
  253. foreach( (array) $negative as $key => $rule) { 
  254. if(isset($M_Rules[$rule->rule_area]) && class_exists($M_Rules[$rule->rule_area])) { 
  255. $this->negativerules[$key] = new $M_Rules[$rule->rule_area]( $this->id ); 
  256.  
  257. if( in_array($this->negativerules[$key]->rulearea, $loadtype) ) { 
  258. $this->negativerules[$key]->on_negative(maybe_unserialize($rule->rule_value)); 
  259. $key++; 
  260. } else { 
  261. unset($this->negativerules[$key]); 
  262.  
  263.  
  264.  
  265. function has_positive_rule($rulename) { 
  266.  
  267. if(!empty($this->positiverules)) { 
  268. foreach($this->positiverules as $key => $rule) { 
  269. if($rule->name == $rulename) { 
  270. return true; 
  271.  
  272. return false; 
  273.  
  274.  
  275. function has_negative_rule($rulename) { 
  276.  
  277. if(!empty($this->negativerules)) { 
  278. foreach($this->negativerules as $key => $rule) { 
  279. if($rule->name == $rulename) { 
  280. return true; 
  281.  
  282. return false; 
  283.  
  284.  
  285. function has_rule($rulename) { 
  286.  
  287. if($this->has_negative_rule($rulename) || $this->has_positive_rule($rulename)) { 
  288. return true; 
  289. } else { 
  290. return false; 
  291.  
  292.  
  293. // pass thrus 
  294.  
  295. function positive_pass_thru($rulename, $function, $arg) { 
  296.  
  297. if(!empty($this->positiverules)) { 
  298. foreach($this->positiverules as $key => $rule) { 
  299. if($rule->name == $rulename) { 
  300. return $rule->$function('positive', $arg); 
  301.  
  302. return false; 
  303.  
  304.  
  305. function negative_pass_thru($rulename, $function, $arg) { 
  306.  
  307. if(!empty($this->negativerules)) { 
  308. foreach($this->negativerules as $key => $rule) { 
  309. if($rule->name == $rulename) { 
  310. return $rule->$function('negative', $arg); 
  311.  
  312. return false; 
  313.  
  314.  
  315. // Counting 
  316. function count( ) { 
  317.  
  318. $sql = $this->db->prepare( "SELECT count(*) as levelcount FROM {$this->membership_relationships} WHERE level_id = %d", $this->id ); 
  319.  
  320. return $this->db->get_var( $sql ); 
  321.  
  322.  
  323. // Meta information 
  324. function get_meta($key, $default = false) { 
  325.  
  326. $sql = $this->db->prepare( "SELECT meta_value FROM {$this->levelmeta} WHERE meta_key = %s AND level_id = %d", $key, $this->id); 
  327.  
  328. $row = $this->db->get_var( $sql ); 
  329.  
  330. if(empty($row)) { 
  331. return $default; 
  332. } else { 
  333. return $row; 
  334.  
  335.  
  336. function add_meta($key, $value) { 
  337.  
  338. return $this->insertorupdate( $this->levelmeta, array( 'level_id' => $this->id, 'meta_key' => $key, 'meta_value' => $value) ); 
  339.  
  340.  
  341. function update_meta($key, $value) { 
  342.  
  343. return $this->insertorupdate( $this->levelmeta, array( 'level_id' => $this->id, 'meta_key' => $key, 'meta_value' => $value) ); 
  344.  
  345.  
  346. function delete_meta($key) { 
  347.  
  348. $sql = $this->db->prepare( "DELETE FROM {$this->levelmeta} WHERE meta_key = %s AND level_id = %d", $key, $this->id); 
  349.  
  350. return $this->db->query( $sql ); 
  351.  
  352.  
  353. function insertorupdate( $table, $query ) { 
  354.  
  355. $fields = array_keys($query); 
  356. $formatted_fields = array(); 
  357. foreach ( $fields as $field ) { 
  358. $form = '%s'; 
  359. $formatted_fields[] = $form; 
  360. $sql = "INSERT INTO `$table` (`" . implode( '`, `', $fields ) . "`) VALUES ('" . implode( "', '", $formatted_fields ) . "')"; 
  361. $sql .= " ON DUPLICATE KEY UPDATE "; 
  362.  
  363. $dup = array(); 
  364. foreach($fields as $field) { 
  365. $dup[] = "`" . $field . "` = VALUES(`" . $field . "`)"; 
  366.  
  367. $sql .= implode(', ', $dup); 
  368.  
  369. return $this->db->query( $this->db->prepare( $sql, $query ) ); 
  370.  
  371.  
  372.  
  373. ?> 
.