PageRenderTime 165ms CodeModel.GetById 35ms RepoModel.GetById 4ms app.codeStats 0ms

/mem_moderation/mem_moderation_link.php

https://bitbucket.org/Manfre/txp-plugins
PHP | 519 lines | 363 code | 109 blank | 47 comment | 66 complexity | 21b49456973cb8587988da3f6bde6af4 MD5 | raw file
  1. <?php
  2. // Copy this file to a new name like abc_myplugin.php. Edit the code, then
  3. // run this file at the command line to produce a plugin for distribution:
  4. // $ php abc_myplugin.php > abc_myplugin-0.1.txt
  5. // Plugin name is optional. If unset, it will be extracted from the current
  6. // file name. Uncomment and edit this line to override:
  7. $plugin['name'] = 'mem_moderation_link';
  8. $plugin['version'] = '0.2';
  9. $plugin['author'] = 'Michael Manfre';
  10. $plugin['author_uri'] = 'http://manfre.net/';
  11. $plugin['description'] = 'Moderation plugin that allows links to be submitted to the moderation queue.';
  12. $plugin['type'] = '1'; // 0 for regular plugin; 1 if it includes admin-side code
  13. @include_once('../zem_tpl.php');
  14. if (0) {
  15. ?>
  16. # --- BEGIN PLUGIN HELP ---
  17. h1(title). mem_moderation_link plugin
  18. h2(section summary). Summary
  19. p. This moderation plugin allows users to submit links to the moderation queue.
  20. h2(section contact). Author Contact
  21. "Michael Manfre":mailto:mmanfre@gmail.com?subject=Textpattern%20mem_moderation_link%20plugin
  22. "http://manfre.net":http://manfre.net
  23. h2(section license). License
  24. p. This plugin is licensed under the "GPLv2":http://www.fsf.org/licensing/licenses/info/GPLv2.html.
  25. h2(section installation). Installation
  26. p. "Start Install Wizard":./index.php?event=link_moderate&step=preinstall
  27. p. Installs two forms: mod_link_submit_form and mod_link_success.
  28. h2(section tags). Tags
  29. h3(tag#mem_moderation_link_form). mem_moderation_link_form
  30. p(tag-summary). mem_form helper tag for link submission/edit form.
  31. p. Takes the same attributes as mem_form. See mem_form for tag attributes and usage.
  32. p. Example: @<txp:mem_moderation_link_form form="mod_link_submit_form" thanks_form="mod_link_success" label="Link Details" />@
  33. # --- END PLUGIN HELP ---
  34. <?php
  35. }
  36. # --- BEGIN PLUGIN CODE ---
  37. ////////////////////////////////////////////////////////////
  38. // Plugin mem_moderation_link
  39. // Author: Michael Manfre (http://manfre.net/)
  40. // Revisions:
  41. //
  42. ////////////////////////////////////////////////////////////
  43. global $event, $step, $mem_link_vars, $mem_link_delete_vars;
  44. require_plugin('mem_moderation');
  45. // link/link-edit vars
  46. $mem_link_vars = array('note','user','email','modid','id','linkid','date','category','url','linkname','linksort','description');
  47. // link-delete vars
  48. $mem_link_delete_vars = array('user','email','linkid','title');
  49. if (@txpinterface == 'admin') {
  50. // give publisher install access
  51. add_privs('moderate.link','1');
  52. // add_privs('moderate.link','1,2,3,4,5,6');
  53. // register event with txp
  54. register_callback('link_moderate','link_moderate','', 1);
  55. // wire up moderation types
  56. mem_moderation_register('link',$mem_link_vars,'link_moderate_presenter','link_moderate_approver','');
  57. mem_moderation_register('link-edit',$mem_link_vars,'link_moderate_presenter','link_moderate_approver','');
  58. mem_moderation_register('link-delete',$mem_link_vars,'link_moderate_presenter','link_moderate_approver','');
  59. if ($event=='link_moderate')
  60. {
  61. /* Event handler */
  62. function link_moderate($event, $step)
  63. {
  64. $msg='';
  65. if ($step=='link_save' or $step=='link_post')
  66. {
  67. // save changes
  68. $msg = link_moderate_save($step);
  69. }
  70. else if ($step=='preinstall' or $step=='install')
  71. {
  72. // need tab during install
  73. register_tab('extensions','link_moderate','link_moderate');
  74. echo pageTop('Link Moderation','');
  75. echo link_install();
  76. return;
  77. }
  78. else
  79. {
  80. if ($step)
  81. {
  82. // $msg = "Step is '$step'";
  83. trigger_error(gTxt('unknown_step'));
  84. }
  85. }
  86. pageTop('Link Moderation',$msg);
  87. echo link_moderate_form();
  88. }
  89. /** Install default forms, prefs, etc. */
  90. function link_install()
  91. {
  92. $log = array();
  93. // default article edit form
  94. $form = fetch('Form','txp_form','name','mod_link_submit_form');
  95. if (!$form)
  96. {
  97. $form_html = <<<EOF
  98. <div><txp:mem_form_text name="linkname" required="1" label="Link Title" break="" /></div>
  99. <div><txp:mem_form_text name="url" required="1" label="URL (incl. http://)" break="" /></div>
  100. <div><txp:mem_form_textarea name="description" required="1" label="Description" break="" /></div>
  101. <div><txp:mem_form_textarea name="note" class="txtarea_medium" label="Notes for the Moderator (optional)" break="" required="0" /></div>
  102. <txp:mem_form_submit button="1" name="action"><span>Submit Link suggestion</span></txp:mem_form_submit>
  103. EOF;
  104. $form_html = doSlash($form_html);
  105. if (safe_insert('txp_form',"name='mod_link_submit_form',type='misc',Form='{$form_html}'"))
  106. $log[] = "Created form 'mod_link_submit_form'";
  107. else
  108. $log[] = "Failed to create form 'mod_link_submit_form'. " . mysql_error();
  109. }
  110. else
  111. {
  112. $log[] = "Form 'mod_link_submit_form' already exists. Skipping installation of default form.";
  113. }
  114. // default article success form
  115. $form = fetch('Form','txp_form','name','mod_link_success');
  116. if (!$form)
  117. {
  118. $form_html = <<<EOF
  119. <txp:mem_if_step name="link_delete">
  120. <h2>Link deleted</h2>
  121. <p>Your submission has been deleted and will not be posted on this site.</p>
  122. <txp:else />
  123. <h2>Link submitted</h2>
  124. <p>Thank you for submitting your link. It will be reviewed by a moderator before being posted to the site.</p>
  125. </txp:mem_if_step>
  126. EOF;
  127. $form_html = doSlash($form_html);
  128. if (safe_insert('txp_form',"name='mod_link_success',type='misc',Form='{$form_html}'"))
  129. $log[] = "Created form 'mod_link_success'";
  130. else
  131. $log[] = "Failed to create form 'mod_link_success'. " . mysql_error();
  132. }
  133. else
  134. {
  135. $log[] = "Form 'mod_link_success' already exists. Skipping installation of default form.";
  136. }
  137. return tag("Install Log",'h2').doWrap($log,'ul','li');
  138. }
  139. } // if link_moderate
  140. } // if admin
  141. // -------------------------------------------------------------
  142. function link_moderate_form()
  143. {
  144. global $step,$mem_link_vars;
  145. if ($mem_link_vars) extract(gpsa($mem_link_vars));
  146. if($id && $step=='link_edit') {
  147. extract(safe_row("*", "txp_link", "id = $id"));
  148. }
  149. if ($step=='link_save' or $step=='link_post'){
  150. foreach($vars as $var) {
  151. $$var = '';
  152. }
  153. }
  154. $textarea = '<textarea name="description" cols="40" rows="7" tabindex="4">'.
  155. $description.'</textarea>';
  156. $selects = event_category_popup("link", $category);
  157. $editlink = ''; //' ['.eLink('category','list','','',gTxt('edit')).']';
  158. $out =
  159. startTable( 'edit' ) .
  160. tr( fLabelCell( 'title' ) . fInputCell( 'linkname', $linkname, 1, 30 )) .
  161. tr( fLabelCell( 'sort_value') .fInputCell( 'linksort', $linksort, 2, 15 )) .
  162. tr( fLabelCell( 'url','link_url') . fInputCell( 'url', $url, 3, 30) ) .
  163. tr( fLabelCell( 'link_category', 'link_category' ) . td( $selects . $editlink ) ) .
  164. tr( fLabelCell( 'description', 'link_description' ) . tda( $textarea, ' valign="top"' ) ) .
  165. tr( td() . td( fInput( "submit", '', gTxt( 'save' ), "publish" ) ) ) .
  166. endTable() .
  167. eInput( 'link_moderate' ) . sInput( ( $step=='link_edit' ) ? 'link_save' : 'link_post' ) .
  168. hInput( 'id', $id );
  169. return form( $out );
  170. }
  171. // -------------------------------------------------------------
  172. function link_moderate_save($step)
  173. {
  174. global $txpcfg,$txpac,$mem_link_vars,$txp_user;
  175. $varray = gpsa($mem_link_vars);
  176. if($txpac['textile_links']) {
  177. include_once $txpcfg['txpath'].'/lib/classTextile.php';
  178. $textile = new Textile();
  179. $varray['description'] = $textile->TextileThis($varray['description'],1);
  180. }
  181. if (!$varray['linksort']) $varray['linksort'] = $varray['linkname'];
  182. $res = '';
  183. if ($step=='link_edit') {
  184. if (update_moderated_content($id,$varray['description'],$varray)) {
  185. $res = "Updated link '$id'";
  186. } else {
  187. $res = "Failed to update link '$id'";
  188. }
  189. } else {
  190. $email = safe_field('email','txp_users',"name='$txp_user'");
  191. if ($email and ($res=submit_moderated_content('link',$email,$varray['description'],$varray))) {
  192. $res = "Submitted link '$res'";
  193. } else {
  194. $res = "Failed to submit link";
  195. }
  196. }
  197. return $res;
  198. }
  199. function link_moderate_presenter($type,$data) {
  200. if ($type=='link') {
  201. $description='';
  202. $linkname='';
  203. $linksort='';
  204. $category='';
  205. $url='';
  206. if (is_array($data)) extract($data);
  207. $selects = event_category_popup("link", $category);
  208. $textarea = '<textarea name="description" cols="40" rows="7" tabindex="4">'.$description.'</textarea>';
  209. return startTable( 'edit' ) .
  210. tr( fLabelCell( 'title' ) . fInputCell( 'linkname', $linkname, 1, 30 )) .
  211. tr( fLabelCell( 'sort_value') .fInputCell( 'linksort', $linksort, 2, 15 )) .
  212. tr( fLabelCell( 'url','link_url') . fInputCell( 'url', $url, 3, 30) ) .
  213. ((!empty($url) and !empty($linkname)) ? tr( fLabelCell( 'url' ) . td( href($linkname,$url) ) ) : '' ).
  214. tr( fLabelCell( 'link_category', 'link_category' ) . td( $selects ) ) .
  215. tr( fLabelCell( 'description', 'link_description' ) . tda( $textarea, ' valign="top"' ) );
  216. endTable();
  217. }
  218. return '';
  219. }
  220. function link_moderate_approver($type,$data)
  221. {
  222. if ($type=='link' && is_array($data)) {
  223. extract($data);
  224. $q = safe_insert("txp_link",
  225. "category = '$category',
  226. date = now(),
  227. url = '".trim($url)."',
  228. linkname = '$linkname',
  229. linksort = '$linksort',
  230. description = '$description'"
  231. );
  232. if (!$q)
  233. return 'Failed to approve link';
  234. }
  235. }
  236. //////////////////////////////////////////////////////
  237. // Public form methods
  238. /** Helper tag for mem_form */
  239. function mem_moderation_link_form($atts, $thing='')
  240. {
  241. $atts['type'] = 'mem_moderation_link';
  242. return mem_form($atts, $thing);
  243. }
  244. // register functions with mem_form
  245. register_callback('mem_mod_link_form_defaults', 'mem_form.defaults');
  246. register_callback('mem_mod_link_form_display', 'mem_form.display');
  247. register_callback('mem_mod_link_form_submitted', 'mem_form.submit');
  248. /** Set form defaults */
  249. function mem_mod_link_form_defaults()
  250. {
  251. global $mem_form, $mem_form_type, $mem_form_default, $mem_mod_info, $mem_modlink_info;
  252. // type check
  253. if ($mem_form_type!='mem_moderation_link')
  254. {
  255. return;
  256. }
  257. extract(gpsa(array('modid','linkid')));
  258. // editing mod item
  259. if (!empty($modid))
  260. {
  261. // get mod data
  262. $mem_mod_info = safe_row('*','txp_moderation',"`id`='".doSlash($modid)."'");
  263. if ($mem_mod_info)
  264. {
  265. // set decoded
  266. $mem_modlink_info = mem_moderation_decode($mem_mod_info['data']);
  267. }
  268. }
  269. // editing link
  270. else if (!empty($linkid))
  271. {
  272. $rs = safe_row('*', 'txp_link',"`id`='".doSlash($linkid)."'");
  273. // set mod data
  274. $mem_modlink_info = array();
  275. foreach($rs as $k => $v)
  276. {
  277. $mem_modlink_info[strtolower($k)] = $v;
  278. }
  279. }
  280. if (is_array($mem_modlink_info))
  281. {
  282. // set defaults
  283. foreach($mem_modlink_info as $key => $val)
  284. {
  285. mem_form_default($key, $val);
  286. }
  287. }
  288. }
  289. function mem_mod_link_form_display()
  290. {
  291. global $mem_form_type, $mem_form_labels, $mem_form_values, $mem_mod_info, $mem_modlink_info;
  292. // type check
  293. if ($mem_form_type!='mem_moderation_link')
  294. {
  295. return;
  296. }
  297. $out = '';
  298. if (isset($mem_mod_info))
  299. {
  300. $out .= n.'<input type="hidden" name="modid" value="'.htmlspecialchars($mem_mod_info['id']).'" />'.
  301. n.'<input type="hidden" name="type" value="'. htmlspecialchars($mem_mod_info['type']).'" />';
  302. if ($mem_mod_info['type'] == 'link-edit' && isset($mem_modlink_info['linkid']))
  303. $out .= n.'<input type="hidden" name="linkid" value="'.$mem_modlink_info['linkid'].'" />';
  304. mem_form_store('modid', 'modid', $mem_mod_info['id']);
  305. mem_form_store('type', 'type', $mem_mod_info['type']);
  306. }
  307. else if (isset($mem_modlink_info))
  308. {
  309. $out .= n.'<input type="hidden" name="linkid" value="'.htmlspecialchars($mem_modlink_info['linkid']).'" />'.
  310. n.'<input type="hidden" name="type" value="link" />';
  311. }
  312. return $out;
  313. }
  314. /** Form submit handler */
  315. function mem_mod_link_form_submitted()
  316. {
  317. global $prefs, $mem_link_vars, $mem_form_type, $txp_user, $ign_user, $mem_mod_info, $mem_modlink_info, $mem_form_thanks_form;
  318. if ($mem_form_type!='mem_moderation_link')
  319. {
  320. return;
  321. }
  322. extract(gpsa(array('modid','step','id','linkid')));
  323. $mem_modlink_info = gpsa($mem_link_vars);
  324. $out = '';
  325. $is_save = ps('mem_moderation_save');
  326. $is_delete = ps('mem_moderation_delete');
  327. $is_update = ps('mem_moderation_update') || ($is_save && ps('modid'));
  328. if (isset($ign_user)) $txp_user = $ign_user;
  329. if (!empty($modid)) $id = $modid;
  330. if ($is_delete)
  331. {
  332. if (remove_moderated_content($modid))
  333. {
  334. $res = mem_moderation_gTxt('link_deleted');
  335. }
  336. else
  337. {
  338. $res = mem_moderation_gTxt('link_delete_failed');
  339. }
  340. }
  341. elseif (!empty($linkid))
  342. {
  343. $linkid = doSlash($linkid);
  344. $rs = safe_rows("*, unix_timestamp(date) as udate","txp_link","`id` = $linkid");
  345. if ($rs)
  346. {
  347. // merge the passed in values to the existing
  348. foreach($mem_modlink_info as $key => $val)
  349. {
  350. $rs[$key] = $val;
  351. }
  352. $rs['linkid'] = $linkid;
  353. if ($is_update)
  354. {
  355. $res = update_moderated_content($id, $mem_modlink_info['note'], $mem_modlink_info);
  356. }
  357. else
  358. {
  359. $res = submit_moderated_content('link-edit', $user_email, $mem_modlink_info['note'], $mem_modlink_info, $linkid);
  360. if ($res)
  361. {
  362. // delete all other pending link moderation actions by this user for the same item_id
  363. safe_delete('txp_moderation', "`type` LIKE 'link%' and user = '$txp_user' and item_id = $linkid and id != $res and item_id != 0");
  364. }
  365. }
  366. } // if $rs
  367. }
  368. else
  369. {
  370. if (isset($id)) $mem_modlink_info['id'] = $id;
  371. if ($is_update)
  372. {
  373. if (isset($mem_modlink_info['note']))
  374. {
  375. $note = $mem_modlink_info['note'];
  376. }
  377. else
  378. {
  379. $note = $mem_mod_info['desc'];
  380. }
  381. if (update_moderated_content($id,$note,$mem_modlink_info))
  382. {
  383. $res = mem_moderation_gTxt('link_updated');
  384. }
  385. else
  386. {
  387. $res = mem_moderation_gTxt('link_update_failed');
  388. }
  389. }
  390. else
  391. {
  392. if (!isset($user))
  393. {
  394. $mem_modlink_info['user'] = $txp_user;
  395. }
  396. $res = submit_moderated_content('link', '', $mem_modlink_info['note'], $mem_modlink_info);
  397. }
  398. }
  399. $mem_modlink_info['result'] = $res;
  400. $thanks_form = @fetch_form($mem_form_thanks_form);
  401. if (!empty($thanks_form))
  402. {
  403. $out = parse($thanks_form);
  404. }
  405. // cleanup global
  406. unset($mem_modlink_info);
  407. return $out;
  408. }
  409. # --- END PLUGIN CODE ---
  410. ?>