PageRenderTime 27ms CodeModel.GetById 3ms RepoModel.GetById 0ms app.codeStats 0ms

/i18n_link_manager/inc/functions.php

https://gitlab.com/getsimple-cms/i18n_link_manager
PHP | 433 lines | 322 code | 34 blank | 77 comment | 67 complexity | 6e254d43592373d61f70b465dac33ba1 MD5 | raw file
  1. <?php if (!defined('IN_GS')) {die('you cannot load this page directly.');}
  2. /**
  3. * General functions used by the GetSimple I18N Link Manager plugin.
  4. */
  5. /*******************************************************
  6. * @function lm_save_category
  7. * @action collect $_POST data (single category) and write to file
  8. */
  9. function lm_save_category() {
  10. @copy(LM_CDATA, LM_CBACKUP);
  11. $id = isset($_POST['category-id']) ? intval($_POST['category-id']) : null;
  12. $cid = isset($_POST['category-cid']) ? intval($_POST['category-cid']) : time();
  13. $arr = array('cid'=>$cid, 'name'=>safe_slash_html($_POST['category-name']));
  14. if (function_exists('return_i18n_languages')) {
  15. foreach(return_i18n_languages() as $lang) {
  16. if ($lang != return_i18n_default_language()) {
  17. $arr['name_'.$lang] = safe_slash_html($_POST['category-name_'.$lang]);
  18. }
  19. }
  20. }
  21. $categories = lm_get_categories();
  22. if (isset($id))
  23. $categories[$id] = $arr;
  24. else
  25. $categories[] = $arr;
  26. if (lm_c_to_xml($categories))
  27. lm_display_message(i18n_r(LM_PLUGIN.'/SUCCESS_SAVE'), true, false, true);
  28. else
  29. lm_display_message(i18n_r(LM_PLUGIN.'/ERROR_SAVE'), false);
  30. }
  31. /*******************************************************
  32. * @function lm_save_category_order
  33. * @action collect $_POST data (category order) and write to file
  34. */
  35. function lm_save_category_order() {
  36. @copy(LM_CDATA, LM_CBACKUP);
  37. $categories = array();
  38. foreach ($_POST as $key=>$val) {
  39. if ($key != 'corder') {
  40. $categories[] = unserialize($val);
  41. }
  42. }
  43. if (lm_c_to_xml($categories))
  44. lm_display_message(i18n_r(LM_PLUGIN,'/SUCCESS_SAVE'), true, false, true);
  45. else
  46. lm_display_message(i18n_r(LM_PLUGIN.'/ERROR_SAVE'), false);
  47. }
  48. /*******************************************************
  49. * @function lm_edit_category
  50. * @param $cid int unique category identifier
  51. * @action create a new category or edit an existing one
  52. */
  53. function lm_edit_category($id=null) {
  54. if (isset($id)) {
  55. $categories = lm_get_categories();
  56. if (array_key_exists($id, $categories)) {
  57. $category = $categories[$id];
  58. $cid = $category['cid'];
  59. }
  60. }
  61. include(LM_TEMPLATE_PATH . 'edit_category.php');
  62. }
  63. /*******************************************************
  64. * @function lm_delete_category
  65. * @param $cid int unique category identifier
  66. * @action remove category with given $cid
  67. */
  68. function lm_delete_category($id) {
  69. @copy(LM_CDATA, LM_CBACKUP);
  70. $categories = lm_get_categories();
  71. if (array_key_exists($id, $categories)) {
  72. unset($categories[$id]);
  73. if (lm_c_to_xml($categories))
  74. lm_display_message(i18n_r(LM_PLUGIN.'/SUCCESS_DELETE'), true, false, true);
  75. else
  76. lm_display_message(i18n_r(LM_PLUGIN.'/ERROR_DELETE'), false);
  77. }
  78. }
  79. /*******************************************************
  80. * @function lm_get_categories
  81. * @return array with categories currently in database
  82. */
  83. function lm_get_categories() {
  84. $categories = array();
  85. $data = @getXML(LM_CDATA);
  86. if (!empty($data)) {
  87. $languages = array();
  88. if (function_exists('return_i18n_languages')) {
  89. $languages = return_i18n_languages();
  90. }
  91. foreach ($data->children() as $item) {
  92. $arr = array('cid'=>intval($item->cid), 'name'=>cl($item->name));
  93. if (!empty($languages)) {
  94. foreach ($languages as $lang) {
  95. if (return_i18n_default_language() != $lang) {
  96. $name = "name_$lang";
  97. $arr[$name] = cl($item->$name);
  98. }
  99. }
  100. }
  101. $categories[] = $arr;
  102. }
  103. }
  104. return $categories;
  105. }
  106. function lm_c_undo() {
  107. if (copy(LM_CBACKUP, LM_CDATA))
  108. lm_display_message(i18n_r(LM_PLUGIN.'/SUCCESS_RESTORE'));
  109. else
  110. lm_display_message(i18n_r(LM_PLUGIN.'/ERROR_RESTORE'), false);
  111. }
  112. /*******************************************************
  113. * @function lm_c_to_xml
  114. * @param $categories array with category data
  115. * @action write category data to xml file
  116. * @return bool true when successful, false otherwise
  117. */
  118. function lm_c_to_xml($categories) {
  119. debugLog($categories);
  120. $languages = array();
  121. if (function_exists('return_i18n_languages')) {
  122. $languages = return_i18n_languages();
  123. }
  124. $xml = new SimpleXMLExtended('<?xml version="1.0" encoding="UTF-8"?><channel></channel>');
  125. foreach ($categories as $id=>$cat) {
  126. $item = $xml->addChild('item');
  127. $elem = $item->addChild('cid');
  128. $elem->addCData($cat['cid']);
  129. if (empty($languages)) {
  130. $elem = $item->addChild('name');
  131. $elem->addCData($cat['name']);
  132. } else {
  133. foreach($languages as $lang) {
  134. $name = (return_i18n_default_language() != $lang) ? $name = "name_$lang" : $name = 'name';
  135. $elem = $item->addChild($name);
  136. $elem->addCData($cat[$name]);
  137. }
  138. }
  139. }
  140. return @XMLsave($xml, LM_CDATA);
  141. }
  142. /*******************************************************
  143. * @function lm_get_links
  144. * @param $count int count of links, or null for all of them
  145. * @return array with links currently in database
  146. */
  147. function lm_get_links($count=null) {
  148. $name = 'name';
  149. $desc = 'description';
  150. $links = array();
  151. $data = @getXML(LM_DATA);
  152. if (!empty($data)) {
  153. if (function_exists('return_i18n_languages'))
  154. $languages = return_i18n_languages();
  155. $categories = lm_get_categories();
  156. $i = 0;
  157. foreach ($data->children() as $item) {
  158. $id = intval($item->id);
  159. $cid = intval($item->cid);
  160. $arr = array('cid'=>$cid, 'url'=>strval($item->url), $name=>cl($item->$name), $desc=>cl($item->$desc));
  161. if (function_exists('return_i18n_languages')) {
  162. foreach ($languages as $lang) {
  163. if ($lang != return_i18n_default_language()) {
  164. $n = $name.'_'.$lang;
  165. $d = $desc.'_'.$lang;
  166. $arr[$n] = cl($item->$n);
  167. $arr[$d] = cl($item->$d);
  168. }
  169. }
  170. }
  171. $links[$id] = $arr;
  172. $i++;
  173. if ($count !== null && $i == $count) break;
  174. }
  175. }
  176. return $links;
  177. }
  178. /*******************************************************
  179. * @function lm_edit_link
  180. * @param $id int unique link identifier
  181. * @action create a new link or edit an existing one
  182. */
  183. function lm_edit_link($id=null) {
  184. if (isset($id)) {
  185. $links = lm_get_links();
  186. $categories = lm_get_categories();
  187. debugLog($categories);
  188. if (function_exists('return_i18n_languages'))
  189. $languages = return_i18n_languages();
  190. if (array_key_exists($id, $links))
  191. $link = $links[$id];
  192. }
  193. include(LM_TEMPLATE_PATH . 'edit_link.php');
  194. }
  195. /*******************************************************
  196. * @function lm_save_link
  197. * @action collect $_POST data (single link) and write to file
  198. */
  199. function lm_save_link() {
  200. @copy(LM_DATA, LM_BACKUP);
  201. $id = isset($_POST['link-id']) ? intval($_POST['link-id']) : null;
  202. $arr = array('cid'=>intval($_POST['link-cid']), 'url'=>$_POST['link-url']);
  203. $arr['name'] = safe_slash_html($_POST['link-name']);
  204. $arr['description'] = safe_slash_html($_POST['link-description']);
  205. if (function_exists('return_i18n_languages')) {
  206. foreach(return_i18n_languages() as $lang) {
  207. if ($lang != return_i18n_default_language()) {
  208. $arr['name_'.$lang] = safe_slash_html($_POST['link-name_'.$lang]);
  209. $arr['description_'.$lang] = safe_slash_html($_POST['link-description_'.$lang]);
  210. }
  211. }
  212. }
  213. $links = lm_get_links();
  214. if (isset($id))
  215. $links[$id] = $arr;
  216. else
  217. $links[] = $arr;
  218. if (lm_to_xml($links))
  219. lm_display_message(i18n_r(LM_PLUGIN.'/SUCCESS_SAVE'), true, true);
  220. else
  221. lm_display_message(i18n_r(LM_PLUGIN.'/ERROR_SAVE'), false);
  222. }
  223. /*******************************************************
  224. * @function lm_delete_link
  225. * @param $id int unique link identifier
  226. * @action remove link with given $id
  227. */
  228. function lm_delete_link($id) {
  229. @copy(LM_DATA, LM_BACKUP);
  230. $links = lm_get_links();
  231. if (array_key_exists($id, $links)) {
  232. unset($links[$id]);
  233. if (lm_to_xml($links))
  234. lm_display_message(i18n_r(LM_PLUGIN.'/SUCCESS_DELETE'), true, true);
  235. else
  236. lm_display_message(i18n_r(LM_PLUGIN.'/ERROR_DELETE'), false);
  237. }
  238. }
  239. /*******************************************************
  240. * @function lm_save_order
  241. * @action collect $_POST data (link order) and write to file
  242. */
  243. function lm_save_order() {
  244. @copy(LM_DATA, LM_BACKUP);
  245. $links = array();
  246. foreach ($_POST as $key=>$val) {
  247. if ($key != 'order') {
  248. $links[] = unserialize($val);
  249. }
  250. }
  251. if (lm_to_xml($links))
  252. lm_display_message(i18n_r(LM_PLUGIN,'/SUCCESS_SAVE'), true, true);
  253. else
  254. lm_display_message(i18n_r(LM_PLUGIN.'/ERROR_SAVE'), false);
  255. }
  256. function lm_undo() {
  257. if (copy(LM_BACKUP, LM_DATA))
  258. lm_display_message(i18n_r(LM_PLUGIN.'/SUCCESS_RESTORE'));
  259. else
  260. lm_display_message(i18n_r(LM_PLUGIN.'/ERROR_RESTORE'), false);
  261. }
  262. /*******************************************************
  263. * @function lm_to_html
  264. * @action convert link data to html list
  265. * @return string links in html format
  266. */
  267. function lm_to_html() {
  268. global $language, $cats;
  269. $cats = lm_get_categories();
  270. function catIdx($cid) {
  271. global $cats;
  272. foreach($cats as $key=>&$val) {
  273. if ($val['cid'] == $cid) return $key;
  274. }
  275. return 0;
  276. }
  277. function linkCmp(&$a, &$b) {
  278. global $cats;
  279. if ($a['cid'] == $b['cid'])
  280. return 0;
  281. return catIdx($a['cid']) > catIdx($b['cid']) ? 1 : -1;
  282. }
  283. $html = '';
  284. $links = lm_get_links();
  285. usort($links, 'linkCmp');
  286. $cid = null;
  287. if (function_exists('return_i18n_default_language') && !empty($language) && $language != return_i18n_default_language()) {
  288. $n = "name_$language";
  289. $d = "description_$language";
  290. } else {
  291. $n = "name";
  292. $d = "description";
  293. }
  294. if (!empty($links)) {
  295. foreach ($links as $id=>$link) {
  296. $c = catIdx($link['cid']);
  297. if ($cid !== $c) {
  298. $cid = $c;
  299. $html .= "</ul><b>" . (empty($cats[$cid][$n]) ? $cats[$cid]['name'] : $cats[$cid][$n]) . ":</b><ul>";
  300. }
  301. $html .= "<li><a href=\"${link['url']}\" target=\"_blank\" rel=\"nofollow\">" . (empty($link[$n]) ? $link['name'] : $link[$n]) ."</a>";
  302. if (!empty($link[$d]) || !empty($link['description'])) {
  303. $html .= ' &nbsp;&mdash;&nbsp; ' . (empty($link[$d]) ? $link['description'] : $link[$d]);
  304. }
  305. $html .= '</li>';
  306. }
  307. $html = '<ul>' . $html . '</ul>';
  308. }
  309. return $html;
  310. }
  311. /*******************************************************
  312. * @function lm_to_xml
  313. * @param $links array with link data
  314. * @action write link data to xml file
  315. * @return bool true when successful, false otherwise
  316. */
  317. function lm_to_xml($links) {
  318. debugLog($links);
  319. $name = 'name';
  320. $desc = 'description';
  321. $languages = array();
  322. if (function_exists('return_i18n_languages')) {
  323. $languages = return_i18n_languages();
  324. }
  325. $xml = new SimpleXMLExtended('<?xml version="1.0" encoding="UTF-8"?><channel></channel>');
  326. foreach ($links as $id=>$link) {
  327. $item = $xml->addChild('item');
  328. $elem = $item->addChild('id');
  329. $elem->addCData($id);
  330. $elem = $item->addChild('cid');
  331. $elem->addCData($link['cid']);
  332. $elem = $item->addChild('url');
  333. $elem->addCData($link['url']);
  334. if (empty($languages)) {
  335. $elem = $item->addChild($name);
  336. $elem->addCData($link[$name]);
  337. $elem = $item->addChild($desc);
  338. $elem->addCData($link[$desc]);
  339. } else {
  340. foreach($languages as $lang) {
  341. if ($lang != return_i18n_default_language()) {
  342. $n = $name.'_'.$lang;
  343. $d = $desc.'_'.$lang;
  344. } else {
  345. $n = $name;
  346. $d = $desc;
  347. }
  348. $elem = $item->addChild($n);
  349. $elem->addCData($link[$n]);
  350. $elem = $item->addChild($d);
  351. $elem->addCData($link[$d]);
  352. }
  353. }
  354. }
  355. return @XMLsave($xml, LM_DATA);
  356. }
  357. /*******************************************************
  358. * @function lm_display_message
  359. * @param $msg string containing the message
  360. * @param $update bool if true, show as $msg as update, else as error
  361. * @param $undo bool if true, provide undo link
  362. * @param $cundo bool if true, provide undo link for category
  363. * @action display status messages on back-end pages
  364. */
  365. function lm_display_message($msg, $update=true, $undo=false, $cundo=false) {
  366. if (isset($msg)) {
  367. if ($undo)
  368. $msg .= " <a href=\"load.php?id=".LM_PLUGIN."&undo\">" . i18n_r(LM_PLUGIN.'/UNDO') . '</a>';
  369. if ($cundo)
  370. $msg .= " <a href=\"load.php?id=".LM_PLUGIN."&cundo\">" . i18n_r(LM_PLUGIN,'/UNDO') . '</a>';
  371. ?>
  372. <script type="text/javascript">
  373. $(function() {
  374. $('div.bodycontent').before('<div class="<?php echo $update ? 'updated' : 'error'; ?>" style="display:block;">'+
  375. <?php echo json_encode($msg); ?>+'</div>');
  376. $(".updated, .error").fadeOut(500).fadeIn(500);
  377. });
  378. </script>
  379. <?php
  380. }
  381. }
  382. /*******************************************************
  383. * @function lm_header_include
  384. * @action insert necessary script/style sections into site header
  385. */
  386. function lm_header_include() {
  387. ?>
  388. <script type="text/javascript" src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.8/jquery.validate.min.js"></script>
  389. <script type="text/javascript" src="../plugins/link_manager/template/js/jquery-ui.sort.min.js"></script>
  390. <style>
  391. .invalid {
  392. color: #D94136;
  393. font-size: 11px;
  394. font-weight: normal;
  395. }
  396. </style>
  397. <?php
  398. }
  399. ?>