materio_personalnotes.pages.inc 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <?php
  2. function materio_personalnotes_createeditnote($action = 'create', $src_nid = null, $note_nid = null){
  3. $debug = false;
  4. global $user;
  5. $rep = array();
  6. if(!$src_nid)
  7. drupal_json_output(array('error'=>"src_nid arguments needed"));
  8. $contenttype = 'note';
  9. // check if request is ajax, if not rediret
  10. if (!$debug && (!isset($_SERVER['HTTP_X_REQUESTED_WITH']) || strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) != 'xmlhttprequest')) {
  11. switch ($action) {
  12. case 'edit':
  13. drupal_goto('node/'.$src_nid.'/edit');
  14. break;
  15. default:
  16. drupal_goto('node/add/' . $contenttype . '?target_id='.$src_nid);
  17. break;
  18. }
  19. exit ;
  20. }
  21. module_load_include('inc', 'node', 'node.pages');
  22. switch ($action) {
  23. case 'edit':
  24. if(!$note_nid){
  25. return "For edit action, note_nid is needed";
  26. }else{
  27. $note = node_load($note_nid);
  28. $form = drupal_get_form('note_node_form', $note);
  29. }
  30. break;
  31. default: // create
  32. $new_note = (object) array(
  33. 'uid' => $user->uid,
  34. 'name' => (isset($user->name) ? $user->name : ''),
  35. 'type' => 'note',
  36. 'language' => LANGUAGE_NONE,
  37. 'field_target_content_nid' => array(
  38. 'und'=>array(
  39. array(
  40. 'value'=>$src_nid
  41. )
  42. )
  43. ),
  44. 'field_personal_note' => array(
  45. 'und'=>array(
  46. array(
  47. 'value'=>""
  48. )
  49. )
  50. )
  51. );
  52. node_object_prepare($new_note);
  53. node_save($new_note);
  54. $note_nid = $new_note->nid;
  55. $rep['new_note_nid'] = $note_nid;
  56. $form = drupal_get_form('note_node_form', $new_note);
  57. // pre-fill the field_target_content_nid with src_nid argument
  58. // $form['field_target_content_nid']['und'][0]['value']['#default_value'] = $src_id;
  59. // $form['field_target_content_nid']['und'][0]['value']['#type'] = 'hidden';
  60. break;
  61. }
  62. $form['note_nid'] = array(
  63. '#type'=>'hidden',
  64. '#value'=> $note_nid,
  65. '#name' => 'note_nid',
  66. );
  67. $rendered_form = drupal_render($form);
  68. $rep += array(
  69. // 'form'=>$form,
  70. 'rendered_form'=>$rendered_form
  71. );
  72. if ($debug) {
  73. dsm($rep, 'rep');
  74. return "debug display";
  75. }else{
  76. drupal_json_output($rep);
  77. }
  78. }
  79. function materio_personalnotes_savenote($note_nid = null){
  80. $debug = false;
  81. if(!$note_nid)
  82. drupal_json_output(array('error'=>"note_nid arguments needed"));
  83. $node = node_load($note_nid);
  84. // $note->field_target_content_nid
  85. $node->field_personal_note[$node->language][0]['value'] = $_POST['content'];
  86. node_save($node);
  87. if($debug){
  88. dsm($node, "note node");
  89. return 'debug display';
  90. }else{
  91. drupal_json_output(array('status'=>'note node saved'));
  92. }
  93. }