· 8 years ago · Apr 30, 2018, 01:16 PM
1<?php
2
3class StorepageController extends FrontController
4{
5
6 public function initContent()
7 {
8 parent::initContent();
9
10 $this->setTemplate(_PS_MODULE_DIR_.'modulename/views/templates/front/storepage.tpl');
11 }
12
13/customstores
14 /customstores.php
15 /config.xml
16 /override
17 /controllers
18 /front
19 StoresController.php
20 /views
21 /templates
22 /front
23 /stores.tpl
24
25/customstores
26 /customstores.php
27 /config.xml
28 /override
29 /controllers
30 /front
31 StoresController.php
32 /views
33 /templates
34 /front
35 /stores.tpl
36 /mystore.tpl
37 /controllers
38 /front
39 /mystore.php
40 /classes
41 /CustomStore.php
42
43<?php
44
45include_once(_PS_MODULE_DIR_ . 'customstores/classes/CustomStore.php');
46
47/**
48 * the name of the class should be [ModuleName][ControllerName]ModuleFrontController
49 */
50class CustomStoresMyStoreModuleFrontController extends ModuleFrontController
51{
52 public function __construct()
53 {
54 parent::__construct();
55 $this->context = Context::getContext();
56 $this->ssl = false;
57 }
58
59 /**
60 * @see FrontController::initContent()
61 */
62 public function initContent()
63 {
64 parent::initContent();
65
66 // We will consider that your controller possesses a form and an ajax call
67
68 if (Tools::isSubmit('storeAjax'))
69 {
70 return $this->displayAjax();
71 }
72
73 if (Tools::isSubmit('storeForm'))
74 {
75 return $this->processStoreForm();
76 }
77
78 $this->getContent();
79 }
80
81 public function getContent($assign = true)
82 {
83 $content = array('store' => null, 'errors' => $errors);
84
85 if (Tools::getValue('id_store') !== false)
86 {
87 $content['store'] = new CustomStore((int) Tools::getValue('id_store'));
88
89 if (! Validate::isLoadedObject($content['store']))
90 {
91 $content['store'] = null;
92 $content['errors'][] = "Can't load the store";
93 }
94 }
95 else
96 {
97 $content['errors'][] = "Not a valid id_store";
98 }
99
100
101 if ($assign)
102 {
103 $this->context->smarty->assign($content);
104 }
105 else
106 {
107 return $content;
108 }
109 }
110
111 public function displayAjax()
112 {
113 return Tools::jsonEncode($this->getContent(false));
114 }
115
116 public function processStoreForm()
117 {
118 // Here you get your values from the controller form
119 $like = Tools::getValue('like');
120 $id_store = (int) Tools::getValue('id_store');
121 // And process it...
122 $this->context->smarty->assign(array(
123 'formResult' = CustomStore::like($id_store, $like)
124 ));
125
126 // And finally display the page
127 $this->getcontent();
128 }
129}
130
131<?php
132
133class CustomStore extends ObjectModel
134{
135 public $id_custom_store;
136
137 public $name;
138
139 public $nb_likes;
140
141 /**
142 * @see ObjectModel::$definition
143 */
144 public static $definition = array(
145 'table' => 'customstores_store',
146 'primary' => 'id_custom_store',
147 'fields' => array(
148 'name' => array('type' => self::TYPE_STRING, 'validate' => 'isGenericName', 'required' => true, 'size' => 128),
149 'nb_likes' => array('type' => self::TYPE_INT, 'validate' => 'isUnsignedId', 'required' => true),
150 ),
151 );
152
153 public static function like($id_store, $like)
154 {
155 $store = new CustomStore($id_store);
156
157 if (! Validate::isLoadedObject($store))
158 {
159 return false;
160 }
161
162 if (! ($like == 1 || $like == -1))
163 {
164 return false;
165 }
166
167 $store->nb_likes + (int) $like;
168 $store->save();
169 }
170}
171
172DB::getInstance()->execute(
173 "CREATE TABLE IF NOT EXISTS `" . _DB_PREFIX_ . "customstores_store` (
174 `id_custom_store` varchar(16) NOT NULL,
175 `name` varchar(128) NOT NULL,
176 `nb_likes` int(10) unsigned NOT NULL DEFAULT '0',
177 PRIMARY KEY (`id_custom_store`)
178 ) ENGINE=" . _MYSQL_ENGINE_ . " DEFAULT CHARSET=utf8;"
179);
180
181public function initContent()
182
183{
184
185 parent::initContent();
186
187 $this->setTemplate('module:dropship/views/templates/front/details.tpl');
188
189}