| 1 |
<?php |
| 2 |
|
| 3 |
if ( ! defined( 'ABSPATH' ) ) { |
| 4 |
exit; // Exit if accessed directly |
| 5 |
} |
| 6 |
|
| 7 |
/** |
| 8 |
* Class LP_Section_Items_DB |
| 9 |
*/ |
| 10 |
class LP_Section_Items_DB extends LP_Database { |
| 11 |
public static $_instance; |
| 12 |
|
| 13 |
public function __construct() { |
| 14 |
parent::__construct(); |
| 15 |
} |
| 16 |
|
| 17 |
public static function getInstance() { |
| 18 |
if ( is_null( self::$_instance ) ) { |
| 19 |
self::$_instance = new self(); |
| 20 |
} |
| 21 |
|
| 22 |
return self::$_instance; |
| 23 |
} |
| 24 |
|
| 25 |
/** |
| 26 |
* Get section items |
| 27 |
* |
| 28 |
* @return array|null|int|string |
| 29 |
* @throws Exception |
| 30 |
* @version 1.0.2 |
| 31 |
* @since 4.1.6 |
| 32 |
*/ |
| 33 |
public function get_section_items( LP_Section_Items_Filter $filter ) { |
| 34 |
$default_fields = $filter->all_fields; |
| 35 |
$filter->fields = array_merge( $default_fields, $filter->fields ); |
| 36 |
|
| 37 |
if ( empty( $filter->collection ) ) { |
| 38 |
$filter->collection = $this->tb_lp_section_items; |
| 39 |
} |
| 40 |
|
| 41 |
if ( empty( $filter->collection_alias ) ) { |
| 42 |
$filter->collection_alias = 'si'; |
| 43 |
} |
| 44 |
|
| 45 |
foreach ( $filter->fields as $k => $field ) { |
| 46 |
$filter->fields[ $k ] = $filter->collection_alias . '.' . $field; |
| 47 |
} |
| 48 |
|
| 49 |
if ( ! empty( $filter->section_id ) ) { |
| 50 |
$filter->where[] = $this->wpdb->prepare( 'AND si.section_id = %d', $filter->section_id ); |
| 51 |
} |
| 52 |
|
| 53 |
if ( ! empty( $filter->item_id ) ) { |
| 54 |
$filter->where[] = $this->wpdb->prepare( 'AND si.item_id = %d', $filter->item_id ); |
| 55 |
} |
| 56 |
|
| 57 |
if ( ! empty( $filter->item_ids ) ) { |
| 58 |
$filter->where[] = $this->wpdb->prepare( |
| 59 |
'AND si.item_id IN(' . LP_Helper::db_format_array( $filter->item_ids, '%d' ) . ')', |
| 60 |
$filter->item_ids |
| 61 |
); |
| 62 |
} |
| 63 |
|
| 64 |
if ( ! empty( $filter->section_item_id ) ) { |
| 65 |
$filter->where[] = $this->wpdb->prepare( 'AND si.section_item_id = %d', $filter->section_item_id ); |
| 66 |
} |
| 67 |
|
| 68 |
if ( ! empty( $filter->item_type ) ) { |
| 69 |
$filter->where[] = $this->wpdb->prepare( 'AND si.item_type = %s', $filter->item_type ); |
| 70 |
} |
| 71 |
|
| 72 |
return $this->execute( $filter ); |
| 73 |
} |
| 74 |
|
| 75 |
/** |
| 76 |
* Update table |
| 77 |
* |
| 78 |
* @throws Exception |
| 79 |
*/ |
| 80 |
public function update( LP_Section_Items_Filter $filter ) { |
| 81 |
$filter->collection = $this->tb_lp_section_items; |
| 82 |
$this->update_execute( $filter ); |
| 83 |
} |
| 84 |
|
| 85 |
/** |
| 86 |
* Delete items on section |
| 87 |
* |
| 88 |
* @param LP_Section_Items_Filter $filter |
| 89 |
* |
| 90 |
* @return bool|int|mysqli_result|resource|null |
| 91 |
* @throws Exception |
| 92 |
*/ |
| 93 |
public function delete_items( LP_Section_Items_Filter $filter ) { |
| 94 |
if ( empty( $filter->item_ids ) ) { |
| 95 |
return 1; |
| 96 |
} |
| 97 |
|
| 98 |
$where = 'WHERE 1=1 '; |
| 99 |
|
| 100 |
$where .= $this->wpdb->prepare( |
| 101 |
'AND item_id IN(' . LP_Helper::db_format_array( $filter->item_ids, '%d' ) . ')', |
| 102 |
$filter->item_ids |
| 103 |
); |
| 104 |
|
| 105 |
$result = $this->wpdb->query( |
| 106 |
"DELETE FROM $this->tb_lp_section_items |
| 107 |
$where |
| 108 |
" |
| 109 |
); |
| 110 |
|
| 111 |
$this->check_execute_has_error(); |
| 112 |
|
| 113 |
return $result; |
| 114 |
} |
| 115 |
|
| 116 |
/** |
| 117 |
* Get last item number order on section |
| 118 |
* |
| 119 |
* @throws Exception |
| 120 |
*/ |
| 121 |
public function get_last_number_order( int $section_id = 0 ): int { |
| 122 |
$query = $this->wpdb->prepare( |
| 123 |
"SELECT MAX(item_order) |
| 124 |
FROM $this->tb_lp_section_items |
| 125 |
WHERE section_id = %d", |
| 126 |
$section_id |
| 127 |
); |
| 128 |
|
| 129 |
$number_order = intval( $this->wpdb->get_var( $query ) ); |
| 130 |
|
| 131 |
$this->check_execute_has_error(); |
| 132 |
|
| 133 |
return $number_order; |
| 134 |
} |
| 135 |
|
| 136 |
/** |
| 137 |
* Delete item on section of course not in table posts. |
| 138 |
* |
| 139 |
* @param int $course_id |
| 140 |
* |
| 141 |
* @throws Exception |
| 142 |
* @since 4.2.6.4 |
| 143 |
* @version 1.0.0 |
| 144 |
*/ |
| 145 |
public function delete_item_not_in_tb_post( int $course_id ) { |
| 146 |
$filter_section = $this->wpdb->prepare( |
| 147 |
"DELETE si |
| 148 |
FROM $this->tb_lp_section_items AS si |
| 149 |
INNER JOIN $this->tb_lp_sections AS s ON si.section_id = s.section_id |
| 150 |
AND s.section_course_id = %d |
| 151 |
WHERE item_id NOT IN (SELECT ID FROM $this->tb_posts WHERE post_status = 'publish') |
| 152 |
", |
| 153 |
$course_id |
| 154 |
); |
| 155 |
|
| 156 |
$this->wpdb->query( $filter_section ); |
| 157 |
|
| 158 |
$this->check_execute_has_error(); |
| 159 |
} |
| 160 |
|
| 161 |
/** |
| 162 |
* Insert data |
| 163 |
* |
| 164 |
* @param array $data |
| 165 |
* |
| 166 |
* @return int |
| 167 |
* @throws Exception |
| 168 |
* @version 1.0.1 |
| 169 |
* @since 4.2.8.6 |
| 170 |
*/ |
| 171 |
public function insert_data( array $data ): int { |
| 172 |
$filter = new LP_Section_ITems_Filter(); |
| 173 |
|
| 174 |
foreach ( $data as $col_name => $value ) { |
| 175 |
if ( ! in_array( $col_name, $filter->all_fields ) ) { |
| 176 |
unset( $data[ $col_name ] ); |
| 177 |
} |
| 178 |
} |
| 179 |
|
| 180 |
// section_item_id is auto increment. |
| 181 |
unset( $data['section_item_id'] ); |
| 182 |
|
| 183 |
$this->wpdb->insert( $this->tb_lp_section_items, $data ); |
| 184 |
|
| 185 |
$this->check_execute_has_error(); |
| 186 |
|
| 187 |
return $this->wpdb->insert_id; |
| 188 |
} |
| 189 |
|
| 190 |
/** |
| 191 |
* Update data |
| 192 |
* |
| 193 |
* @param array $data |
| 194 |
* |
| 195 |
* @return bool |
| 196 |
* |
| 197 |
* @throws Exception |
| 198 |
* @since 4.2.8.6 |
| 199 |
* @version 1.0.0 |
| 200 |
*/ |
| 201 |
public function update_data( array $data ): bool { |
| 202 |
if ( empty( $data['section_item_id'] ) ) { |
| 203 |
throw new Exception( __( 'Invalid section_item_id!', 'learnpress' ) . ' | ' . __FUNCTION__ ); |
| 204 |
} |
| 205 |
|
| 206 |
$filter = new LP_Section_items_Filter(); |
| 207 |
$filter->collection = $this->tb_lp_section_items; |
| 208 |
foreach ( $data as $col_name => $value ) { |
| 209 |
if ( ! in_array( $col_name, $filter->all_fields ) ) { |
| 210 |
continue; |
| 211 |
} |
| 212 |
|
| 213 |
if ( is_null( $value ) ) { |
| 214 |
$filter->set[] = $col_name . ' = null'; |
| 215 |
} else { |
| 216 |
$filter->set[] = $this->wpdb->prepare( $col_name . ' = %s', $value ); |
| 217 |
} |
| 218 |
} |
| 219 |
|
| 220 |
$filter->where[] = $this->wpdb->prepare( 'AND section_item_id = %d', $data['section_item_id'] ); |
| 221 |
$this->update_execute( $filter ); |
| 222 |
|
| 223 |
return true; |
| 224 |
} |
| 225 |
|
| 226 |
/** |
| 227 |
* Update items position |
| 228 |
* Update item_order of each item in section |
| 229 |
* |
| 230 |
* @throws Exception |
| 231 |
* @since 4.2.8.6 |
| 232 |
* @version 1.0.0 |
| 233 |
* @deprecated 4.3.2 |
| 234 |
*/ |
| 235 |
public function update_items_position( array $item_ids, $section_id ) { |
| 236 |
_deprecated_function( __METHOD__, '4.3.2', 'CourseSectionItemsDB::update_items_position' ); |
| 237 |
$filter = new LP_Section_Items_Filter(); |
| 238 |
$filter->collection = $this->tb_lp_section_items; |
| 239 |
$SET_SQL = 'item_order = CASE'; |
| 240 |
|
| 241 |
foreach ( $item_ids as $position => $item_id ) { |
| 242 |
++$position; |
| 243 |
$item_id = absint( $item_id ); |
| 244 |
if ( empty( $item_id ) ) { |
| 245 |
continue; |
| 246 |
} |
| 247 |
|
| 248 |
$SET_SQL .= $this->wpdb->prepare( ' WHEN item_id = %d THEN %d', $item_id, $position ); |
| 249 |
} |
| 250 |
|
| 251 |
$SET_SQL .= ' ELSE item_order END'; |
| 252 |
$filter->set[] = $SET_SQL; |
| 253 |
$filter->where[] = $this->wpdb->prepare( 'AND section_id = %d', $section_id ); |
| 254 |
|
| 255 |
$this->update_execute( $filter ); |
| 256 |
} |
| 257 |
} |
| 258 |
|