chcreateur.php 17 KB

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