chcreateur.php 17 KB

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