Нет описания

model.php 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. <?php
  2. // Exit if accessed directly
  3. if ( ! defined( 'ABSPATH' ) ) {
  4. exit;
  5. }
  6. class EModal_Model {
  7. protected $_class_name = 'EModal_Model';
  8. protected $_table_name = '';
  9. protected $_pk = 'id';
  10. protected $_data = array();
  11. protected $_default_fields = array();
  12. protected $_state = null;
  13. public function __construct( $id = null, $limit = 1 ) {
  14. global $wpdb;
  15. $table_name = $wpdb->prefix . $this->_table_name;
  16. $class_name = strtolower( $this->_class_name );
  17. $this->_data = apply_filters( "{$class_name}_fields", $this->_default_fields );
  18. if ( $id && is_numeric( $id ) ) {
  19. $row = $wpdb->get_row( "SELECT * FROM $table_name WHERE $this->_pk = $id LIMIT 1", ARRAY_A );
  20. if ( $row[ $this->_pk ] ) {
  21. $this->process_load( $row );
  22. }
  23. } else {
  24. $this->set_fields( apply_filters( "{$class_name}_defaults", array() ) );
  25. }
  26. return $this;
  27. }
  28. public function load( $query = null ) {
  29. global $wpdb;
  30. $table_name = $wpdb->prefix . $this->_table_name;
  31. if ( ! $query ) {
  32. $query = "SELECT * FROM $table_name";
  33. }
  34. $rows = $wpdb->get_results( $query, ARRAY_A );
  35. if ( ! empty( $rows ) ) {
  36. $results = array();
  37. foreach ( $rows as $row ) {
  38. $model = new $this->_class_name;
  39. $model->process_load( $row );
  40. $results[] = $model;
  41. }
  42. return $results;
  43. }
  44. return array();
  45. }
  46. public function save() {
  47. global $wpdb;
  48. $table_name = $wpdb->prefix . $this->_table_name;
  49. if ( $this->id ) {
  50. if ( ! $wpdb->update( $table_name, $this->serialized_values(), array( $this->_pk => $this->{$this->_pk} ) ) ) {
  51. $wpdb->insert( $table_name, $this->serialized_values() );
  52. $this->id = $wpdb->insert_id;
  53. }
  54. } else {
  55. $wpdb->insert( $table_name, $this->serialized_values() );
  56. $this->id = $wpdb->insert_id;
  57. }
  58. }
  59. public function delete() {
  60. global $wpdb;
  61. $table_name = $wpdb->prefix . $this->_table_name;
  62. return $wpdb->delete( $table_name, array( $this->_pk => $this->{$this->_pk} ) );
  63. }
  64. public function as_array() {
  65. $values = $this->_data;
  66. foreach ( $values as $key => $value ) {
  67. $values[ $key ] = $this->$key;
  68. }
  69. return $values;
  70. }
  71. public function process_load( $data ) {
  72. foreach ( $data as $key => $val ) {
  73. if ( array_key_exists( $key, $this->_data ) ) {
  74. $this->$key = maybe_unserialize( $val );
  75. }
  76. }
  77. }
  78. public function serialized_values() {
  79. $values = $this->_data;
  80. foreach ( $values as $key => $value ) {
  81. if ( $key != 'id' ) {
  82. $values[ $key ] = maybe_serialize( $this->$key );
  83. }
  84. }
  85. return $values;
  86. }
  87. public function __get( $key ) {
  88. if ( array_key_exists( $key, $this->_data ) ) {
  89. return $this->_data[ $key ];
  90. } elseif ( $key == 'id' ) {
  91. if ( array_key_exists( $this->_pk, $this->_data ) ) {
  92. return $this->_data[ $this->_pk ];
  93. }
  94. }
  95. }
  96. public function __set( $key, $value ) {
  97. if ( array_key_exists( $key, $this->_data ) ) {
  98. $this->_data[ $key ] = $value;
  99. return;
  100. }
  101. }
  102. public function __isset( $name ) {
  103. return isset( $this->_data[ $name ] );
  104. }
  105. public function fields() {
  106. return array_keys( $this->_data );
  107. }
  108. public function set_fields( array $data ) {
  109. foreach ( $data as $key => $val ) {
  110. if ( array_key_exists( $key, $this->_data ) ) {
  111. if ( is_array( $this->$key ) && is_array( $val ) ) {
  112. $this->$key = array_replace_recursive( $this->$key, $val );
  113. } else {
  114. $this->$key = $val;
  115. }
  116. }
  117. }
  118. }
  119. // Array Access Interface
  120. public function offsetExists( $key ) {
  121. return array_key_exists( $key, $this->as_array() );
  122. }
  123. public function offsetSet( $key, $value ) {
  124. $this->__set( $key, $value );
  125. }
  126. public function offsetGet( $key ) {
  127. return $this->$key;
  128. }
  129. public function offsetUnset( $key ) {
  130. $this->_data[ $key ] = null;
  131. }
  132. }