PageRenderTime 38ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/modules/shop/classes/model/shipmethods/newpost.php

https://bitbucket.org/seyar/ari100krat.local
PHP | 191 lines | 152 code | 20 blank | 19 comment | 12 complexity | 7c0654097cd22b526632096faaa10823 MD5 | raw file
Possible License(s): BSD-3-Clause, LGPL-2.1
  1. <?php defined('SYSPATH') OR die('No direct access allowed.');
  2. /* @version $Id: v 0.1 ${date} - ${time} Exp $
  3. *
  4. * Project: ${project.name}
  5. * File: ${nameAndExt} *
  6. *
  7. * This library is commercial distributed software; you can't
  8. * redistribute it and/or modify it without owner (or author) approval.
  9. *
  10. * @link http://bestartdesign.com
  11. * @Best IT Solutions (C) 2010
  12. *
  13. * @author ${fullName} ${email}
  14. */
  15. class Model_Shipmethods_Newpost extends Model_ShipMethods
  16. {
  17. public $method = 'Newpost';
  18. public $sum = 0;
  19. public $weight = 0;
  20. public $border = 0;
  21. public function show()
  22. {
  23. die('func show');
  24. }
  25. public function getItem($id)
  26. {
  27. $parent = parent::getMainItem( array('id' => $id, 'fs_name' => $this->method) );
  28. $result =
  29. DB::select()
  30. ->from('shipmethod_'.strtolower($this->method))
  31. ->where('id','=', $parent['settings_id'] )
  32. ->execute()
  33. ;
  34. if( $result->count() == 0 ) return $parent;
  35. else{
  36. $return = $result->current();
  37. $return['borders'] = unserialize($return['borders']);
  38. return array_merge($return, $parent);
  39. }
  40. }
  41. public function addItem($lang_id = 1)
  42. {
  43. if ($post = $this->validate_save())
  44. {
  45. $borders = array();
  46. if($post['border_sum'] && $post['border_value'])
  47. {
  48. foreach( $post['border_value'] as $key => $item )
  49. $borders[$post['border_sum'][$key]] = preg_replace ('/[^\d][^\%]/', '', $post['border_value']);
  50. }
  51. list($id, $total_rows) =
  52. DB::insert('shipmethod_'.strtolower($this->method), array('borders') )
  53. ->values(
  54. array(serialize($borders) )
  55. )
  56. // ->where('id', '=', $id)
  57. ->execute()
  58. ;
  59. list($id, $total_rows) =
  60. DB::insert('shop_shipment_methods', array('settings_id','fs_name','name','description','text', 'letter_description', 'orderid') )
  61. ->values(
  62. array(
  63. $id,
  64. $post['basic_fs_name'],
  65. $post['name'],
  66. $post['description'],
  67. $post['text'],
  68. $post['letter_description'],
  69. $post['orderid']
  70. )
  71. )
  72. // ->where('id', '=', $id)
  73. ->execute()
  74. ;
  75. return $id;
  76. }else {
  77. return false;
  78. }
  79. }
  80. public function save($id, $lang_id = 1)
  81. {
  82. if ($post = $this->validate_save())
  83. {
  84. $parent = parent::getMainItem(array('id'=>$id, 'fs_name' => $this->method) );
  85. $borders = array();
  86. if($post['border_sum'] && $post['border_value'])
  87. {
  88. foreach( $post['border_value'] as $key => $item )
  89. {
  90. $item = str_replace(',','.',$item);
  91. $item = preg_replace('/[^\d\.,%]/', '', $item);
  92. if( $item )
  93. $borders[$post['border_sum'][$key]] = $item;
  94. }
  95. }
  96. DB::update('shipmethod_'.strtolower($this->method))
  97. ->set(
  98. array(
  99. 'borders' => serialize($borders),
  100. )
  101. )
  102. ->where('id', '=', $parent['settings_id'])
  103. ->execute()
  104. ;
  105. DB::update('shop_shipment_methods')
  106. ->set(
  107. array(
  108. 'name' => $post['name'],
  109. 'description' => $post['description'],
  110. 'text' => $post['text'],
  111. 'letter_description' => $post['letter_description'],
  112. 'orderid' => $post['orderid'],
  113. )
  114. )
  115. ->where('id', '=', $parent['id'])
  116. ->execute()
  117. ;
  118. return $id;
  119. }else {
  120. return false;
  121. }
  122. }
  123. public function validate_save()
  124. {
  125. $keys = array (
  126. 'name' ,
  127. 'description' ,
  128. 'text' ,
  129. 'orderid' ,
  130. 'basic_fs_name',
  131. 'letter_description',
  132. 'border_sum',
  133. 'border_value',
  134. );
  135. $params = Arr::extract($_POST, $keys, '');
  136. $post = Validate::factory($params)
  137. ->rule('name', 'not_empty')
  138. // ->rule('border_sum', 'digit')
  139. // ->rule('border_value', 'regex', array("/^[0-9]+((\.|,)[0-9]{1,2})?$/"))
  140. // ->rule('percent', 'not_empty')
  141. ->rule('basic_fs_name', 'not_empty')
  142. // ->rule('percent', 'digit')
  143. ;
  144. if ($post->check())
  145. {
  146. return $params;
  147. }
  148. else
  149. {
  150. $this->errors = $post->errors('validate');
  151. }
  152. }
  153. public function getCost($method_id)
  154. {
  155. if($method_id)
  156. {
  157. $method = $this->getItem($method_id);
  158. krsort($method['borders']);
  159. $border = array_walk($method['borders'], array('Model_Shipmethods_Newpost', 'foundborder'));
  160. return (float)$method['borders'][$this->border];
  161. }
  162. }
  163. function foundborder($item, $key = 0)
  164. {
  165. if( $this->sum < $key )
  166. {
  167. $this->border = $key; // return $key;
  168. }
  169. }
  170. }
  171. ?>