M_Level

The Membership 2 M Level class.

Defined (1)

The class is defined in the following location(s).

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