| @@ -1,131 +1,131 @@ | ||
| 1 | -<?php | |
| 2 | - | |
| 3 | -namespace LearnPress\Databases\Course; | |
| 4 | - | |
| 5 | -use Exception; | |
| 6 | -use LearnPress\Databases\DataBase; | |
| 7 | -use LearnPress\Filters\Course\CourseSectionItemsFilter; | |
| 8 | -use LP_Helper; | |
| 9 | - | |
| 10 | -if ( ! defined( 'ABSPATH' ) ) { | |
| 11 | - exit; // Exit if accessed directly | |
| 12 | -} | |
| 13 | - | |
| 14 | -/** | |
| 15 | - * Class CourseSectionItemsDB | |
| 16 | - * | |
| 17 | - * Convert from LP_Section_Items_DB | |
| 18 | - * | |
| 19 | - * @package LearnPress/Databases/Course | |
| 20 | - * @since 4.3.2 | |
| 21 | - * @version 1.0.0 | |
| 22 | - */ | |
| 23 | -class CourseSectionItemsDB extends DataBase { | |
| 24 | - public static $_instance; | |
| 25 | - | |
| 26 | - public function __construct() { | |
| 27 | - parent::__construct(); | |
| 28 | - } | |
| 29 | - | |
| 30 | - public static function getInstance() { | |
| 31 | - if ( is_null( self::$_instance ) ) { | |
| 32 | - self::$_instance = new self(); | |
| 33 | - } | |
| 34 | - | |
| 35 | - return self::$_instance; | |
| 36 | - } | |
| 37 | - | |
| 38 | - /** | |
| 39 | - * Get section items | |
| 40 | - * | |
| 41 | - * @param CourseSectionItemsFilter $filter | |
| 42 | - * | |
| 43 | - * @return array|null|int|string | |
| 44 | - * @throws Exception | |
| 45 | - * @version 1.0.2 | |
| 46 | - * @since 4.1.6 | |
| 47 | - */ | |
| 48 | - public function get_section_items( $filter ) { | |
| 49 | - $default_fields = $filter->all_fields; | |
| 50 | - $filter->fields = array_merge( $default_fields, $filter->fields ); | |
| 51 | - | |
| 52 | - if ( empty( $filter->collection ) ) { | |
| 53 | - $filter->collection = $this->tb_lp_section_items; | |
| 54 | - } | |
| 55 | - | |
| 56 | - if ( empty( $filter->collection_alias ) ) { | |
| 57 | - $filter->collection_alias = 'si'; | |
| 58 | - } | |
| 59 | - | |
| 60 | - foreach ( $filter->fields as $k => $field ) { | |
| 61 | - $filter->fields[ $k ] = $filter->collection_alias . '.' . $field; | |
| 62 | - } | |
| 63 | - | |
| 64 | - if ( ! empty( $filter->section_id ) ) { | |
| 65 | - $filter->where[] = $this->wpdb->prepare( 'AND si.section_id = %d', $filter->section_id ); | |
| 66 | - } | |
| 67 | - | |
| 68 | - if ( ! empty( $filter->item_id ) ) { | |
| 69 | - $filter->where[] = $this->wpdb->prepare( 'AND si.item_id = %d', $filter->item_id ); | |
| 70 | - } | |
| 71 | - | |
| 72 | - if ( ! empty( $filter->item_ids ) ) { | |
| 73 | - $filter->where[] = $this->wpdb->prepare( | |
| 74 | - 'AND si.item_id IN(' . LP_Helper::db_format_array( $filter->item_ids, '%d' ) . ')', | |
| 75 | - $filter->item_ids | |
| 76 | - ); | |
| 77 | - } | |
| 78 | - | |
| 79 | - if ( ! empty( $filter->section_item_id ) ) { | |
| 80 | - $filter->where[] = $this->wpdb->prepare( 'AND si.section_item_id = %d', $filter->section_item_id ); | |
| 81 | - } | |
| 82 | - | |
| 83 | - if ( ! empty( $filter->item_type ) ) { | |
| 84 | - $filter->where[] = $this->wpdb->prepare( 'AND si.item_type = %s', $filter->item_type ); | |
| 85 | - } | |
| 86 | - | |
| 87 | - return $this->execute( $filter ); | |
| 88 | - } | |
| 89 | - | |
| 90 | - /** | |
| 91 | - * Update items position | |
| 92 | - * Update item_order of each item in section | |
| 93 | - * | |
| 94 | - * convert from LP_Section_Items_DB::update_items_position | |
| 95 | - * | |
| 96 | - * @throws Exception | |
| 97 | - * @since 4.3.2 | |
| 98 | - * @version 1.0.0 | |
| 99 | - */ | |
| 100 | - public function update_items_position( array $item_ids, $section_id ) { | |
| 101 | - $filter = new CourseSectionItemsFilter(); | |
| 102 | - $filter->collection = $this->tb_lp_section_items; | |
| 103 | - $SET_SQL = 'item_order = CASE'; | |
| 104 | - | |
| 105 | - foreach ( $item_ids as $position => $item_id ) { | |
| 106 | - ++$position; | |
| 107 | - $item_id = absint( $item_id ); | |
| 108 | - if ( empty( $item_id ) ) { | |
| 109 | - continue; | |
| 110 | - } | |
| 111 | - | |
| 112 | - $SET_SQL .= $this->wpdb->prepare( ' WHEN item_id = %d THEN %d', $item_id, $position ); | |
| 113 | - } | |
| 114 | - | |
| 115 | - $SET_SQL .= ' ELSE item_order END'; | |
| 116 | - $filter->set[] = $SET_SQL; | |
| 117 | - $filter->where[] = $this->wpdb->prepare( 'AND section_id = %d', $section_id ); | |
| 118 | - | |
| 119 | - $this->update_execute( $filter ); | |
| 120 | - } | |
| 121 | - | |
| 122 | - /** | |
| 123 | - * Update table | |
| 124 | - * | |
| 125 | - * @throws Exception | |
| 126 | - */ | |
| 127 | - public function update( CourseSectionItemsFilter $filter ) { | |
| 128 | - $filter->collection = $this->tb_lp_section_items; | |
| 129 | - $this->update_execute( $filter ); | |
| 130 | - } | |
| 131 | -} | |
| 1 | +<?php | |
| 2 | + | |
| 3 | +namespace LearnPress\Databases\Course; | |
| 4 | + | |
| 5 | +use Exception; | |
| 6 | +use LearnPress\Databases\DataBase; | |
| 7 | +use LearnPress\Filters\Course\CourseSectionItemsFilter; | |
| 8 | +use LP_Helper; | |
| 9 | + | |
| 10 | +if ( ! defined( 'ABSPATH' ) ) { | |
| 11 | + exit; // Exit if accessed directly | |
| 12 | +} | |
| 13 | + | |
| 14 | +/** | |
| 15 | + * Class CourseSectionItemsDB | |
| 16 | + * | |
| 17 | + * Convert from LP_Section_Items_DB | |
| 18 | + * | |
| 19 | + * @package LearnPress/Databases/Course | |
| 20 | + * @since 4.3.2 | |
| 21 | + * @version 1.0.0 | |
| 22 | + */ | |
| 23 | +class CourseSectionItemsDB extends DataBase { | |
| 24 | + public static $_instance; | |
| 25 | + | |
| 26 | + public function __construct() { | |
| 27 | + parent::__construct(); | |
| 28 | + } | |
| 29 | + | |
| 30 | + public static function getInstance() { | |
| 31 | + if ( is_null( self::$_instance ) ) { | |
| 32 | + self::$_instance = new self(); | |
| 33 | + } | |
| 34 | + | |
| 35 | + return self::$_instance; | |
| 36 | + } | |
| 37 | + | |
| 38 | + /** | |
| 39 | + * Get section items | |
| 40 | + * | |
| 41 | + * @param CourseSectionItemsFilter $filter | |
| 42 | + * | |
| 43 | + * @return array|null|int|string | |
| 44 | + * @throws Exception | |
| 45 | + * @version 1.0.2 | |
| 46 | + * @since 4.1.6 | |
| 47 | + */ | |
| 48 | + public function get_section_items( $filter ) { | |
| 49 | + $default_fields = $filter->all_fields; | |
| 50 | + $filter->fields = array_merge( $default_fields, $filter->fields ); | |
| 51 | + | |
| 52 | + if ( empty( $filter->collection ) ) { | |
| 53 | + $filter->collection = $this->tb_lp_section_items; | |
| 54 | + } | |
| 55 | + | |
| 56 | + if ( empty( $filter->collection_alias ) ) { | |
| 57 | + $filter->collection_alias = 'si'; | |
| 58 | + } | |
| 59 | + | |
| 60 | + foreach ( $filter->fields as $k => $field ) { | |
| 61 | + $filter->fields[ $k ] = $filter->collection_alias . '.' . $field; | |
| 62 | + } | |
| 63 | + | |
| 64 | + if ( ! empty( $filter->section_id ) ) { | |
| 65 | + $filter->where[] = $this->wpdb->prepare( 'AND si.section_id = %d', $filter->section_id ); | |
| 66 | + } | |
| 67 | + | |
| 68 | + if ( ! empty( $filter->item_id ) ) { | |
| 69 | + $filter->where[] = $this->wpdb->prepare( 'AND si.item_id = %d', $filter->item_id ); | |
| 70 | + } | |
| 71 | + | |
| 72 | + if ( ! empty( $filter->item_ids ) ) { | |
| 73 | + $filter->where[] = $this->wpdb->prepare( | |
| 74 | + 'AND si.item_id IN(' . LP_Helper::db_format_array( $filter->item_ids, '%d' ) . ')', | |
| 75 | + $filter->item_ids | |
| 76 | + ); | |
| 77 | + } | |
| 78 | + | |
| 79 | + if ( ! empty( $filter->section_item_id ) ) { | |
| 80 | + $filter->where[] = $this->wpdb->prepare( 'AND si.section_item_id = %d', $filter->section_item_id ); | |
| 81 | + } | |
| 82 | + | |
| 83 | + if ( ! empty( $filter->item_type ) ) { | |
| 84 | + $filter->where[] = $this->wpdb->prepare( 'AND si.item_type = %s', $filter->item_type ); | |
| 85 | + } | |
| 86 | + | |
| 87 | + return $this->execute( $filter ); | |
| 88 | + } | |
| 89 | + | |
| 90 | + /** | |
| 91 | + * Update items position | |
| 92 | + * Update item_order of each item in section | |
| 93 | + * | |
| 94 | + * convert from LP_Section_Items_DB::update_items_position | |
| 95 | + * | |
| 96 | + * @throws Exception | |
| 97 | + * @since 4.3.2 | |
| 98 | + * @version 1.0.0 | |
| 99 | + */ | |
| 100 | + public function update_items_position( array $item_ids, $section_id ) { | |
| 101 | + $filter = new CourseSectionItemsFilter(); | |
| 102 | + $filter->collection = $this->tb_lp_section_items; | |
| 103 | + $SET_SQL = 'item_order = CASE'; | |
| 104 | + | |
| 105 | + foreach ( $item_ids as $position => $item_id ) { | |
| 106 | + ++$position; | |
| 107 | + $item_id = absint( $item_id ); | |
| 108 | + if ( empty( $item_id ) ) { | |
| 109 | + continue; | |
| 110 | + } | |
| 111 | + | |
| 112 | + $SET_SQL .= $this->wpdb->prepare( ' WHEN item_id = %d THEN %d', $item_id, $position ); | |
| 113 | + } | |
| 114 | + | |
| 115 | + $SET_SQL .= ' ELSE item_order END'; | |
| 116 | + $filter->set[] = $SET_SQL; | |
| 117 | + $filter->where[] = $this->wpdb->prepare( 'AND section_id = %d', $section_id ); | |
| 118 | + | |
| 119 | + $this->update_execute( $filter ); | |
| 120 | + } | |
| 121 | + | |
| 122 | + /** | |
| 123 | + * Update table | |
| 124 | + * | |
| 125 | + * @throws Exception | |
| 126 | + */ | |
| 127 | + public function update( CourseSectionItemsFilter $filter ) { | |
| 128 | + $filter->collection = $this->tb_lp_section_items; | |
| 129 | + $this->update_execute( $filter ); | |
| 130 | + } | |
| 131 | +} | |