index.php
11 years ago
model-contacts.php
5 years ago
model-forms.php
4 years ago
model-lang.php
5 years ago
model-users.php
4 years ago
model-lang.php
192 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Model class <i>SIB_Forms_Lang</i> represents forms language |
| 4 | * |
| 5 | * @package SIB_Model |
| 6 | */ |
| 7 | |
| 8 | if ( ! class_exists( 'SIB_Forms_Lang' ) ) { |
| 9 | /** |
| 10 | * Class SIB_Forms_Lang |
| 11 | */ |
| 12 | class SIB_Forms_Lang { |
| 13 | |
| 14 | /** |
| 15 | * Tab table name |
| 16 | */ |
| 17 | const TABLE_NAME = 'sib_model_lang'; |
| 18 | |
| 19 | /** Create Table */ |
| 20 | public static function createTable() { |
| 21 | global $wpdb; |
| 22 | // create list table. |
| 23 | $creation_query = |
| 24 | 'CREATE TABLE IF NOT EXISTS ' . $wpdb->prefix . self::TABLE_NAME . ' ( |
| 25 | `id` int(20) NOT NULL AUTO_INCREMENT, |
| 26 | `frmID` int(20) NOT NULL DEFAULT -1, |
| 27 | `pID` int(20) NOT NULL DEFAULT -1, |
| 28 | `lang` varchar(120), |
| 29 | PRIMARY KEY (`id`) |
| 30 | );'; |
| 31 | require_once( ABSPATH . 'wp-admin/includes/upgrade.php' ); |
| 32 | $wpdb->query($creation_query); |
| 33 | } |
| 34 | |
| 35 | /** |
| 36 | * Remove table |
| 37 | */ |
| 38 | public static function removeTable() { |
| 39 | global $wpdb; |
| 40 | $query = 'DROP TABLE IF EXISTS ' . $wpdb->prefix . self::TABLE_NAME . ';'; |
| 41 | $wpdb->query( $query ); // db call ok; no-cache ok. |
| 42 | } |
| 43 | |
| 44 | /** |
| 45 | * Get form ID by pid and language. |
| 46 | * |
| 47 | * @param int $pID - parent form ID. |
| 48 | * @param string $lang - language. |
| 49 | * @return null |
| 50 | */ |
| 51 | public static function get_form_ID( $pID, $lang ) { |
| 52 | global $wpdb; |
| 53 | $query = "SHOW TABLES LIKE '" . $wpdb->prefix . self::TABLE_NAME . "'; "; |
| 54 | if ( $wpdb->get_var( $query ) == $wpdb->prefix . self::TABLE_NAME ) { |
| 55 | $query = $wpdb->prepare( |
| 56 | 'SELECT * FROM ' . $wpdb->prefix . self::TABLE_NAME . ' WHERE pID = %d AND lang= %s', |
| 57 | array( |
| 58 | esc_sql($pID), |
| 59 | esc_sql($lang) |
| 60 | ) |
| 61 | ); |
| 62 | $results = $wpdb->get_row( $query ); // db call ok; no-cache ok. |
| 63 | if ( ! empty( $results ) ) { |
| 64 | return $results->frmID; |
| 65 | } else { |
| 66 | return null; |
| 67 | } |
| 68 | } else { |
| 69 | return null; |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * Get form language by form id and parent id. |
| 75 | * |
| 76 | * @param int $frmID - form ID. |
| 77 | * @param int $pID - parent form ID. |
| 78 | * @return null |
| 79 | */ |
| 80 | public static function get_lang( $frmID, $pID ) { |
| 81 | global $wpdb; |
| 82 | $query = "SHOW TABLES LIKE '" . $wpdb->prefix . self::TABLE_NAME . "'; "; |
| 83 | if ( $wpdb->get_var( $query ) == $wpdb->prefix . self::TABLE_NAME ) { |
| 84 | $sql = $wpdb->prepare( |
| 85 | 'SELECT * FROM ' . $wpdb->prefix . self::TABLE_NAME . ' WHERE frmID = %d AND pID= %d', |
| 86 | array( |
| 87 | esc_sql($frmID), |
| 88 | esc_sql($pID) |
| 89 | ) |
| 90 | ); |
| 91 | $results = $wpdb->get_row( $sql ); // db call ok; no-cache ok. |
| 92 | if ( ! empty( $results ) ) { |
| 93 | return $results->lang; |
| 94 | } else { |
| 95 | return null; |
| 96 | } |
| 97 | } else { |
| 98 | return null; |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | /** |
| 103 | * Add form |
| 104 | * |
| 105 | * @param int $frmID - form ID. |
| 106 | * @param int $pid - parent form ID. |
| 107 | * @param string $lang - language. |
| 108 | * @return null|string |
| 109 | */ |
| 110 | public static function add_form_ID( $frmID, $pid, $lang ) { |
| 111 | // insert. |
| 112 | global $wpdb; |
| 113 | $query = $wpdb->prepare( |
| 114 | 'INSERT INTO ' . $wpdb->prefix . self::TABLE_NAME . ' (frmID,pID,lang) VALUES (%d, %d, %s)', |
| 115 | array( |
| 116 | esc_sql($frmID), |
| 117 | esc_sql($pid), |
| 118 | esc_sql($lang) |
| 119 | ) |
| 120 | ); |
| 121 | $wpdb->query( $query ); // db call ok; no-cache ok. |
| 122 | $index = $wpdb->get_var( 'SELECT LAST_INSERT_ID();' ); // db call ok; no-cache ok. |
| 123 | return $index; |
| 124 | } |
| 125 | |
| 126 | /** |
| 127 | * Check if origin form or translated form |
| 128 | * |
| 129 | * @param int $frmID - form ID. |
| 130 | * @return bool |
| 131 | */ |
| 132 | public static function check_form_trans( $frmID ) { |
| 133 | global $wpdb; |
| 134 | $query = "SHOW TABLES LIKE '" . $wpdb->prefix . self::TABLE_NAME . "'; "; |
| 135 | if ( $wpdb->get_var( $query ) == $wpdb->prefix . self::TABLE_NAME ) { |
| 136 | $sql = $wpdb->prepare( |
| 137 | 'SELECT * FROM ' . $wpdb->prefix . self::TABLE_NAME . ' WHERE frmID = %d', |
| 138 | array(esc_sql($frmID)) |
| 139 | ); |
| 140 | $results = $wpdb->get_row( $sql ); // db call ok; no-cache ok. |
| 141 | if ( ! empty( $results ) ) { |
| 142 | return true; |
| 143 | } else { |
| 144 | return false; |
| 145 | } |
| 146 | } else { |
| 147 | return false; |
| 148 | } |
| 149 | |
| 150 | } |
| 151 | |
| 152 | /** |
| 153 | * Remove forms |
| 154 | * |
| 155 | * @param int $pID - parent form ID. |
| 156 | */ |
| 157 | public static function remove_trans( $pID ) { |
| 158 | global $wpdb; |
| 159 | $query = "SHOW TABLES LIKE '" . $wpdb->prefix . self::TABLE_NAME . "'; "; |
| 160 | if ( $wpdb->get_var( $query ) == $wpdb->prefix . self::TABLE_NAME ) { |
| 161 | $query_forms = $wpdb->prepare( |
| 162 | 'SELECT * FROM ' . $wpdb->prefix . self::TABLE_NAME . ' WHERE pID= %d', |
| 163 | array(esc_sql($pID)) |
| 164 | ); |
| 165 | $trans = $wpdb->get_results( $query_forms ); // db call ok; no-cache ok. |
| 166 | if ( $trans ) { |
| 167 | foreach ( $trans as $tran ) { |
| 168 | SIB_Forms::deleteForm( $tran->frmID ); |
| 169 | } |
| 170 | } |
| 171 | $wpdb->delete( |
| 172 | $wpdb->prefix . self::TABLE_NAME, |
| 173 | array( |
| 174 | 'pID' => $pID, |
| 175 | ) |
| 176 | ); |
| 177 | } |
| 178 | |
| 179 | } |
| 180 | |
| 181 | /** |
| 182 | * Remove all translated forms |
| 183 | */ |
| 184 | public static function remove_all_trans() { |
| 185 | global $wpdb; |
| 186 | $query = "SHOW TABLES LIKE '" . $wpdb->prefix . self::TABLE_NAME . "'; "; |
| 187 | if ( $wpdb->get_var( $query ) == $wpdb->prefix . self::TABLE_NAME ) { |
| 188 | $wpdb->query( 'TRUNCATE TABLE ' . $wpdb->prefix . self::TABLE_NAME ); |
| 189 | } |
| 190 | } |
| 191 | } |
| 192 | } |