AdminChcreateur.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509
  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 678",
  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 678",
  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. ],
  267. ],
  268. [
  269. 'type' => 'gallery',
  270. 'label' => "Image dans l'article </br> 1200 x 678",
  271. 'name' => 'gallery_temp',
  272. 'defaults' => $gallery_temp,
  273. 'defaults_str' => $gallery_temp_str,
  274. 'url' => $gallery_url,
  275. 'desc' => $this->l('Please give Image url for Gallery post. Separate by comma(,). You can add Any Kind of Image URL.'),
  276. ],
  277. /*
  278. [
  279. // 'type' => 'textarea',
  280. 'type' => 'textarea',
  281. 'label' => $this->l('Video'),
  282. 'name' => 'video_temp',
  283. 'defaults' => $video_temp,
  284. 'desc' => $this->l('Please give video iframe url for video post. Separate by comma(,). You can add youtube or vimeo video url.'),
  285. ],
  286. */
  287. [
  288. // 'type' => 'textarea',
  289. 'type' => 'textarea',
  290. 'label' => $this->l('Video'),
  291. 'name' => 'video',
  292. //'defaults' => $video_temp,
  293. 'desc' => $this->l('Please give video iframe url for video post. Separate by comma(,). You can add youtube or vimeo video url.'),
  294. ],
  295. [
  296. 'type' => 'textarea',
  297. //'type' => 'text_multiple',
  298. 'label' => $this->l('Audio'),
  299. 'name' => 'audio',
  300. //'name' => 'audio_temp',
  301. //'defaults' => $audio_temp,
  302. 'desc' => $this->l('Please give Audio url for Audio post. Separate by comma(,). You can add any kind of an audio source.'),
  303. ],
  304. [
  305. 'type' => 'text',
  306. 'label' => $this->l('URL Rewrite'),
  307. 'name' => 'link_rewrite',
  308. 'desc' => $this->l('Enter Your Post Url for SEO'),
  309. 'lang' => true,
  310. ],
  311. [
  312. 'type' => 'select',
  313. 'label' => $this->l('Comment Status'),
  314. 'name' => 'comment_status',
  315. 'options' => [
  316. 'query' => [
  317. [
  318. 'id' => 'open',
  319. 'name' => 'Open',
  320. ],
  321. [
  322. 'id' => 'close',
  323. 'name' => 'Closed',
  324. ],
  325. [
  326. 'id' => 'disable',
  327. 'name' => 'Disabled',
  328. ],
  329. ],
  330. 'id' => 'id',
  331. 'name' => 'name',
  332. ],
  333. ],
  334. [
  335. 'type' => 'switch',
  336. 'label' => $this->l('Status'),
  337. 'name' => 'active',
  338. 'required' => false,
  339. 'class' => 't',
  340. 'is_bool' => true,
  341. 'values' => [
  342. [
  343. 'id' => 'active',
  344. 'value' => 1,
  345. 'label' => $this->l('Enabled'),
  346. ],
  347. [
  348. 'id' => 'active',
  349. 'value' => 0,
  350. 'label' => $this->l('Disabled'),
  351. ],
  352. ],
  353. ],
  354. ],
  355. 'submit' => [
  356. 'title' => $this->l('Save'),
  357. 'class' => 'btn btn-default pull-right',
  358. ],
  359. ];
  360. if (Shop::isFeatureActive()) {
  361. $this->fields_form['input'][] = [
  362. 'type' => 'shop',
  363. 'label' => $this->l('Shop association:'),
  364. 'name' => 'checkBoxShopAsso',
  365. ];
  366. }
  367. if (!($Chcreapost = $this->loadObject(true))) {
  368. return;
  369. }
  370. $this->setdefaultvalue($Chcreapost);
  371. $this->fields_form['submit'] = [
  372. 'title' => $this->l('Save '),
  373. 'class' => 'btn btn-default pull-right',
  374. ];
  375. $this->tpl_form_vars = [
  376. 'active' => $this->object->active,
  377. 'PS_ALLOW_ACCENTED_CHARS_URL', (int) Configuration::get('PS_ALLOW_ACCENTED_CHARS_URL'),
  378. ];
  379. Media::addJsDef(['PS_ALLOW_ACCENTED_CHARS_URL' => (int) Configuration::get('PS_ALLOW_ACCENTED_CHARS_URL')]);
  380. // print('<pre>'.print_r($this->fields_form,true).'</pre>');exit();
  381. // print('<pre>'.print_r($this->override_folder,true).'</pre>');exit();
  382. return parent::renderForm();
  383. }
  384. public function setdefaultvalue($obj)
  385. {
  386. if (isset($obj->post_format) && !empty($obj->post_format)) {
  387. $this->fields_value['post_format'] = $obj->post_format;
  388. } else {
  389. $this->fields_value['post_format'] = 'standrad';
  390. }
  391. if (isset($obj->active) && !empty($obj->active)) {
  392. $this->fields_value['active'] = $obj->active;
  393. } else {
  394. $this->fields_value['active'] = 1;
  395. }
  396. }
  397. public function renderList()
  398. {
  399. if (isset($this->_filter) && '' == trim($this->_filter)) {
  400. $this->_filter = $this->original_filter;
  401. }
  402. $this->addRowAction('edit');
  403. $this->addRowAction('delete');
  404. return parent::renderList();
  405. }
  406. public static function getallproducts()
  407. {
  408. $rslt = [];
  409. $rslt[0]['id'] = 0;
  410. $rslt[0]['name'] = 'Select Products';
  411. $id_lang = (int) Context::getContext()->language->id;
  412. $sql = 'SELECT p.`id_product`, pl.`name`
  413. FROM `'._DB_PREFIX_.'product` p
  414. '.Shop::addSqlAssociation('product', 'p').'
  415. LEFT JOIN `'._DB_PREFIX_.'product_lang` pl ON (p.`id_product` = pl.`id_product` '.Shop::addSqlRestrictionOnLang('pl').')
  416. WHERE pl.`id_lang` = '.(int) $id_lang.' ORDER BY pl.`name`';
  417. $products = Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS($sql);
  418. if (isset($products)) {
  419. $i = 1;
  420. foreach ($products as $r) {
  421. $rslt[$i]['id'] = $r['id_product'];
  422. $rslt[$i]['name'] = $r['name'];
  423. ++$i;
  424. }
  425. }
  426. return $rslt;
  427. }
  428. public function processPosition()
  429. {
  430. // ------------------------------------------------------------------------------
  431. // error_log("-----:function:".__FUNCTION__, 0);
  432. print_r($_POST);
  433. exit();
  434. if ('1' !== $this->tabAccess['edit']) {
  435. $this->errors[] = Tools::displayError('You do not have permission to edit this.');
  436. } elseif (!Validate::isLoadedObject($object = new Chcreapost((int) Tools::getValue($this->identifier, Tools::getValue('id_chcreaposts', 1))))) {
  437. $this->errors[] = Tools::displayError('An error occurred while updating the status for an object.').' <b>'.
  438. $this->table.'</b> '.Tools::displayError('(cannot load object)');
  439. }
  440. if (!$object->updatePosition((int) Tools::getValue('way'), (int) Tools::getValue('position'))) {
  441. $this->errors[] = Tools::displayError('Failed to update the position.');
  442. } else {
  443. $object->regenerateEntireNtree();
  444. 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'));
  445. }
  446. }
  447. public function ajaxProcessUpdatePositions()
  448. {
  449. $id_chcreaposts = (int) Tools::getValue('id');
  450. $way = (int) Tools::getValue('way');
  451. $positions = Tools::getValue($this->table);
  452. if (is_array($positions)) {
  453. foreach ($positions as $key => $value) {
  454. $pos = explode('_', $value);
  455. if ((isset($pos[1], $pos[2])) && ($pos[2] == $id_chcreaposts)) {
  456. $position = $key + 1;
  457. break;
  458. }
  459. }
  460. }
  461. $Chcreapost = new Chcreapost($id_chcreaposts);
  462. if (Validate::isLoadedObject($Chcreapost)) {
  463. if (isset($position) && $Chcreapost->updatePosition($way, $position)) {
  464. Hook::exec('action'.$this->className.'Update');
  465. exit(true);
  466. }
  467. exit('{"hasError" : true, errors : "Can not update Chcreapost position"}');
  468. }
  469. exit('{"hasError" : true, "errors" : "This Chcreapost can not be loaded"}');
  470. }
  471. }