/modules/shop/classes/model/paymethods.php

https://bitbucket.org/seyar/kinda.local · PHP · 347 lines · 276 code · 56 blank · 15 comment · 25 complexity · a7d5038b91f33d968f117f6cd4fc0dd6 MD5 · raw file

  1. <?php
  2. defined('SYSPATH') OR die('No direct access allowed.');
  3. class Model_PayMethods extends Model
  4. {
  5. // Singleton static instance
  6. private static $_instance;
  7. public function setMethod($class)
  8. {
  9. $class = "Model_Paymethods_$class";
  10. return new $class;
  11. }
  12. public static function instance()
  13. {
  14. if (self::$_instance === NULL)
  15. {
  16. // Create a new instance
  17. self::$_instance = new self;
  18. }
  19. return self::$_instance;
  20. }
  21. public function show()
  22. {
  23. $post = $_POST;
  24. if (!isset($_POST['rows_num']))
  25. {
  26. $post['rows_num'] = 0;
  27. }
  28. $rows_per_page = ((int) $post['rows_num']) ? ((int) $post['rows_num']) : (int) cookie::get('rows_per_page', 10);
  29. if (isset($_POST['rows_per_page']))
  30. {
  31. $rows_per_page = $_POST['rows_per_page'];
  32. }
  33. cookie::set('rows_per_page', $rows_per_page);
  34. $_POST['rows_num'] = $rows_per_page;
  35. //
  36. $query_c = DB::select(DB::expr('COUNT(*) AS mycount'))
  37. ->from('shop_payment_methods')
  38. ;
  39. $count = $query_c
  40. ->execute()
  41. ->get('mycount');
  42. $pagination = Pagination::factory(array(
  43. 'total_items' => $count,
  44. 'items_per_page' => $rows_per_page,
  45. ));
  46. $query = DB::select('*')
  47. ->from('shop_payment_methods')
  48. ;
  49. $result = $query
  50. ->order_by('name', 'ASC')
  51. ->limit($pagination->items_per_page)
  52. ->offset($pagination->offset)
  53. ->execute();
  54. $result = $result->as_array();
  55. return array($result, $pagination, $result);
  56. }
  57. public function delete($id = NULL)
  58. {
  59. $query = DB::delete('shop_payment_methods');
  60. if (is_numeric($id))
  61. {
  62. $query->where('id', '=', $id); // ->limit(1)
  63. $total_rows = $query->execute();
  64. }
  65. }
  66. public function delete_list($array = array(), $fs_name = NULL)
  67. {
  68. //die(Kohana::debug($array));
  69. $query = DB::delete('shop_payment_methods');
  70. if (count($array))
  71. {
  72. $query->where('id', 'in', array_flip($array)); // ->limit(1)
  73. $ids = self::getMainItem($array);
  74. $total_rows = $query->execute();
  75. $newids = array();
  76. foreach ($ids as $item)
  77. $newids[] = $item['settings_id'];
  78. //$this->setMethod($fs_name)->delete_list($newids);
  79. }
  80. }
  81. public function load($id = NULL)
  82. {
  83. if( (int)$id )
  84. {
  85. $query = DB::select()
  86. ->from('shop_payment_methods')
  87. ->where('id', '=', $id)
  88. ->limit(1)
  89. ;
  90. $result = $query->execute();
  91. //die(Kohana::debug($result));
  92. if ($result->count() == 0) return array();
  93. else
  94. {
  95. $return = $result->current();
  96. $return2 = $this->setMethod($return['fs_name'])->getItem( array('id' => $return['settings_id']), FALSE );
  97. $return['forShipment'] = explode(',', $return['forShipment']);
  98. return array_merge($return, $return2);
  99. }
  100. }
  101. else
  102. {
  103. return array();
  104. }
  105. }
  106. public function add($lang_id = 1)
  107. {
  108. if ($post = $this->validate_save())
  109. {
  110. $paytype = DB::select('type')->from('shop_payment_methods')->where('fs_name', '=', $post['fs_name'])->limit(1)->execute()->get('type');
  111. list($id, $total_rows) = DB::insert('shop_payment_methods',
  112. array(
  113. 'name', 'fs_name', 'text', 'forShipment', 'include_tax', 'type', 'orderid','rateMethod'
  114. )
  115. )
  116. ->values(
  117. array(
  118. $post['name'],
  119. $post['fs_name'],
  120. $post['text'],
  121. // $post['description'],
  122. implode(',', $post['forShipment']),
  123. $post['include_tax'],
  124. $paytype,
  125. $post['orderid'],
  126. $post['rateMethod'],
  127. )
  128. )
  129. ->execute()
  130. ;
  131. $insert_id = $this->setMethod($post['fs_name'])->add($post);
  132. self::update($id, array('settings_id' => $insert_id));
  133. return $id;
  134. }
  135. else
  136. {
  137. return false;
  138. }
  139. }
  140. public function save($lang_id = 1, $id = NULL)
  141. {
  142. if( $post = $this->validate_save() )
  143. {
  144. if ((int)$id)
  145. { // update
  146. DB::update('shop_payment_methods')
  147. ->set(
  148. array(
  149. 'name' => $post['name'],
  150. // 'cost' => $post['cost'],
  151. // 'description' => $post['description'],
  152. 'text' => $post['text'],
  153. 'orderid' => $post['orderid'],
  154. 'include_tax' => $post['include_tax'],
  155. 'rateMethod' => $post['rateMethod'],
  156. 'forShipment' => implode(',',$post['forShipment']),
  157. )
  158. )
  159. ->where('id', '=', $id)
  160. ->execute()
  161. ;
  162. $this->setMethod($post['fs_name'])->save( $post['settings_id'],$post );
  163. }
  164. return $id;
  165. }
  166. else
  167. {
  168. return false;
  169. }
  170. }
  171. static public function update( $id = NULL, $data)
  172. {
  173. if ( $id && $data )
  174. {
  175. DB::update('shop_payment_methods')
  176. ->set(
  177. $data
  178. )
  179. ->where('id', '=', $id)
  180. ->execute()
  181. ;
  182. return true;
  183. }
  184. else
  185. return false;
  186. }
  187. function validate_save()
  188. {
  189. $keys = array(
  190. "fs_name",
  191. "name",
  192. "cost",
  193. // "margin",
  194. "settings_id",
  195. "percent",
  196. "plusToOrder",
  197. "invoice",
  198. "ordersendmail",
  199. "letter_description",
  200. "include_tax",
  201. "forShipment",
  202. "text",
  203. "postavshik",
  204. "merchant_info",
  205. "payment_testmode",
  206. "payment_rule",
  207. "action_url",
  208. "payment_type",
  209. "payment_addvalue",
  210. "secretcode",
  211. "orderid",
  212. "rateMethod",
  213. );
  214. $params = Arr::extract($_POST, $keys, '');
  215. $post = Validate::factory($params)
  216. ->rule('name', 'not_empty')
  217. // ->rule('cost', 'not_empty')
  218. // ->rule('cost', 'digit')
  219. ;
  220. if ($post->check())
  221. {
  222. return $params;
  223. }
  224. else
  225. {
  226. $this->errors = $post->errors('validate');
  227. }
  228. }
  229. static function getPaymentGroups($where, $as_array = TRUE)
  230. {
  231. $query = DB::select()
  232. ->from('shop_payment_methods')
  233. ->where('protected', '=', '1');
  234. if ($where)
  235. {
  236. foreach ($where as $key => $item)
  237. $query->and_where($key, '=', $item);
  238. }
  239. $result = $query->execute();
  240. if ($result->count() == 0)
  241. return array();
  242. else
  243. {
  244. if (!$as_array)
  245. return $result->current();
  246. return $result->as_array('id');
  247. }
  248. }
  249. static public function getMainItem($where = NULL, $as_array = TRUE)
  250. {
  251. $query = DB::select()
  252. ->from('shop_payment_methods');
  253. $query->where('id', 'IN', array_flip($where));
  254. // ->limit(1);
  255. $result = $query->execute();
  256. if ($result->count() == 0)
  257. return array();
  258. else
  259. {
  260. if ($as_array == TRUE)
  261. $return = $result->as_array();
  262. else
  263. $return = $result->current();
  264. // $return2 = $this->setMethod($return['fs_name'])->getItem( array('id' => $return['settings_id']), FALSE );
  265. // $return['forShipment'] = explode(',', $return['forShipment']);
  266. return $return;
  267. }
  268. }
  269. public function status($id = NULL)
  270. {
  271. $query = DB::update('shop_payment_methods')->set(array(
  272. 'status' => DB::expr('!status')
  273. ));
  274. if (is_numeric($id))
  275. {
  276. $query->where('id', '=', $id); // ->limit(1)
  277. $total_rows = $query->execute();
  278. }
  279. }
  280. public function updateOrderId( $data = array() )
  281. {
  282. if( !empty($data) ):
  283. foreach( $data as $key => $item )
  284. {
  285. if( !self::update( $item, array( 'orderid'=> $key )) )
  286. return false;
  287. }
  288. endif;
  289. return TRUE;
  290. }
  291. }