chcreateur.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636
  1. <?php
  2. if (!defined('_PS_VERSION_')) {
  3. exit;
  4. }
  5. include_once _PS_MODULE_DIR_.'chcreateur/config/define.inc.php';
  6. include_once _PS_MODULE_DIR_.'chcreateur/classes/chcreaimagetype.php';
  7. include_once _PS_MODULE_DIR_.'chcreateur/classes/chcreapost.php';
  8. use PrestaShop\PrestaShop\Core\Module\WidgetInterface;
  9. class Chcreateur extends Module implements WidgetInterface
  10. {
  11. public $dbfiles = '/db/dbfiles.php';
  12. public function __construct()
  13. {
  14. $this->name = 'chcreateur';
  15. $this->tab = 'front_office_features';
  16. $this->version = '1.0.0';
  17. $this->author = 'Cedric Hansen';
  18. $this->need_instance = 0;
  19. $this->ps_versions_compliancy = [
  20. 'min' => '1.6.0',
  21. 'max' => '8.4',
  22. ];
  23. $this->bootstrap = true;
  24. parent::__construct();
  25. $this->displayName = $this->l('CH createur');
  26. $this->description = $this->l("Presentation d'une createur ");
  27. $this->confirmUninstall = $this->l('Are you sure you want to uninstall?');
  28. }
  29. /**
  30. * @return [type]
  31. */
  32. public function install()
  33. {
  34. if (Shop::isFeatureActive()) {
  35. Shop::setContext(Shop::CONTEXT_ALL);
  36. }
  37. return (
  38. parent::install()
  39. //
  40. //&& $this->registerHook('displayBlockPosition2')
  41. && $this->registerHook('displayHeader')
  42. && $this->registerHook('ModuleRoutes')
  43. && $this->registerHook('ActionFrontControllerSetVariables')
  44. && $this->Register_SQL()
  45. && $this->_installTab()
  46. && Configuration::updateValue('CHCREATEUR_TITLE', 'Nos createurs')
  47. && Configuration::updateValue('CHCREATEUR_URL', 'nos-createurs')
  48. && Configuration::updateValue('CHCREATEUR_DESCRIPTION', 'Une description', true)
  49. );
  50. }
  51. /**
  52. * @return [type]
  53. */
  54. public function Register_SQL()
  55. {
  56. $querys = array();
  57. if (file_exists(dirname(__FILE__).$this->dbfiles)) {
  58. require_once(dirname(__FILE__).$this->dbfiles);
  59. if (isset($querys) && !empty($querys)) {
  60. foreach ($querys as $query) {
  61. if (!Db::getInstance()->Execute($query)) {
  62. return false;
  63. }
  64. }
  65. }
  66. }
  67. return true;
  68. }
  69. /**
  70. * Installation du controller dans la backoffice
  71. * @return boolean
  72. */
  73. protected function _installTab()
  74. {
  75. $tab = new Tab();
  76. $tab->class_name = 'AdminChcreateur';
  77. $tab->module = $this->name;
  78. $tab->id_parent = (int)Tab::getIdFromClassName('IMPROVE');
  79. $tab->icon = 'chat';
  80. $languages = Language::getLanguages();
  81. foreach ($languages as $lang) {
  82. $tab->name[$lang['id_lang']] = $this->l('Notre createur');
  83. }
  84. try {
  85. $tab->save();
  86. } catch (Exception $e) {
  87. echo $e->getMessage();
  88. return false;
  89. }
  90. return true;
  91. }
  92. /**
  93. * @return [type]
  94. */
  95. public function uninstall()
  96. {
  97. return (
  98. parent::uninstall()
  99. //&& $this->UnRegister_SQL()
  100. //&& Configuration::deleteByName('CHCREATEUR_NAME')
  101. );
  102. }
  103. /**
  104. * @return [type]
  105. */
  106. public function UnRegister_SQL()
  107. {
  108. $querys_u = array();
  109. if (file_exists(dirname(__FILE__).$this->dbfiles)) {
  110. require_once(dirname(__FILE__).$this->dbfiles);
  111. if (isset($querys_u) && !empty($querys_u)) {
  112. foreach ($querys_u as $query_u) {
  113. if (!Db::getInstance()->Execute($query_u)) {
  114. return false;
  115. }
  116. }
  117. }
  118. }
  119. return true;
  120. }
  121. /**
  122. * @param mixed $params
  123. *
  124. * @return $array
  125. */
  126. public function hookModuleRoutes($params)
  127. {
  128. $chcreateurroutes = array(
  129. 'chcreateur-archive-module' => [
  130. 'controller' => 'archive',
  131. 'rule' => Configuration::get('CHCREATEUR_URL'),
  132. 'keywords' => [],
  133. 'params' => [
  134. 'fc' => 'module',
  135. 'module' => 'chcreateur',
  136. ]
  137. ],
  138. 'chcreateur-single-module' => [
  139. 'controller' => 'single',
  140. 'rule' => Configuration::get('CHCREATEUR_URL').'/{id}_{rewrite}',
  141. 'keywords' => [
  142. 'id' => array('regexp' => '[0-9]+','param' => 'id'),
  143. 'rewrite' => array('regexp' => '[_a-zA-Z0-9-\pL]*','param' => 'rewrite'),
  144. ],
  145. 'params' => [
  146. 'fc' => 'module',
  147. 'module' => 'chcreateur',
  148. ]
  149. ]
  150. );
  151. return $chcreateurroutes;
  152. }
  153. public function postProcess()
  154. {
  155. $output = '';
  156. //print_r($_POST);
  157. //print_r($_GET);
  158. // this part is executed only when the form is submitted
  159. if (Tools::isSubmit('submit' . $this->name)) {
  160. // retrieve the value set by the user
  161. $config_TITLE = (string) Tools::getValue('CHCREATEUR_TITLE');
  162. $config_URL = (string) Tools::getValue('CHCREATEUR_URL');
  163. $config_DESCRIPTION = (string) Tools::getValue('CHCREATEUR_DESCRIPTION');
  164. // check that the value is valid
  165. if (empty($config_TITLE) || !Validate::isGenericName($config_TITLE)) {
  166. // invalid value, show an error
  167. $output = $this->displayError($this->l('Invalid Configuration value'));
  168. } else {
  169. // value is ok, update it and display a confirmation message
  170. Configuration::updateValue('CHCREATEUR_TITLE', $config_TITLE);
  171. Configuration::updateValue('CHCREATEUR_URL', $config_URL);
  172. Configuration::updateValue('CHCREATEUR_DESCRIPTION', $config_DESCRIPTION, true);
  173. $output = $this->displayConfirmation($this->l('Settings updated'));
  174. }
  175. }
  176. if (Tools::isSubmit('updatechcrea_image_type') || Tools::getValue('addnew')) {
  177. return $output . $this->renderImageForm();
  178. }
  179. if (Tools::isSubmit('SubmitImageForm')) {
  180. if (Tools::getValue('id_image_type')) {
  181. $obj = new chcreaimagetypeclass(Tools::getValue('id_image_type'));
  182. } else {
  183. $obj = new chcreaimagetypeclass();
  184. }
  185. foreach (chcreaimagetypeclass::$definition['fields'] as $key => $value) {
  186. $obj->$key = Tools::getValue($key);
  187. }
  188. if (Tools::getValue('id_image_type')) {
  189. if ($obj->update()) {
  190. return $this->displayConfirmation($this->l('Settings Updated'));
  191. } else {
  192. return $this->displayError($this->l('Unable to update settings'));
  193. }
  194. } else {
  195. if ($obj->add()) {
  196. return $this->displayConfirmation($this->l('Settings Created'));
  197. } else {
  198. return $this->displayError($this->l('Unable to create settings'));
  199. }
  200. }
  201. }
  202. }
  203. /**
  204. * @return string
  205. * @throws PrestaShopDatabaseException
  206. * @throws PrestaShopException
  207. */
  208. public function getContent()
  209. {
  210. //$lien = Context::getContext()->link->getModuleLink('chcreateur', 'archive', array('idPayment' => 1337));
  211. //print($lien);
  212. $html = $this->postProcess();
  213. $html .= $this->displayForm();
  214. $html .= $this->displayList();
  215. return $html;
  216. }
  217. /**
  218. * Get configuration form
  219. * @return string
  220. * @throws PrestaShopDatabaseException
  221. * @throws PrestaShopException
  222. */
  223. protected function renderImageForm()
  224. {
  225. $fields_form = [
  226. 'form' => [
  227. 'description' => $this->l('Format des images generées pour les templates'),
  228. 'legend' => [
  229. 'title' => $this->l('Configure Image'),
  230. 'icon' => 'icon-cogs'
  231. ],
  232. 'input' => [
  233. [
  234. 'type' => 'hidden',
  235. 'label' => $this->l('Enable for id'),
  236. 'name' => 'id_image_type',
  237. 'required' => true,
  238. ],
  239. [
  240. 'type' => 'hidden',
  241. 'label' => $this->l('Enable for shop'),
  242. 'name' => 'id_shop',
  243. 'required' => true,
  244. ],
  245. [
  246. 'type' => 'text',
  247. 'label' => $this->l('Name'),
  248. 'name' => 'name',
  249. 'required' => false,
  250. ],
  251. [
  252. 'type' => 'text',
  253. 'label' => $this->l('Width of picture'),
  254. 'name' => 'width',
  255. 'required' => true,
  256. ],
  257. [
  258. 'type' => 'text',
  259. 'label' => $this->l('Height of picture'),
  260. 'name' => 'height',
  261. 'required' => true,
  262. ],
  263. [
  264. 'type' => 'switch',
  265. 'label' => $this->l('Status'),
  266. 'name' => 'active',
  267. 'required' => false,
  268. 'class' => 't',
  269. 'is_bool' => true,
  270. 'values' => array(
  271. array(
  272. 'id' => 'active',
  273. 'value' => 1,
  274. 'label' => $this->l('Enabled')
  275. ),
  276. array(
  277. 'id' => 'active',
  278. 'value' => 0,
  279. 'label' => $this->l('Disabled')
  280. )
  281. )
  282. ],
  283. ],
  284. 'submit' => [
  285. 'title' => $this->l('Save'),
  286. 'icon' => 'icon-save',
  287. 'class' => 'button btn btn-default pull-right',
  288. ]
  289. ],
  290. ];
  291. $helper = new HelperForm();
  292. //$helper->show_toolbar = true;
  293. $helper->show_cancel_button = true;
  294. $lang = new Language((int)Configuration::get('PS_LANG_DEFAULT'));
  295. $helper->default_form_language = $lang->id;
  296. $helper->allow_employee_form_lang = Configuration::get('PS_BO_ALLOW_EMPLOYEE_FORM_LANG') ? Configuration::get('PS_BO_ALLOW_EMPLOYEE_FORM_LANG') : 0;
  297. $helper->identifier = "image_type";
  298. $helper->submit_action = 'SubmitImageForm';
  299. $helper->currentIndex = $this->context->link->getAdminLink('AdminModules', false) . '&configure=' . $this->name . '&tab_module=' . $this->tab . '&module_name=' . $this->name;
  300. $helper->token = Tools::getAdminTokenLite('AdminModules');
  301. $helper->tpl_vars = array(
  302. 'fields_value' => $this->getAddFieldsValues(),
  303. 'languages' => $this->context->controller->getLanguages(),
  304. 'id_language' => $this->context->language->id
  305. );
  306. return $helper->generateForm([$fields_form]);
  307. }
  308. public function getAddFieldsValues()
  309. {
  310. $fields = array();
  311. $id_image_type = (int) Tools::getValue('id_image_type');
  312. if ($id_image_type == 0) {
  313. $obj = new Chcreaimagetypeclass();
  314. print("id_image_type:".$obj->id);
  315. print("</br>");
  316. } else {
  317. $obj = new Chcreaimagetypeclass($id_image_type);
  318. }
  319. $fields = [
  320. 'id_image_type' => Tools::getValue('id_image_type', $obj->id),
  321. 'name' => Tools::getValue('name', $obj->name),
  322. 'width' => Tools::getValue('width', $obj->width),
  323. 'height' => Tools::getValue('height', $obj->height),
  324. 'active' => Tools::getValue('active', $obj->active),
  325. 'id_shop' => Tools::getValue('id_shop', $obj->id_shop),
  326. ];
  327. return $fields;
  328. }
  329. public function displayForm()
  330. {
  331. // Init Fields form array
  332. $form = [
  333. 'form' => [
  334. 'legend' => [
  335. 'title' => $this->l('Settings'),
  336. ],
  337. 'input' => [
  338. [
  339. 'type' => 'text',
  340. 'label' => $this->l('Title'),
  341. 'name' => 'CHCREATEUR_TITLE',
  342. 'size' => 128,
  343. 'required' => true,
  344. ],
  345. [
  346. 'type' => 'textarea',
  347. 'label' => $this->l('Description'),
  348. 'name' => 'CHCREATEUR_DESCRIPTION',
  349. 'size' => 800,
  350. 'required' => true,
  351. ],
  352. [
  353. 'type' => 'text',
  354. 'label' => $this->l('Base URL'),
  355. 'name' => 'CHCREATEUR_URL',
  356. 'size' => 128,
  357. 'required' => true,
  358. ],
  359. ],
  360. 'submit' => [
  361. 'title' => $this->l('Save'),
  362. 'class' => 'btn btn-default pull-right',
  363. ],
  364. ],
  365. ];
  366. $helper = new HelperForm();
  367. // Module, token and currentIndex
  368. $helper->table = $this->table;
  369. $helper->name_controller = $this->name;
  370. $helper->token = Tools::getAdminTokenLite('AdminModules');
  371. $helper->currentIndex = AdminController::$currentIndex . '&' . http_build_query(['configure' => $this->name]);
  372. $helper->submit_action = 'submit' . $this->name;
  373. // Default language
  374. $helper->default_form_language = (int) Configuration::get('PS_LANG_DEFAULT');
  375. // Load current value into the form
  376. $helper->fields_value['CHCREATEUR_TITLE'] = Tools::getValue('CHCREATEUR_TITLE', Configuration::get('CHCREATEUR_TITLE'));
  377. $helper->fields_value['CHCREATEUR_DESCRIPTION'] = Tools::getValue('CHCREATEUR_DESCRIPTION', Configuration::get('CHCREATEUR_DESCRIPTION'));
  378. $helper->fields_value['CHCREATEUR_URL'] = Tools::getValue('CHCREATEUR_URL', Configuration::get('CHCREATEUR_URL'));
  379. return $helper->generateForm([$form]);
  380. }
  381. public function displayList()
  382. {
  383. $fields_list = [
  384. 'id_image_type' => [
  385. 'title' => $this->l('Id'),
  386. //'width' => 140,
  387. 'type' => 'text',
  388. ],
  389. 'name' => [
  390. 'title' => $this->l('Name'),
  391. //'width' => '100%',
  392. 'type' => 'texte',
  393. ],
  394. 'width' => [
  395. 'title' => $this->l('Width'),
  396. //'width' => 140,
  397. 'type' => 'texte',
  398. ],
  399. 'height' => [
  400. 'title' => $this->l('height'),
  401. //'width' => 140,
  402. 'type' => 'texte',
  403. ],
  404. 'active' => [
  405. 'title' => $this->l('active'),
  406. //'width' => 30,
  407. 'type' => 'bool',
  408. 'active' => 'status',
  409. 'icon' => [
  410. 0 => 'disabled.gif',
  411. 1 => 'enabled.gif',
  412. 'default' => 'disabled.gif'
  413. ],
  414. ],
  415. ];
  416. $helper = new HelperList();
  417. $helper->shopLinkType = '';
  418. $helper->simple_header = false;
  419. // Actions to be displayed in the "Actions" column
  420. $helper->actions = array('edit', 'delete');
  421. $helper->show_toolbar = true;
  422. $helper->identifier = 'id_image_type';
  423. $helper->toolbar_btn['new'] = [
  424. 'href' => Context::getContext()->link->getAdminLink('AdminModules').'&addnew=true&configure='.$this->name,
  425. 'desc' => $this->l('Add new')
  426. ];
  427. $helper->title = 'List Image Type';
  428. $helper->table = Chcreaimagetypeclass::$definition['table'];
  429. $helper->className = Chcreaimagetypeclass::class; //Classe de l'objet
  430. $helper->token = Tools::getAdminTokenLite('AdminModules');
  431. //$helper->currentIndex = $this->context->link->getAdminLink('AdminModules', false) . '&configure=' . $this->name . '&tab_module=' . $this->tab . '&module_name=' . $this->name;
  432. //AdminController::$currentIndex.'&configure='.$this->name;
  433. $helper->currentIndex = AdminController::$currentIndex.'&configure='.$this->name;
  434. return $helper->generateList(Chcreaimagetypeclass::GetAllImageTypes(), $fields_list);
  435. }
  436. public function initToolbar()
  437. {
  438. parent::initToolbar();
  439. $this->toolbar_btn['new'] = array(
  440. 'href' => $this->context->link->getAdminLink('some link'),
  441. 'desc' => $this->l('some description')
  442. );
  443. }
  444. /*
  445. public function renderList()
  446. {
  447. $this->addRowAction('edit');
  448. $this->addRowAction('delete');
  449. return parent::renderList();
  450. }
  451. */
  452. /**
  453. * @param null $hookName
  454. * @param array $configuration
  455. *
  456. * @return [type]
  457. */
  458. public function renderWidget($hookName = null, array $configuration = [])
  459. {
  460. if ($hookName == 'displayleft') {
  461. $this->smarty->assign($this->getWidgetVariables($hookName, $configuration));
  462. return $this->fetch('module:'.$this->name.'/views/templates/front/left-list.tpl');
  463. } elseif ( $hookName == 'displayBlockPosition2' ) {
  464. $this->smarty->assign($this->getWidgetVariables($hookName, $configuration));
  465. //print_r($this->getWidgetVariables($hookName, $configuration));
  466. //print($this->name);
  467. //return "voila";
  468. return $this->fetch('module:'.$this->name.'/views/templates/front/vignette.tpl');
  469. } else {
  470. $this->smarty->assign($this->getWidgetVariables($hookName, $configuration));
  471. //print_r($this->getWidgetVariables($hookName, $configuration));
  472. //print($this->name);
  473. //return "voila";
  474. return $this->fetch('module:'.$this->name.'/views/templates/front/'.$this->name.'.tpl');
  475. }
  476. }
  477. public function getWidgetVariables($hookName = null, array $configuration = [])
  478. {
  479. $id_lang = (int)$this->context->language->id;
  480. //$xipbdp_title = Configuration::get(self::$xipblogshortname.'xipbdp_title', $id_lang);
  481. // $xipbdp_subtext = Configuration::get(self::$xipblogshortname.'xipbdp_subtext', $id_lang);
  482. // $xipbdp_postcount = Configuration::get(self::$xipblogshortname.'xipbdp_postcount');
  483. //Media::addJsDef(array('xipbdp_numcolumn'=>$xipbdp_numcolumn));
  484. $chcreaposts = array();
  485. $chcreaposts = Chcreapost::getAllSelections(6);
  486. // print('<Pre>'.print_r($chcreaposts,true).'</pre>');
  487. // exit();
  488. return array(
  489. 'chcrea_title' => Configuration::get('CHCREATEUR_TITLE'),
  490. 'chcrea_desc' => Configuration::get('CHCREATEUR_DESCRIPTION'),
  491. 'hookName' => $hookName,
  492. 'chposts' => $chcreaposts,
  493. 'vignettes_nbr' => count($chcreaposts),
  494. );
  495. }
  496. public static function getBaseLink($id_shop = null, $ssl = null, $relative_protocol = false)
  497. {
  498. static $force_ssl = null;
  499. if ($ssl === null) {
  500. if ($force_ssl === null) {
  501. $force_ssl = (Configuration::get('PS_SSL_ENABLED') && Configuration::get('PS_SSL_ENABLED_EVERYWHERE'));
  502. }
  503. $ssl = $force_ssl;
  504. }
  505. if (Configuration::get('PS_MULTISHOP_FEATURE_ACTIVE') && $id_shop !== null) {
  506. $shop = new Shop($id_shop);
  507. } else {
  508. $shop = Context::getContext()->shop;
  509. }
  510. if ($relative_protocol) {
  511. $base = '//'.($ssl ? $shop->domain_ssl : $shop->domain);
  512. } else {
  513. $base = (($ssl) ? 'https://'.$shop->domain_ssl : 'http://'.$shop->domain);
  514. }
  515. return $base.$shop->getBaseURI();
  516. }
  517. /*
  518. public static function XipBlogPostLink($params = array())
  519. {
  520. $url_format = Configuration::get(self::$xipblogshortname."url_format");
  521. if(isset($params['id']) && !isset($params['rewrite'])) {
  522. $params['rewrite'] = xippostsclass::get_the_rewrite($params['id']);
  523. }
  524. if(!isset($params['id']) && isset($params['rewrite'])) {
  525. $params['id'] = xippostsclass::get_the_id($params['rewrite']);
  526. }
  527. if(!isset($params['page_type'])) {
  528. $params['page_type'] = 'post';
  529. }
  530. if($url_format == 'preid_seo_url') {
  531. $rule = 'xipblog-single-module';
  532. return self::XipBlogLink($rule, $params);
  533. } elseif ($url_format == 'postid_seo_url') {
  534. $rule = 'xipblog-single-aftrid-module';
  535. return self::XipBlogLink($rule, $params);
  536. } elseif ($url_format == 'wthotid_seo_url') {
  537. $rule = 'xipblog-single-wid-module';
  538. return self::XipBlogLink($rule, $params);
  539. } elseif ($url_format == 'default_seo_url') {
  540. return self::GetLinkObject()->getModuleLink("xipblog", "single", $params);
  541. } else {
  542. $rule = 'xipblog-single-module';
  543. }
  544. }
  545. */
  546. public function hookActionFrontControllerSetVariables()
  547. {
  548. $posts = Chcreapost::getAllSelections(5);
  549. $links = [];
  550. foreach($posts as $post ) {
  551. $links[] = [
  552. 'id' => 'chcreapost-' . $post['id_chcreaposts'],
  553. 'label' => $post['post_title'],
  554. 'url' => $post['link']
  555. ];
  556. }
  557. return [
  558. 'title' => Configuration::get('CHCREATEUR_TITLE'),
  559. 'links' => $links,
  560. ];
  561. }
  562. }