PageRenderTime 28ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

/code/order/SilvercartCarrier.php

https://bitbucket.org/silvercart/silvercart/
PHP | 270 lines | 111 code | 21 blank | 138 comment | 8 complexity | 5fd2452892d10ab002eef756e03949a4 MD5 | raw file
  1. <?php
  2. /**
  3. * Copyright 2010, 2011 pixeltricks GmbH
  4. *
  5. * This file is part of SilverCart.
  6. *
  7. * SilverCart is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation, either version 3 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * SilverCart is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public License
  18. * along with SilverCart. If not, see <http://www.gnu.org/licenses/>.
  19. *
  20. * @package Silvercart
  21. * @subpackage Order
  22. */
  23. /**
  24. * abstract for a shipping carrier
  25. *
  26. * @package Silvercart
  27. * @subpackage Order
  28. * @author Roland Lehmann <rlehmann@pixeltricks.de>
  29. * @copyright Pixeltricks GmbH
  30. * @since 06.11.2010
  31. * @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License
  32. */
  33. class SilvercartCarrier extends DataObject {
  34. /**
  35. * Attributes.
  36. *
  37. * @var array
  38. *
  39. * @author Sascha Koehler <skoehler@pixeltricks.de>
  40. * @copyright 2011 pixeltricks GmbH
  41. * @since 31.01.2011
  42. */
  43. public static $db = array(
  44. 'Title' => 'VarChar(25)',
  45. 'FullTitle' => 'VarChar(60)'
  46. );
  47. /**
  48. * Has-many relationship.
  49. *
  50. * @var array
  51. *
  52. * @author Sascha Koehler <skoehler@pixeltricks.de>
  53. * @copyright 2011 pixeltricks GmbH
  54. * @since 31.01.2011
  55. */
  56. public static $has_many = array(
  57. 'SilvercartShippingMethods' => 'SilvercartShippingMethod',
  58. 'SilvercartZones' => 'SilvercartZone'
  59. );
  60. /**
  61. * Summaryfields for display in tables.
  62. *
  63. * @var array
  64. *
  65. * @author Sascha Koehler <skoehler@pixeltricks.de>
  66. * @copyright 2011 pixeltricks GmbH
  67. * @since 31.01.2011
  68. */
  69. public static $summary_fields = array(
  70. 'Title' => 'Name',
  71. 'AttributedZones' => 'Zugeordnete Zonen',
  72. 'AttributedShippingMethods' => 'Zugeordnete Versandarten'
  73. );
  74. /**
  75. * Column labels for display in tables.
  76. *
  77. * @var array
  78. *
  79. * @author Sascha Koehler <skoehler@pixeltricks.de>
  80. * @copyright 2011 pixeltricks GmbH
  81. * @since 31.01.2011
  82. */
  83. public static $field_labels = array(
  84. 'Title' => 'Name',
  85. 'FullTitle' => 'voller Name',
  86. 'AttributedZones' => 'Zugeordnete Zonen',
  87. 'AttributedShippingMethods' => 'Zugeordnete Versandarten'
  88. );
  89. /**
  90. * Virtual database fields.
  91. *
  92. * @var array
  93. *
  94. * @author Sascha Koehler <skoehler@pixeltricks.de>
  95. * @copyright 2011 pixeltricks GmbH
  96. * @since 31.01.2011
  97. */
  98. public static $casting = array(
  99. 'AttributedZones' => 'Varchar(255)',
  100. 'AttributedShippingMethods' => 'Varchar(255)'
  101. );
  102. /**
  103. * Defines the form fields for the search in ModelAdmin
  104. *
  105. * @return array seach fields definition
  106. *
  107. * @author Roland Lehmann <rlehmann@pixeltricks.de>
  108. * @since 5.7.2011
  109. */
  110. public function searchableFields() {
  111. $seachableFields = array(
  112. 'Title' => array(
  113. 'title' => _t('SilvercartCarrier.SINGULARNAME'),
  114. 'filter' => 'PartialMatchFilter'
  115. ),
  116. 'SilvercartZones.ID' => array(
  117. 'title' => _t('SilvercartCountry.ATTRIBUTED_ZONES'),
  118. 'filter' => 'ExactMatchFilter'
  119. ),
  120. 'SilvercartShippingMethods.ID' => array(
  121. 'title' => _t('SilvercartCarrier.ATTRIBUTED_SHIPPINGMETHODS'),
  122. 'filter' => 'ExactMatchFilter'
  123. )
  124. );
  125. $this->extend('updateSearchableFields', $searchableFields);
  126. return $seachableFields;
  127. }
  128. /**
  129. * Returns the objects field labels
  130. *
  131. * @param bool $includerelations configuration setting
  132. *
  133. * @return array the translated field labels
  134. *
  135. * @author Roland Lehmann <rlehmann@pixeltricks.de>
  136. * @since 5.7.2011
  137. */
  138. public function fieldLabels($includerelations = true) {
  139. return array_merge(
  140. parent::fieldLabels($includerelations),
  141. array(
  142. 'Title' => _t('SilvercartProduct.COLUMN_TITLE'),
  143. 'FullTitle' => _t('SilvercartCarrier.FULL_NAME', 'full name'),
  144. 'AttributedZones' => _t('SilvercartCountry.ATTRIBUTED_ZONES'),
  145. 'AttributedShippingMethods' => _t('SilvercartCarrier.ATTRIBUTED_SHIPPINGMETHODS'),
  146. 'SilvercartShippingMethods' => _t('SilvercartShippingMethod.PLURALNAME', 'zones'),
  147. 'SilvercartZones' => _t('SilvercartZone.PLURALNAME', 'zones')
  148. )
  149. );
  150. }
  151. /**
  152. * Sets the summary fields.
  153. *
  154. * @return array
  155. *
  156. * @author Roland Lehmann <rlehmann@pixeltricks.de>
  157. * @since 5.7.2011
  158. */
  159. public function summaryFields() {
  160. return array_merge(
  161. parent::summaryFields(),
  162. array(
  163. 'Title' => _t('SilvercartProduct.COLUMN_TITLE'),
  164. 'AttributedZones' => _t('SilvercartCountry.ATTRIBUTED_ZONES'),
  165. 'AttributedShippingMethods' => _t('SilvercartCarrier.ATTRIBUTED_SHIPPINGMETHODS', 'attributed shipping methods')
  166. )
  167. );
  168. }
  169. /**
  170. * Returns the translated singular name of the object. If no translation exists
  171. * the class name will be returned.
  172. *
  173. * @return string The objects singular name
  174. *
  175. * @author Roland Lehmann <rlehmann@pixeltricks.de>
  176. * @since 5.7.2011
  177. */
  178. public function singular_name() {
  179. if (_t('SilvercartCarrier.SINGULARNAME')) {
  180. return _t('SilvercartCarrier.SINGULARNAME');
  181. } else {
  182. return parent::singular_name();
  183. }
  184. }
  185. /**
  186. * Returns the translated plural name of the object. If no translation exists
  187. * the class name will be returned.
  188. *
  189. * @return string the objects plural name
  190. *
  191. * @author Roland Lehmann <rlehmann@pixeltricks.de>
  192. * @since 5.7.2011
  193. */
  194. public function plural_name() {
  195. if (_t('SilvercartCarrier.PLURALNAME')) {
  196. return _t('SilvercartCarrier.PLURALNAME');
  197. } else {
  198. return parent::plural_name();
  199. }
  200. }
  201. /**
  202. * Returns the attributed zones as string (limited to 150 chars).
  203. *
  204. * @return string
  205. *
  206. * @author Sascha Koehler <skoehler@pixeltricks.de>
  207. * @copyright 2011 pixeltricks GmbH
  208. * @since 31.01.2011
  209. */
  210. public function AttributedZones() {
  211. $attributedZonesStr = '';
  212. $attributedZones = array();
  213. $maxLength = 150;
  214. foreach ($this->SilvercartZones() as $zone) {
  215. $attributedZones[] = $zone->Title;
  216. }
  217. if (!empty($attributedZones)) {
  218. $attributedZonesStr = implode(', ', $attributedZones);
  219. if (strlen($attributedZonesStr) > $maxLength) {
  220. $attributedZonesStr = substr($attributedZonesStr, 0, $maxLength).'...';
  221. }
  222. }
  223. return $attributedZonesStr;
  224. }
  225. /**
  226. * Returns the attributed shipping methods as string (limited to 150 chars).
  227. *
  228. * @return string
  229. *
  230. * @author Sascha Koehler <skoehler@pixeltricks.de>
  231. * @copyright 2011 pixeltricks GmbH
  232. * @since 31.01.2011
  233. */
  234. public function AttributedShippingMethods() {
  235. $attributedShippingMethodsStr = '';
  236. $attributedShippingMethods = array();
  237. $maxLength = 150;
  238. foreach ($this->SilvercartShippingMethods() as $shippingMethod) {
  239. $attributedShippingMethods[] = $shippingMethod->Title;
  240. }
  241. if (!empty($attributedShippingMethods)) {
  242. $attributedShippingMethodsStr = implode(', ', $attributedShippingMethods);
  243. if (strlen($attributedShippingMethodsStr) > $maxLength) {
  244. $attributedShippingMethodsStr = substr($attributedShippingMethodsStr, 0, $maxLength).'...';
  245. }
  246. }
  247. return $attributedShippingMethodsStr;
  248. }
  249. }