Advertisement
Guest User

Untitled

a guest
Feb 23rd, 2015
189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.53 KB | None | 0 0
  1. <?php
  2. /**
  3. * Coupon rates that can be set in {@link SiteConfig}. Several flat rates can be set
  4. * for any supported shipping country.
  5. */
  6. class Coupon extends DataObject implements PermissionProvider {
  7.  
  8. /**
  9. * Fields for this tax rate
  10. *
  11. * @var Array
  12. */
  13. private static $db = array(
  14. 'Title' => 'Varchar',
  15. 'Code' => 'Varchar',
  16. 'Discount' => 'Decimal(18,2)',
  17. 'Expiry' => 'Date',
  18. 'ProductCode' => 'Varchar'
  19. );
  20.  
  21. /**
  22. * Coupon rates are associated with SiteConfigs.
  23. *
  24. * @var unknown_type
  25. */
  26. private static $has_one = array(
  27. 'ShopConfig' => 'ShopConfig'
  28. );
  29.  
  30. public static $many_many = array(
  31. 'DiscountProducts' => 'Product'
  32. );
  33.  
  34. private static $summary_fields = array(
  35. 'Title' => 'Title',
  36. 'Code' => 'Code',
  37. 'SummaryOfDiscount' => 'Discount',
  38. 'Expiry' => 'Expiry',
  39. 'ProductCode' => 'Product Code'
  40. );
  41.  
  42. public function providePermissions()
  43. {
  44. return array(
  45. 'EDIT_COUPONS' => 'Edit Coupons',
  46. );
  47. }
  48.  
  49. public function canEdit($member = null)
  50. {
  51. return Permission::check('EDIT_COUPONS');
  52. }
  53.  
  54. public function canView($member = null)
  55. {
  56. return true;
  57. }
  58.  
  59. public function canDelete($member = null)
  60. {
  61. return Permission::check('EDIT_COUPONS');
  62. }
  63.  
  64. public function canCreate($member = null)
  65. {
  66. return Permission::check('EDIT_COUPONS');
  67. }
  68.  
  69. /**
  70. * Field for editing a {@link Coupon}.
  71. *
  72. * @return FieldSet
  73. */
  74. public function getCMSFields() {
  75.  
  76. $fields = new FieldList(new TabSet('Root'));
  77.  
  78. $fields->addFieldToTab("Root.CouponRate", New TextField('Title', _t('Coupon.TITLE', 'Title')));
  79. $fields->addFieldToTab("Root.CouponRate", New TextField('Code', _t('Coupon.CODE', 'Code')));
  80.  
  81. $discount = New NumericField('Discount', _t('Coupon.DISCOUNT', 'Coupon discount'));
  82. $discount->setRightTitle('As a $ amount');
  83. $fields->addFieldToTab('Root.CouponRate', $discount);
  84.  
  85. $date = New DateField('Expiry');
  86. $date->setConfig('showcalendar', true);
  87. $fields->addFieldToTab('Root.CouponRate', $date);
  88.  
  89. $gridFieldComplex = GridFieldConfig_RecordEditor::create()->addComponents(
  90. new GridFieldToolbarHeader(),
  91. new GridFieldSortableHeader(),
  92. new GridFieldDataColumns(),
  93. new GridFieldManyRelationHandler(),
  94. new GridFieldPaginator(10),
  95. new GridFieldEditButton(),
  96. new GridFieldDeleteAction(),
  97. new GridFieldDetailForm()
  98. );
  99.  
  100. $tablefield = new GridField(
  101. 'DiscountProducts',
  102. 'Discount Products:',
  103. $this->DiscountProducts(),
  104. $gridFieldComplex
  105. );
  106. $fields->addFieldToTab('Root.CouponRate', $tablefield);
  107.  
  108.  
  109. return $fields;
  110. }
  111.  
  112. /**
  113. * Label for using on {@link CouponModifierField}s.
  114. *
  115. * @see CouponModifierField
  116. * @return String
  117. */
  118. public function Label() {
  119. return $this->Title . ' ' . $this->SummaryOfDiscount() . ' discount';
  120. }
  121.  
  122. /**
  123. * Summary of the current tax rate
  124. *
  125. * @return String
  126. */
  127. public function SummaryOfDiscount() {
  128. return '$' . $this->Discount;
  129. }
  130.  
  131. public function Amount($order) {
  132.  
  133. // TODO: Multi currency
  134.  
  135. $shopConfig = ShopConfig::current_shop_config();
  136.  
  137. $amount = new Price();
  138. $amount->setCurrency($shopConfig->BaseCurrency);
  139. $amount->setSymbol($shopConfig->BaseCurrencySymbol);
  140.  
  141. $total = $order->SubTotal()->getAmount();
  142. $mods = $order->TotalModifications();
  143.  
  144. if ($mods && $mods->exists()) foreach ($mods as $mod) {
  145. if ($mod->ClassName != 'CouponModification') {
  146. $total += $mod->Amount()->getAmount();
  147. }
  148. }
  149. $amount->setAmount( - $this->Discount);
  150.  
  151. return $amount;
  152. }
  153.  
  154. /**
  155. * Display price, can decorate for multiple currency etc.
  156. *
  157. * @return Price
  158. */
  159. public function Price($order) {
  160.  
  161. $amount = $this->Amount($order);
  162. $this->extend('updatePrice', $amount);
  163. return $amount;
  164. }
  165.  
  166. }
  167.  
  168. /**
  169. * So that {@link Coupon}s can be created in {@link SiteConfig}.
  170. */
  171. class Coupon_Extension extends DataExtension {
  172.  
  173. /**
  174. * Attach {@link Coupon}s to {@link SiteConfig}.
  175. *
  176. * @see DataObjectDecorator::extraStatics()
  177. */
  178. public static $has_many = array(
  179. 'Coupons' => 'Coupon'
  180. );
  181. }
  182.  
  183. class Coupon_Admin extends ShopAdmin {
  184.  
  185. private static $tree_class = 'ShopConfig';
  186.  
  187. private static $allowed_actions = array(
  188. 'CouponSettings',
  189. 'CouponSettingsForm',
  190. 'saveCouponSettings'
  191. );
  192.  
  193. private static $url_rule = 'ShopConfig/Coupon';
  194. protected static $url_priority = 100;
  195. private static $menu_title = 'Shop Coupons';
  196.  
  197. private static $url_handlers = array(
  198. 'ShopConfig/Coupon/CouponSettingsForm' => 'CouponSettingsForm',
  199. 'ShopConfig/Coupon' => 'CouponSettings'
  200. );
  201.  
  202. public function init() {
  203. parent::init();
  204. $this->modelClass = 'ShopConfig';
  205. }
  206.  
  207. public function Breadcrumbs($unlinked = false) {
  208.  
  209. $request = $this->getRequest();
  210. $items = parent::Breadcrumbs($unlinked);
  211.  
  212. if ($items->count() > 1) $items->remove($items->pop());
  213.  
  214. $items->push(new ArrayData(array(
  215. 'Title' => 'Coupon Settings',
  216. 'Link' => $this->Link(Controller::join_links($this->sanitiseClassName($this->modelClass), 'Coupon'))
  217. )));
  218.  
  219. return $items;
  220. }
  221.  
  222. public function SettingsForm($request = null) {
  223. return $this->CouponSettingsForm();
  224. }
  225.  
  226. public function CouponSettings($request) {
  227.  
  228. if ($request->isAjax()) {
  229. $controller = $this;
  230. $responseNegotiator = new PjaxResponseNegotiator(
  231. array(
  232. 'CurrentForm' => function() use(&$controller) {
  233. return $controller->CouponSettingsForm()->forTemplate();
  234. },
  235. 'Content' => function() use(&$controller) {
  236. return $controller->renderWith('ShopAdminSettings_Content');
  237. },
  238. 'Breadcrumbs' => function() use (&$controller) {
  239. return $controller->renderWith('CMSBreadcrumbs');
  240. },
  241. 'default' => function() use(&$controller) {
  242. return $controller->renderWith($controller->getViewer('show'));
  243. }
  244. ),
  245. $this->response
  246. );
  247. return $responseNegotiator->respond($this->getRequest());
  248. }
  249.  
  250. return $this->renderWith('ShopAdminSettings');
  251. }
  252.  
  253. public function CouponSettingsForm() {
  254.  
  255. $shopConfig = ShopConfig::get()->First();
  256.  
  257. $fields = new FieldList(
  258. $rootTab = new TabSet('Root',
  259. $tabMain = new Tab('Coupon',
  260. GridField::create(
  261. 'Coupons',
  262. 'Coupons',
  263. $shopConfig->Coupons(),
  264. GridFieldConfig_HasManyRelationEditor::create()
  265. )
  266. )
  267. )
  268. );
  269.  
  270. $actions = new FieldList();
  271. $actions->push(FormAction::create('saveCouponSettings', _t('GridFieldDetailForm.Save', 'Save'))
  272. ->setUseButtonTag(true)
  273. ->addExtraClass('ss-ui-action-constructive')
  274. ->setAttribute('data-icon', 'add'));
  275.  
  276. $form = new Form(
  277. $this,
  278. 'EditForm',
  279. $fields,
  280. $actions
  281. );
  282.  
  283. $form->setTemplate('ShopAdminSettings_EditForm');
  284. $form->setAttribute('data-pjax-fragment', 'CurrentForm');
  285. $form->addExtraClass('cms-content cms-edit-form center ss-tabset');
  286. if($form->Fields()->hasTabset()) $form->Fields()->findOrMakeTab('Root')->setTemplate('CMSTabSet');
  287. $form->setFormAction(Controller::join_links($this->Link($this->sanitiseClassName($this->modelClass)), 'Coupon/CouponSettingsForm'));
  288.  
  289. $form->loadDataFrom($shopConfig);
  290.  
  291. return $form;
  292. }
  293.  
  294. public function saveCouponSettings($data, $form) {
  295.  
  296. //Hack for LeftAndMain::getRecord()
  297. self::$tree_class = 'ShopConfig';
  298.  
  299. $config = ShopConfig::get()->First();
  300. $form->saveInto($config);
  301. $config->write();
  302. $form->sessionMessage('Saved Coupon Settings', 'good');
  303.  
  304. $controller = $this;
  305. $responseNegotiator = new PjaxResponseNegotiator(
  306. array(
  307. 'CurrentForm' => function() use(&$controller) {
  308. //return $controller->renderWith('ShopAdminSettings_Content');
  309. return $controller->CouponSettingsForm()->forTemplate();
  310. },
  311. 'Content' => function() use(&$controller) {
  312. //return $controller->renderWith($controller->getTemplatesWithSuffix('_Content'));
  313. },
  314. 'Breadcrumbs' => function() use (&$controller) {
  315. return $controller->renderWith('CMSBreadcrumbs');
  316. },
  317. 'default' => function() use(&$controller) {
  318. return $controller->renderWith($controller->getViewer('show'));
  319. }
  320. ),
  321. $this->response
  322. );
  323. return $responseNegotiator->respond($this->getRequest());
  324. }
  325.  
  326. public function getSnippet() {
  327.  
  328. if (!$member = Member::currentUser()) return false;
  329. if (!Permission::check('CMS_ACCESS_' . get_class($this), 'any', $member)) return false;
  330.  
  331. return $this->customise(array(
  332. 'Title' => 'Coupon Management',
  333. 'Help' => 'Create coupons',
  334. 'Link' => Controller::join_links($this->Link('ShopConfig'), 'Coupon'),
  335. 'LinkTitle' => 'Edit coupons'
  336. ))->renderWith('ShopAdmin_Snippet');
  337. }
  338.  
  339. }
  340.  
  341. class Coupon_OrderExtension extends DataExtension {
  342.  
  343. /**
  344. * Attach {@link Coupon}s to {@link SiteConfig}.
  345. *
  346. * @see DataObjectDecorator::extraStatics()
  347. */
  348. public static $db = array(
  349. 'CouponCode' => 'Varchar'
  350. );
  351. }
  352.  
  353. class Coupon_CheckoutFormExtension extends Extension {
  354.  
  355. public function getCouponFields() {
  356.  
  357. $fields = new FieldList();
  358. $fields->push(Coupon_Field::create('CouponCode', _t('Coupon.COUPON_CODE_LABEL', 'Enter your coupon code'))
  359. ->setForm($this->owner)
  360. );
  361. return $fields;
  362. }
  363. }
  364.  
  365. class Coupon_Field extends TextField {
  366.  
  367. public function FieldHolder($properties = array()) {
  368.  
  369. Requirements::javascript(THIRDPARTY_DIR . '/jquery/jquery.js');
  370. Requirements::javascript('swipestripe-coupon/javascript/CouponModifierField.js');
  371. return $this->renderWith('CouponField');
  372. }
  373. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement