MS_View_Settings_Import

Displays the Import preview.

Defined (1)

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

/app/view/settings/class-ms-view-settings-import.php  
  1. class MS_View_Settings_Import extends MS_View { 
  2.  
  3. /** 
  4. * Displays the import preview form. 
  5. * @since 1.0.0 
  6. * @return string 
  7. */ 
  8. public function to_html() { 
  9. $data = apply_filters( 
  10. 'ms_import_preview_data_before',  
  11. $this->data['model']->source 
  12. ); 
  13. $compact = ! empty( $this->data['compact'] ); 
  14. if ( ! is_object( $data ) ) { 
  15. $data = (object) array( 
  16. 'memberships' => array(),  
  17. 'members' => array(),  
  18. 'notes' => array(),  
  19. 'settings' => array(),  
  20. 'source' => '',  
  21. 'export_time' => '',  
  22. ); 
  23.  
  24. // Converts object to array 
  25. $data->memberships = (array) $data->memberships; 
  26. $data->members = (array) $data->members; 
  27.  
  28. $fields = $this->prepare_fields( $data ); 
  29.  
  30. if ( $compact ) { 
  31. $overview_box = array( 
  32. $fields['batchsize'],  
  33. $fields['sep'],  
  34. $fields['clear_all'],  
  35. $fields['skip'],  
  36. $fields['import'],  
  37. ); 
  38. } else { 
  39. $overview_box = array( 
  40. $fields['details'],  
  41. $fields['sep'],  
  42. $fields['batchsize'],  
  43. $fields['sep'],  
  44. $fields['clear_all'],  
  45. $fields['back'],  
  46. $fields['import'],  
  47. $fields['download'],  
  48. ); 
  49.  
  50. ob_start(); 
  51. MS_Helper_Html::settings_box( 
  52. $overview_box,  
  53. __( 'Import Overview', 'membership2' ) 
  54. ); 
  55.  
  56. if ( ! $compact ) { 
  57. MS_Helper_Html::settings_box( 
  58. array( $fields['memberships'] ),  
  59. __( 'List of all Memberships', 'membership2' ),  
  60. '',  
  61. 'open' 
  62. ); 
  63.  
  64. MS_Helper_Html::settings_box( 
  65. array( $fields['members'] ),  
  66. __( 'List of all Members', 'membership2' ),  
  67. '',  
  68. 'open' 
  69. ); 
  70.  
  71. MS_Helper_Html::settings_box( 
  72. array( $fields['settings'] ),  
  73. __( 'Imported Settings', 'membership2' ),  
  74. '',  
  75. 'open' 
  76. ); 
  77.  
  78. echo '<script>window._ms_import_obj = ' . json_encode( $data ) . '</script>'; 
  79.  
  80. $html = ob_get_clean(); 
  81.  
  82. return apply_filters( 
  83. 'ms_import_preview_object',  
  84. $html,  
  85. $data 
  86. ); 
  87.  
  88. /** 
  89. * Prepare the HTML fields that can be displayed 
  90. * @since 1.0.0 
  91. * @return array 
  92. */ 
  93. protected function prepare_fields( $data ) { 
  94. // List of known Membership types; used to display the nice-name 
  95. $ms_types = MS_Model_Membership::get_types(); 
  96. $ms_paytypes = MS_Model_Membership::get_payment_types(); 
  97.  
  98. // Prepare the "Memberships" table 
  99. $memberships = array( 
  100. array( 
  101. __( 'Membership name', 'membership2' ),  
  102. __( 'Membership Type', 'membership2' ),  
  103. __( 'Payment Type', 'membership2' ),  
  104. __( 'Description', 'membership2' ),  
  105. ),  
  106. ); 
  107.  
  108. foreach ( $data->memberships as $item ) { 
  109. if ( ! isset( $ms_types[$item->type] ) ) { 
  110. $item->type = MS_Model_Membership::TYPE_STANDARD; 
  111.  
  112. switch ( $item->pay_type ) { 
  113. case 'recurring': 
  114. $payment_type = MS_Model_Membership::PAYMENT_TYPE_RECURRING; 
  115. break; 
  116.  
  117. case 'finite': 
  118. $payment_type = MS_Model_Membership::PAYMENT_TYPE_FINITE; 
  119. break; 
  120.  
  121. case 'date': 
  122. $payment_type = MS_Model_Membership::PAYMENT_TYPE_DATE_RANGE; 
  123. break; 
  124.  
  125. default: 
  126. $payment_type = MS_Model_Membership::PAYMENT_TYPE_PERMANENT; 
  127. break; 
  128.  
  129. $memberships[] = array( 
  130. $item->name,  
  131. $ms_types[$item->type],  
  132. $ms_paytypes[$payment_type],  
  133. $item->description,  
  134. ); 
  135.  
  136. // Prepare the "Members" table 
  137. $members = array( 
  138. array( 
  139. __( 'Username', 'membership2' ),  
  140. __( 'Email', 'membership2' ),  
  141. __( 'Subscriptions', 'membership2' ),  
  142. __( 'Invoices', 'membership2' ),  
  143. ),  
  144. ); 
  145.  
  146. foreach ( $data->members as $item ) { 
  147. $inv_count = 0; 
  148. if ( isset( $item->subscriptions ) 
  149. && is_array( $item->subscriptions ) 
  150. ) { 
  151. foreach ( $item->subscriptions as $registration ) { 
  152. $inv_count += count( $registration->invoices ); 
  153.  
  154. $members[] = array( 
  155. $item->username,  
  156. $item->email,  
  157. count( $item->subscriptions ),  
  158. $inv_count,  
  159. ); 
  160.  
  161. $settings = array(); 
  162. foreach ( $data->settings as $setting => $value ) { 
  163. switch ( $setting ) { 
  164. case 'addons': 
  165. $model = MS_Factory::load( 'MS_Model_Addon' ); 
  166. $list = $model->get_addon_list(); 
  167. $code = ''; 
  168. foreach ( $value as $addon => $state ) { 
  169. if ( $state ) { 
  170. $code .= __( 'Activate: ', 'membership2' ); 
  171. } else { 
  172. $code .= __( 'Dectivate: ', 'membership2' ); 
  173. $code .= $list[$addon]->name . '<br/>'; 
  174. $settings[] = array( 
  175. __( 'Add-Ons', 'membership2' ),  
  176. $code,  
  177. ); 
  178. break; 
  179. if ( empty( $settings ) ) { 
  180. $settings[] = array( 
  181. '',  
  182. __( '(No settings are changed)', 'membership2' ),  
  183. ); 
  184.  
  185. // Prepare the return value. 
  186. $fields = array(); 
  187.  
  188. // Export-Notes 
  189. $notes = ''; 
  190. if ( isset( $data->notes ) ) { 
  191. if ( is_scalar( $data->notes ) ) { 
  192. $notes = array( $data->notes ); 
  193.  
  194. $in_sub = false; 
  195. $notes = '<ul class="ms-import-notes">'; 
  196. foreach ( $data->notes as $line => $text ) { 
  197. $is_sub = ( strpos( $text, '- ' ) === 0 ); 
  198. if ( $in_sub != $is_sub ) { 
  199. $in_sub = $is_sub; 
  200. if ( $is_sub ) { 
  201. $notes .= '<ul>'; 
  202. } else { 
  203. $notes .= '</ul>'; 
  204. if ( $in_sub ) { 
  205. $text = substr( $text, 2 ); 
  206. $notes .= '<li>' . $text; 
  207. $notes .= '</ul>'; 
  208.  
  209. $fields['details'] = array( 
  210. 'type' => MS_Helper_Html::TYPE_HTML_TABLE,  
  211. 'class' => 'ms-import-preview',  
  212. 'value' => array( 
  213. array( 
  214. __( 'Data source', 'membership2' ),  
  215. $data->source . 
  216. '   <small>' . 
  217. sprintf( 
  218. __( 'exported on %1$s', 'membership2' ),  
  219. $data->export_time 
  220. ) . 
  221. '</small>',  
  222. ),  
  223. array( 
  224. __( 'Content', 'membership2' ),  
  225. sprintf( 
  226. _n( 
  227. '%1$s Membership',  
  228. '%1$s Memberships',  
  229. count( $data->memberships ),  
  230. 'membership2' 
  231. ),  
  232. '<b>' . count( $data->memberships ) . '</b>' 
  233. ) . ' / ' . sprintf( 
  234. _n( 
  235. '%1$s Member',  
  236. '%1$s Members',  
  237. count( $data->members ),  
  238. 'membership2' 
  239. ),  
  240. '<b>' . count( $data->members ) . '</b>' 
  241. ),  
  242. ),  
  243. ),  
  244. 'field_options' => array( 
  245. 'head_col' => true,  
  246. 'head_row' => false,  
  247. 'col_class' => array( 'preview-label', 'preview-data' ),  
  248. ); 
  249.  
  250. if ( ! empty( $notes ) ) { 
  251. $fields['details']['value'][] = array( 
  252. __( 'Please note', 'membership2' ),  
  253. $notes,  
  254. ); 
  255.  
  256. $batchsizes = array( 
  257. 10 => __( 'Small (10 items)' ),  
  258. 30 => __( 'Normal (30 items)' ),  
  259. 100 => __( 'Big (100 items)' ),  
  260. ); 
  261.  
  262. $fields['batchsize'] = array( 
  263. 'id' => 'batchsize',  
  264. 'type' => MS_Helper_Html::INPUT_TYPE_SELECT,  
  265. 'title' => __( 'Batch size for import', 'membership2' ),  
  266. 'desc' => __( 'Big batches will be processed faster but may result in PHP Memory errors.', 'membership2' ),  
  267. 'value' => 30,  
  268. 'field_options' => $batchsizes,  
  269. 'class' => 'sel-batchsize',  
  270. ); 
  271.  
  272. $fields['clear_all'] = array( 
  273. 'id' => 'clear_all',  
  274. 'type' => MS_Helper_Html::INPUT_TYPE_CHECKBOX,  
  275. 'title' => __( 'Replace current content with import data (removes existing Memberships/Members before importing data)', 'membership2' ),  
  276. 'class' => 'widefat',  
  277. ); 
  278.  
  279. $fields['memberships'] = array( 
  280. 'type' => MS_Helper_Html::TYPE_HTML_TABLE,  
  281. 'class' => 'ms-import-preview',  
  282. 'value' => $memberships,  
  283. 'field_options' => array( 
  284. 'head_col' => false,  
  285. 'head_row' => true,  
  286. 'col_class' => array( 'preview-name', 'preview-type', 'preview-pay-type', 'preview-desc' ),  
  287. ); 
  288.  
  289. $fields['members'] = array( 
  290. 'type' => MS_Helper_Html::TYPE_HTML_TABLE,  
  291. 'class' => 'ms-import-preview',  
  292. 'value' => $members,  
  293. 'field_options' => array( 
  294. 'head_col' => false,  
  295. 'head_row' => true,  
  296. 'col_class' => array( 'preview-name', 'preview-email', 'preview-count', 'preview-count' ),  
  297. ); 
  298.  
  299. $fields['settings'] = array( 
  300. 'type' => MS_Helper_Html::TYPE_HTML_TABLE,  
  301. 'class' => 'ms-import-preview',  
  302. 'value' => $settings,  
  303. 'field_options' => array( 
  304. 'head_col' => true,  
  305. 'head_row' => false,  
  306. ); 
  307.  
  308. $fields['sep'] = array( 
  309. 'type' => MS_Helper_Html::TYPE_HTML_SEPARATOR,  
  310. ); 
  311.  
  312. $fields['back'] = array( 
  313. 'type' => MS_Helper_Html::TYPE_HTML_LINK,  
  314. 'class' => 'wpmui-field-button button',  
  315. 'value' => __( 'Cancel', 'membership2' ),  
  316. 'url' => $_SERVER['REQUEST_URI'],  
  317. ); 
  318.  
  319. $fields['skip'] = array( 
  320. 'type' => MS_Helper_Html::TYPE_HTML_LINK,  
  321. 'class' => 'wpmui-field-button button',  
  322. 'value' => __( 'Skip', 'membership2' ),  
  323. 'url' => MS_Controller_Plugin::get_admin_url( 
  324. false,  
  325. array( 'skip_import' => 1 ) 
  326. ),  
  327. ); 
  328.  
  329. $fields['import'] = array( 
  330. 'id' => 'btn-import',  
  331. 'type' => MS_Helper_Html::INPUT_TYPE_BUTTON,  
  332. 'value' => __( 'Import', 'membership2' ),  
  333. 'button_value' => MS_Controller_Import::AJAX_ACTION_IMPORT,  
  334. 'button_type' => 'submit',  
  335. ); 
  336.  
  337. $fields['download'] = array( 
  338. 'id' => 'btn-download',  
  339. 'type' => MS_Helper_Html::INPUT_TYPE_BUTTON,  
  340. 'value' => __( 'Download as Export File', 'membership2' ),  
  341. 'class' => 'button-link',  
  342. ); 
  343.  
  344. return $fields; 
  345.