AdminChcreateur.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511
  1. <?php
  2. include_once _PS_MODULE_DIR_.'chcreateur/classes/chcreapost.php';
  3. include_once _PS_MODULE_DIR_.'chcreateur/classes/chcreaimages.php';
  4. class AdminChcreateurController extends ModuleAdminController
  5. {
  6. public function __construct()
  7. {
  8. $this->table = 'chcreaposts';
  9. $this->className = 'Chcreapost';
  10. $this->name = 'AdminChcreateur';
  11. $this->lang = true;
  12. $this->deleted = false;
  13. $this->module = 'chcreateur';
  14. $this->position_identifier = 'id_chcreaposts';
  15. $this->allow_export = false;
  16. $this->bootstrap = true;
  17. if (Shop::isFeatureActive()) {
  18. Shop::addTableAssociation($this->table, ['type' => 'shop']);
  19. }
  20. parent::__construct();
  21. $this->fields_list = [
  22. 'id_chcreaposts' => [
  23. 'title' => $this->l('Id'),
  24. 'width' => 100,
  25. 'type' => 'text',
  26. ],
  27. 'id_temp' => [
  28. 'title' => 'Image',
  29. 'align' => 'center',
  30. 'callback' => 'getImage',
  31. ],
  32. 'post_title' => [
  33. 'title' => $this->l('Post Title'),
  34. 'width' => 60,
  35. 'type' => 'text',
  36. ],
  37. 'post_excerpt' => [
  38. 'title' => $this->l('Excerpt'),
  39. 'width' => 220,
  40. 'type' => 'text',
  41. ],
  42. 'link_rewrite' => [
  43. 'title' => $this->l('URL Rewrite'),
  44. 'width' => 220,
  45. 'type' => 'text',
  46. ],
  47. 'post_format' => [
  48. 'title' => $this->l('Format'),
  49. 'width' => 20,
  50. 'type' => 'text',
  51. ],
  52. 'position' => [
  53. 'title' => $this->l('Position'),
  54. 'align' => 'left',
  55. 'filter_key' => 'a!position',
  56. 'position' => 'position',
  57. // 'orderby' => false
  58. ],
  59. /*
  60. 'post_date' => [
  61. 'title' => $this->l('Date'),
  62. 'align' => 'text-right',
  63. 'type' => 'datetime',
  64. 'filter_key' => 'post_date',
  65. ],
  66. */
  67. 'active' => [
  68. 'title' => $this->l('Status'),
  69. 'width' => 60,
  70. 'align' => 'center',
  71. 'active' => 'status',
  72. 'type' => 'bool',
  73. 'orderby' => false,
  74. ],
  75. ];
  76. $this->bulk_actions = [
  77. 'delete' => [
  78. 'text' => $this->l('Delete selected'),
  79. 'icon' => 'icon-trash',
  80. 'confirm' => $this->l('Delete selected items?'),
  81. ],
  82. ];
  83. parent::__construct();
  84. }
  85. public function getImage($id, $col)
  86. {
  87. $Chcreaimages = new Chcreaimages();
  88. $imgs = $Chcreaimages->getUrls($id);
  89. if ( count($imgs) < 1 ) {
  90. $html = '';
  91. } else {
  92. $html = '<a href="#"><img src="'.$imgs[0].'" height="50px" ></a>';
  93. }
  94. return $html;
  95. }
  96. public function __initToolbar()
  97. {
  98. parent::initToolbar();
  99. /*
  100. $this->toolbar_btn['custom'] = array(
  101. 'href' => '',
  102. 'desc' => 'Un nouveau'
  103. );
  104. */
  105. // print_r($this->toolbar_btn);exit();
  106. }
  107. public function init()
  108. {
  109. parent::init();
  110. $this->_join = 'LEFT JOIN '._DB_PREFIX_.'chcreaposts_shop sbp ON a.id_chcreaposts=sbp.id_chcreaposts && sbp.id_shop IN('.implode(',', Shop::getContextListShopID()).')';
  111. $this->_select = 'a.`id_chcreaposts` as `id_temp`';
  112. $this->_orderBy = 'a.position';
  113. }
  114. public function setMedia($isNewTheme = false)
  115. {
  116. parent::setMedia();
  117. $this->addJqueryUi('ui.widget');
  118. $this->addJqueryPlugin('tagify');
  119. $this->addJqueryPlugin('select2');
  120. //$this->context->controller->addJS(chcrea_js_dir.'validator.min.js');
  121. }
  122. public function renderForm()
  123. {
  124. $id_chcreaposts = Tools::getValue('id_chcreaposts');
  125. $audio_temp = '';
  126. $video_temp = '';
  127. $gallery_temp = [];
  128. $gallery_temp_str = '';
  129. $post_img_temp = '';
  130. $prod_temp = '';
  131. $gallery_url = chcrea_img_uri;
  132. if (isset($id_chcreaposts) && !empty($id_chcreaposts)) {
  133. $Chcreapost = new Chcreapost($id_chcreaposts);
  134. if (isset($Chcreapost->audio) && !empty($Chcreapost->audio)) {
  135. $audio_temp = @explode(',', $Chcreapost->audio);
  136. }
  137. if (isset($Chcreapost->related_products) && !empty($Chcreapost->related_products)) {
  138. $prod_temp = @explode(',', $Chcreapost->related_products);
  139. }
  140. if (isset($Chcreapost->video) && !empty($Chcreapost->video)) {
  141. //$video_temp = @explode(',', $Chcreapost->video);
  142. $video_temp = $Chcreapost->video;
  143. }
  144. // if (isset($Chcreapost->gallery) && !empty($Chcreapost->gallery)) {
  145. // $gallery_temp = @explode(',', $Chcreapost->gallery);
  146. // $gallery_temp_str = $Chcreapost->gallery;
  147. // }
  148. // if (isset($Chcreapost->post_img) && !empty($Chcreapost->post_img)) {
  149. // $post_img_temp = '<img src="'.chcrea_img_uri.$Chcreapost->post_img.'" height="110" width="auto"><br>';
  150. // }
  151. $Chcreaimages = new Chcreaimages();
  152. $imgs = $Chcreaimages->getUrls($id_chcreaposts);
  153. if ( is_array($imgs) ){
  154. foreach($imgs as $img) {
  155. $post_img_temp .= '<img src="'.$img.'" height="110" width="auto"><br>';
  156. }
  157. }
  158. $gallery_url = chcrea_img_uri.'/'. $id_chcreaposts .'/';
  159. $Chcreaimages = new Chcreaimages();
  160. $imgs = $Chcreaimages->getGallery($id_chcreaposts);
  161. foreach($imgs as $value) {
  162. $gallery_temp[$value['id_images']] = $value['name'];
  163. }
  164. // print("<pre>".print_r($gallery_temp,true)."</pre>");
  165. // exit();
  166. }
  167. $this->fields_form = [
  168. 'description' => "Le format des images : 1200 x 630",
  169. 'legend' => [
  170. 'title' => $this->l('Add New Post'),
  171. ],
  172. 'input' => [
  173. /*
  174. [
  175. 'type' => 'radio',
  176. 'label' => $this->l('Post Format'),
  177. 'name' => 'post_format',
  178. 'required' => false,
  179. 'class' => 't',
  180. 'is_bool' => true,
  181. 'values' => [
  182. [
  183. 'id' => 'standrad',
  184. 'value' => 'standrad',
  185. 'label' => $this->l('Standard'),
  186. ],
  187. [
  188. 'id' => 'gallery',
  189. 'value' => 'gallery',
  190. 'label' => $this->l('Gallery'),
  191. ],
  192. [
  193. 'id' => 'video',
  194. 'value' => 'video',
  195. 'label' => $this->l('Video'),
  196. ],
  197. [
  198. 'id' => 'audio',
  199. 'value' => 'audio',
  200. 'label' => $this->l('Audio'),
  201. ],
  202. ],
  203. ],
  204. */
  205. [
  206. 'type' => 'text',
  207. 'label' => $this->l('Post Title'),
  208. 'name' => 'post_title',
  209. 'id' => 'name', // for copyMeta2friendlyURL compatibility
  210. 'class' => 'copyMeta2friendlyURL',
  211. 'desc' => $this->l('Enter Your Blog Post Title'),
  212. 'lang' => true,
  213. ],
  214. [
  215. 'type' => 'textarea',
  216. 'label' => $this->l('Post Excerpt'),
  217. 'name' => 'post_excerpt',
  218. 'desc' => $this->l('Enter Your Blog Post Excerpt'),
  219. 'lang' => true,
  220. ],
  221. [
  222. 'type' => 'textarea',
  223. 'label' => $this->l('Post Content'),
  224. 'name' => 'post_content',
  225. 'desc' => $this->l('Enter Your Blog Post Content'),
  226. 'lang' => true,
  227. 'autoload_rte' => true,
  228. ],
  229. [
  230. 'type' => 'text',
  231. 'label' => $this->l('Meta Title'),
  232. 'name' => 'meta_title',
  233. 'desc' => $this->l('Enter Your Post Meta Title for SEO'),
  234. 'lang' => true,
  235. ],
  236. [
  237. 'type' => 'textarea',
  238. 'label' => $this->l('Meta Description'),
  239. 'name' => 'meta_description',
  240. 'desc' => $this->l('Enter Your Post Meta Description for SEO'),
  241. 'lang' => true,
  242. ],
  243. [
  244. 'type' => 'tags',
  245. 'label' => $this->l('Meta Keyword'),
  246. 'name' => 'meta_keyword',
  247. 'desc' => $this->l('Enter Your Post Meta Keyword for SEO. Separate by comma(,)'),
  248. 'lang' => true,
  249. ],
  250. [
  251. 'type' => 'file',
  252. 'label' => "Meta Image </br> 1200 x 630",
  253. 'name' => 'post_img',
  254. 'desc' => $post_img_temp.$this->l('Format Please Upload Feature Image From Your Computer.'),
  255. ],
  256. [
  257. 'type' => 'select_multiple',
  258. // 'type' => 'select',
  259. 'label' => $this->l('Select Related Products'),
  260. 'name' => 'related_products_temp',
  261. 'defaults' => $prod_temp,
  262. 'options' => [
  263. 'query' => self::getallproducts(),
  264. 'id' => 'id',
  265. 'name' => 'name',
  266. 'reference' => 'reference',
  267. ],
  268. ],
  269. [
  270. 'type' => 'gallery',
  271. 'label' => "Image dans l'article </br> 1200 x 630",
  272. 'name' => 'gallery_temp',
  273. 'defaults' => $gallery_temp,
  274. 'defaults_str' => $gallery_temp_str,
  275. 'url' => $gallery_url,
  276. 'desc' => $this->l('Please give Image url for Gallery post. Separate by comma(,). You can add Any Kind of Image URL.'),
  277. ],
  278. /*
  279. [
  280. // 'type' => 'textarea',
  281. 'type' => 'textarea',
  282. 'label' => $this->l('Video'),
  283. 'name' => 'video_temp',
  284. 'defaults' => $video_temp,
  285. 'desc' => $this->l('Please give video iframe url for video post. Separate by comma(,). You can add youtube or vimeo video url.'),
  286. ],
  287. */
  288. [
  289. // 'type' => 'textarea',
  290. 'type' => 'textarea',
  291. 'label' => $this->l('Video'),
  292. 'name' => 'video',
  293. //'defaults' => $video_temp,
  294. 'desc' => $this->l('Please give video iframe url for video post. Separate by comma(,). You can add youtube or vimeo video url.'),
  295. ],
  296. [
  297. 'type' => 'textarea',
  298. //'type' => 'text_multiple',
  299. 'label' => $this->l('Audio'),
  300. 'name' => 'audio',
  301. //'name' => 'audio_temp',
  302. //'defaults' => $audio_temp,
  303. 'desc' => $this->l('Please give Audio url for Audio post. Separate by comma(,). You can add any kind of an audio source.'),
  304. ],
  305. [
  306. 'type' => 'text',
  307. 'label' => $this->l('URL Rewrite'),
  308. 'name' => 'link_rewrite',
  309. 'desc' => $this->l('Enter Your Post Url for SEO'),
  310. 'lang' => true,
  311. ],
  312. [
  313. 'type' => 'select',
  314. 'label' => $this->l('Comment Status'),
  315. 'name' => 'comment_status',
  316. 'options' => [
  317. 'query' => [
  318. [
  319. 'id' => 'open',
  320. 'name' => 'Open',
  321. ],
  322. [
  323. 'id' => 'close',
  324. 'name' => 'Closed',
  325. ],
  326. [
  327. 'id' => 'disable',
  328. 'name' => 'Disabled',
  329. ],
  330. ],
  331. 'id' => 'id',
  332. 'name' => 'name',
  333. ],
  334. ],
  335. [
  336. 'type' => 'switch',
  337. 'label' => $this->l('Status'),
  338. 'name' => 'active',
  339. 'required' => false,
  340. 'class' => 't',
  341. 'is_bool' => true,
  342. 'values' => [
  343. [
  344. 'id' => 'active',
  345. 'value' => 1,
  346. 'label' => $this->l('Enabled'),
  347. ],
  348. [
  349. 'id' => 'active',
  350. 'value' => 0,
  351. 'label' => $this->l('Disabled'),
  352. ],
  353. ],
  354. ],
  355. ],
  356. 'submit' => [
  357. 'title' => $this->l('Save'),
  358. 'class' => 'btn btn-default pull-right',
  359. ],
  360. ];
  361. if (Shop::isFeatureActive()) {
  362. $this->fields_form['input'][] = [
  363. 'type' => 'shop',
  364. 'label' => $this->l('Shop association:'),
  365. 'name' => 'checkBoxShopAsso',
  366. ];
  367. }
  368. if (!($Chcreapost = $this->loadObject(true))) {
  369. return;
  370. }
  371. $this->setdefaultvalue($Chcreapost);
  372. $this->fields_form['submit'] = [
  373. 'title' => $this->l('Save '),
  374. 'class' => 'btn btn-default pull-right',
  375. ];
  376. $this->tpl_form_vars = [
  377. 'active' => $this->object->active,
  378. 'PS_ALLOW_ACCENTED_CHARS_URL', (int) Configuration::get('PS_ALLOW_ACCENTED_CHARS_URL'),
  379. ];
  380. Media::addJsDef(['PS_ALLOW_ACCENTED_CHARS_URL' => (int) Configuration::get('PS_ALLOW_ACCENTED_CHARS_URL')]);
  381. // print('<pre>'.print_r($this->fields_form,true).'</pre>');exit();
  382. // print('<pre>'.print_r($this->override_folder,true).'</pre>');exit();
  383. return parent::renderForm();
  384. }
  385. public function setdefaultvalue($obj)
  386. {
  387. if (isset($obj->post_format) && !empty($obj->post_format)) {
  388. $this->fields_value['post_format'] = $obj->post_format;
  389. } else {
  390. $this->fields_value['post_format'] = 'standrad';
  391. }
  392. if (isset($obj->active) && !empty($obj->active)) {
  393. $this->fields_value['active'] = $obj->active;
  394. } else {
  395. $this->fields_value['active'] = 1;
  396. }
  397. }
  398. public function renderList()
  399. {
  400. if (isset($this->_filter) && '' == trim($this->_filter)) {
  401. $this->_filter = $this->original_filter;
  402. }
  403. $this->addRowAction('edit');
  404. $this->addRowAction('delete');
  405. return parent::renderList();
  406. }
  407. public static function getallproducts()
  408. {
  409. $rslt = [];
  410. $rslt[0]['id'] = 0;
  411. $rslt[0]['name'] = 'Select Products';
  412. $id_lang = (int) Context::getContext()->language->id;
  413. $sql = 'SELECT p.`id_product`, pl.`name`, p.`reference`
  414. FROM `'._DB_PREFIX_.'product` p
  415. '.Shop::addSqlAssociation('product', 'p').'
  416. LEFT JOIN `'._DB_PREFIX_.'product_lang` pl ON (p.`id_product` = pl.`id_product` '.Shop::addSqlRestrictionOnLang('pl').')
  417. WHERE pl.`id_lang` = '.(int) $id_lang.' ORDER BY pl.`name`';
  418. $products = Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS($sql);
  419. if (isset($products)) {
  420. $i = 1;
  421. foreach ($products as $r) {
  422. $rslt[$i]['id'] = $r['id_product'];
  423. $rslt[$i]['name'] = $r['name'];
  424. $rslt[$i]['reference'] = $r['reference'];
  425. ++$i;
  426. }
  427. }
  428. return $rslt;
  429. }
  430. public function processPosition()
  431. {
  432. // ------------------------------------------------------------------------------
  433. // error_log("-----:function:".__FUNCTION__, 0);
  434. print_r($_POST);
  435. exit();
  436. if ('1' !== $this->tabAccess['edit']) {
  437. $this->errors[] = Tools::displayError('You do not have permission to edit this.');
  438. } elseif (!Validate::isLoadedObject($object = new Chcreapost((int) Tools::getValue($this->identifier, Tools::getValue('id_chcreaposts', 1))))) {
  439. $this->errors[] = Tools::displayError('An error occurred while updating the status for an object.').' <b>'.
  440. $this->table.'</b> '.Tools::displayError('(cannot load object)');
  441. }
  442. if (!$object->updatePosition((int) Tools::getValue('way'), (int) Tools::getValue('position'))) {
  443. $this->errors[] = Tools::displayError('Failed to update the position.');
  444. } else {
  445. $object->regenerateEntireNtree();
  446. Tools::redirectAdmin(self::$currentIndex.'&'.$this->table.'Orderby=position&'.$this->table.'Orderway=asc&conf=5'.(($id_chcreaposts = (int) Tools::getValue($this->identifier)) ? ('&'.$this->identifier.'='.$id_chcreaposts) : '').'&token='.Tools::getAdminTokenLite('Adminxipcategory'));
  447. }
  448. }
  449. public function ajaxProcessUpdatePositions()
  450. {
  451. $id_chcreaposts = (int) Tools::getValue('id');
  452. $way = (int) Tools::getValue('way');
  453. $positions = Tools::getValue($this->table);
  454. if (is_array($positions)) {
  455. foreach ($positions as $key => $value) {
  456. $pos = explode('_', $value);
  457. if ((isset($pos[1], $pos[2])) && ($pos[2] == $id_chcreaposts)) {
  458. $position = $key + 1;
  459. break;
  460. }
  461. }
  462. }
  463. $Chcreapost = new Chcreapost($id_chcreaposts);
  464. if (Validate::isLoadedObject($Chcreapost)) {
  465. if (isset($position) && $Chcreapost->updatePosition($way, $position)) {
  466. Hook::exec('action'.$this->className.'Update');
  467. exit(true);
  468. }
  469. exit('{"hasError" : true, errors : "Can not update Chcreapost position"}');
  470. }
  471. exit('{"hasError" : true, "errors" : "This Chcreapost can not be loaded"}');
  472. }
  473. }