materio_flag.pages.inc 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497
  1. <?php
  2. /**
  3. * materio_flag_refresh_blocks($flag)
  4. *
  5. */
  6. function materio_flag_refresh_block($flag){
  7. switch ($flag) {
  8. case 'bookmarks':
  9. $block_name = 'materio_flag_mybookmarks';
  10. break;
  11. case 'lists':
  12. $block_name = 'materio_flag_mylists';
  13. $block_nav_name = 'materio_flag_mylists_nav';
  14. break;
  15. }
  16. if(!isset($block_name))
  17. return;
  18. $rep = array("flag"=>$flag);
  19. $block = block_load('materio_flag', $block_name);
  20. $block_content = _block_render_blocks(array($block));
  21. $build = _block_get_renderable_array($block_content);
  22. $block_rendered = drupal_render($build);
  23. $rep['block'] = $block_rendered;
  24. if(isset($block_nav_name)){
  25. $block = block_load('materio_flag', $block_nav_name);
  26. $block_content = _block_render_blocks(array($block));
  27. $build = _block_get_renderable_array($block_content);
  28. $block_rendered = drupal_render($build);
  29. $rep['block_nav'] = $block_rendered;
  30. }
  31. drupal_json_output($rep);
  32. }
  33. /**
  34. * materio_flag_createlist($type)
  35. *
  36. */
  37. function materio_flag_createlist_form($type){
  38. $debug = false;
  39. $path = 'flag-lists/add/' . $type;
  40. // check if request is ajax, if not rediret
  41. if (!$debug && (!isset($_SERVER['HTTP_X_REQUESTED_WITH']) || strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) != 'xmlhttprequest')) {
  42. drupal_goto('flag-lists/add/' . $type);
  43. exit ;
  44. }
  45. // menu_set_active_item($path);
  46. // $return = menu_execute_active_handler($path, FALSE);
  47. // if (is_int($return)) {
  48. // switch ($return) {
  49. // case MENU_NOT_FOUND :
  50. // drupal_add_http_header('Status', '404 Not Found');
  51. // break;
  52. // case MENU_ACCESS_DENIED :
  53. // drupal_add_http_header('Status', '403 Forbidden');
  54. // break;
  55. // case MENU_SITE_OFFLINE :
  56. // drupal_add_http_header('Status', '503 Service unavailable');
  57. // break;
  58. // }
  59. // } elseif (isset($return)) {
  60. // $form_state['build_info']['args'] = array(NULL, 'breve');
  61. // $f = drupal_get_form('flag_lists_form', $form_state);
  62. // dsm($f);
  63. $form = array(
  64. '#type' => 'form',
  65. '#id' => 'materio-flag-create-list-form',
  66. );
  67. $form['type'] = array(
  68. '#type' => 'hidden',
  69. '#value' => $type,
  70. '#name' => 'type',
  71. );
  72. $form['listname'] = array(
  73. '#type' => 'textfield',
  74. '#title' => t('Create new @name', array('@name'=>variable_get('flag_lists_name', 'list'))),
  75. '#default_value' => '',
  76. // '#size' => 100,
  77. '#maxlength' => 255,
  78. '#name' => 'flag-lists-name',
  79. '#description' => t('A short, descriptive title for this Folder list. Limit to 255 characters.'),
  80. );
  81. $form['actions'] = array(
  82. '#prefix' => '<div class="actions">',
  83. '#suffix' => '</div>',
  84. );
  85. $form['actions']['create'] = array(
  86. '#type' => 'submit',
  87. '#value' => t('create'),
  88. '#name' => 'create',
  89. );
  90. $form['actions']['cancel'] = array(
  91. '#type' => 'button',
  92. '#value' => t('cancel'),
  93. '#name' => 'cancel',
  94. );
  95. // dsm($form, 'form');
  96. $return = drupal_render($form);
  97. $rep = array(
  98. 'rendered_form'=>$return,
  99. );
  100. if ($debug) {
  101. dsm($rep, 'rep');
  102. return "debug display";
  103. }else{
  104. drupal_json_output($rep);
  105. }
  106. }
  107. /**
  108. * materio_flag_editlist_form()
  109. */
  110. function materio_flag_editlist_form($type, $fid){
  111. $debug = false;
  112. $path = 'flags/lists/edit/' . $fid;
  113. // check if request is ajax, if not rediret
  114. if (!$debug && (!isset($_SERVER['HTTP_X_REQUESTED_WITH']) || strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) != 'xmlhttprequest')) {
  115. drupal_goto($path);
  116. exit ;
  117. }
  118. // If name is numeric, then we have the fid, so get the name.
  119. if (is_numeric($fid)) {
  120. $name = db_select('flag_lists_flags', 'f')
  121. ->fields('f', array('name'))
  122. ->condition('fid', $fid)
  123. ->execute()
  124. ->fetchField();
  125. }
  126. if(flag_lists_is_owner('edit', $fid)){
  127. $flag = flag_lists_get_flag($name);
  128. $form = array(
  129. '#type' => 'form',
  130. '#id' => 'materio-flag-edit-list-form',
  131. );
  132. $form['type'] = array(
  133. '#type' => 'hidden',
  134. '#value' => $type,
  135. '#name' => 'type',
  136. );
  137. $form['fid'] = array(
  138. '#type' => 'hidden',
  139. '#value' => $fid,
  140. '#name' => 'fid',
  141. );
  142. $form['name'] = array(
  143. '#type' => 'hidden',
  144. '#value' => $name,
  145. '#name' => 'name',
  146. );
  147. $form['listtitle'] = array(
  148. '#type' => 'textfield',
  149. '#title' => t('Edit this '.variable_get('flag_lists_name', 'list')),
  150. '#value' => $flag->title,
  151. // '#size' => 100,
  152. '#maxlength' => 255,
  153. '#name' => 'flag-lists-title',
  154. '#description' => t('A short, descriptive title for this Folder list. Limit to 255 characters.'),
  155. );
  156. $form['actions'] = array(
  157. '#prefix' => '<div class="actions">',
  158. '#suffix' => '</div>',
  159. );
  160. $form['actions']['save'] = array(
  161. '#type' => 'submit',
  162. '#value' => t('save'),
  163. '#name' => 'save',
  164. );
  165. $form['actions']['delete'] = array(
  166. '#type' => 'submit',
  167. '#value' => t('delete'),
  168. '#name' => 'delete',
  169. );
  170. $form['actions']['cancel'] = array(
  171. '#type' => 'button',
  172. '#value' => t('cancel'),
  173. '#name' => 'cancel',
  174. );
  175. // dsm($form, 'form');
  176. $return = drupal_render($form);
  177. $rep = array(
  178. 'rendered_form'=>$return,
  179. );
  180. }else{
  181. $rep = array(
  182. 'status' => 'error',
  183. 'message' => t('you do not have rights to edit this '.variable_get('flag_lists_name', 'list')),
  184. );
  185. }
  186. if ($debug) {
  187. dsm($rep, 'rep');
  188. return "debug display";
  189. }else{
  190. drupal_json_output($rep);
  191. }
  192. }
  193. function materio_flag_edit_list($fid, $name, $title){
  194. $path = 'flags/lists/edit/' . $fid;
  195. // check if request is ajax, if not rediret
  196. if (!isset($_SERVER['HTTP_X_REQUESTED_WITH']) || strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) != 'xmlhttprequest') {
  197. drupal_goto($path);
  198. exit ;
  199. }
  200. if(!flag_lists_is_owner('edit', $fid)){
  201. $rep = array(
  202. 'error' => true,
  203. 'message' => t('You have not enough rights to edit this @name.', array('@name' => variable_get('flag_lists_name', t('list'))))
  204. );
  205. }else if (flag_lists_title_exists($title, 'materiau') || flag_lists_title_exists($title, 'breve')){
  206. $rep = array(
  207. 'error' => true,
  208. 'message' => t('You already have a @name with this name.', array('@name' => variable_get('flag_lists_name', t('list'))))
  209. );
  210. }else{
  211. $flag = flag_lists_get_flag($name);
  212. $flag->title = $title;
  213. flag_lists_set_messages($flag);
  214. flag_lists_save($flag);
  215. _flag_lists_clear_cache();
  216. $rep = array(
  217. 'status' => 'saved',
  218. 'listname' => $name,
  219. 'listtitle' => $title,
  220. );
  221. }
  222. drupal_json_output($rep);
  223. }
  224. function materio_flag_delete_list($fid){
  225. $path = 'flags/lists/delete/' . $fid;
  226. // check if request is ajax, if not rediret
  227. if (!isset($_SERVER['HTTP_X_REQUESTED_WITH']) || strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) != 'xmlhttprequest') {
  228. drupal_goto($path);
  229. exit ;
  230. }
  231. if(!flag_lists_is_owner('delete', $fid)){
  232. $rep = array(
  233. 'error' => true,
  234. 'message' => t('You have not enough rights to delete this @name.', array('@name' => variable_get('flag_lists_name', t('list'))))
  235. );
  236. }else{
  237. // If name is numeric, then we have the fid, so get the name.
  238. if (is_numeric($fid)) {
  239. $name = db_select('flag_lists_flags', 'f')
  240. ->fields('f', array('name'))
  241. ->condition('fid', $fid)
  242. ->execute()
  243. ->fetchField();
  244. }
  245. $flag = flag_lists_get_flag($name);
  246. flag_lists_fl_delete($flag);
  247. $rep = array(
  248. 'status' => 'saved',
  249. );
  250. }
  251. drupal_json_output($rep);
  252. }
  253. function materio_flag_nodelinks(){
  254. $nids = explode(';', $_REQUEST['nids']);
  255. if(count($nids)){
  256. foreach ($nids as $nid) {
  257. $node = node_load($nid);
  258. $lks = render(materio_flag_get_entity_links($node, 'node'));
  259. $links[$nid] = $lks;
  260. }
  261. $rep = array(
  262. "status" => 1,
  263. "nids" => $nids,
  264. 'links' => $links,
  265. );
  266. }else{
  267. $rep['status'] = 0;
  268. }
  269. drupal_json_output($rep);
  270. }
  271. function materio_flag_user_bookmarks(){
  272. global $user;
  273. drupal_set_title(t('My bookmarks'), PASS_THROUGH);
  274. $flaged = flag_get_user_flags('node');
  275. // dsm($flaged, 'flaged');
  276. $flag = flag_get_flag('bookmarks');
  277. $flaged_content = $flaged['bookmarks'];
  278. // dsm($flaged_content, 'flaged_content');
  279. $viewmode = isset($user->data['materiosearchapi_viewmode']) ? $user->data['materiosearchapi_viewmode'] : variable_get('defaultviewmode', 'full');
  280. $limit = variable_get($viewmode.'_limite', '10');
  281. $offset = pager_find_page() * $limit;
  282. $count = count($flaged_content);
  283. $max = $offset+$limit > $count ? $count : $offset+$limit;
  284. $i = 0;
  285. foreach ($flaged_content as $id => $item) {
  286. if($i >= $offset){
  287. if($i < $max){
  288. if($item->entity_type == 'node'){
  289. $items[] = node_load($id);
  290. }
  291. }else{
  292. break;
  293. }
  294. }
  295. }
  296. $ret['#items'] = $items;
  297. $ret['#theme'] = 'materio_flag_mylists_list';
  298. $ret['#view_mode'] = $viewmode;
  299. $ret['#fid'] = $flag->fid;
  300. $ret['#count'] = $count;
  301. $ret['#name'] = variable_get('flag_lists_name', 'list');
  302. $ret['#title'] = $flag->title;
  303. pager_default_initialize($count, $limit);
  304. $ret['#pager'] = theme('pager');
  305. drupal_add_js(drupal_get_path('module', 'materio_flag').'/js/materio_flag-ck.js');
  306. return $ret;
  307. }
  308. function materio_flag_user_lists($fid){
  309. global $user;
  310. $flag = flag_lists_get_flag($fid);
  311. // dsm($flag, 'flag');
  312. drupal_set_title(_materio_flag_get_listpagetitle($flag), PASS_THROUGH);
  313. $flaged_content = flag_lists_get_flagged_content($fid, $user->uid);
  314. // dsm($flaged_content, 'flaged_content');
  315. $viewmode = isset($user->data['materiosearchapi_viewmode']) ? $user->data['materiosearchapi_viewmode'] : variable_get('defaultviewmode', 'full');
  316. $limit = variable_get($viewmode.'_limite', '10');
  317. $offset = pager_find_page() * $limit;
  318. $count = count($flaged_content);
  319. $max = $offset+$limit > $count ? $count : $offset+$limit;
  320. for ($i=$offset; $i < $max; $i++) {
  321. if($flaged_content[$i]->entity_type == 'node'){
  322. $items[] = node_load($flaged_content[$i]->entity_id);
  323. }
  324. }
  325. $ret['#items'] = $items;
  326. $ret['#theme'] = 'materio_flag_mylists_list';
  327. $ret['#view_mode'] = $viewmode;
  328. $ret['#fid'] = $fid;
  329. $ret['#count'] = $count;
  330. $ret['#name'] = variable_get('flag_lists_name', 'list');
  331. $ret['#title'] = $flag->title;
  332. pager_default_initialize($count, $limit);
  333. $ret['#pager'] = theme('pager');
  334. return $ret;
  335. }
  336. function materio_flag_ajax_list($fid, $page = 0){
  337. $debug = false;
  338. $_GET['page'] = $page;
  339. if($fid == 1){
  340. $list_path = $path = 'bookmarks';
  341. }else{
  342. $list_path = 'lists';
  343. $path = $list_path.'/'.$fid;
  344. }
  345. // check if request is ajax, if not rediret to search_api_page page with right keys
  346. if (!$debug && (!isset($_SERVER['HTTP_X_REQUESTED_WITH']) || strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) != 'xmlhttprequest')) {
  347. drupal_goto($path, array('query'=>array('page'=>$page)), 301);
  348. exit;
  349. }
  350. if(isset($_GET['current_path'])){
  351. // dsm($_GET['current_path'], '$_GET[current_path]');
  352. // url() generates the prefix using hook_url_outbound_alter(). Instead of
  353. // running the hook_url_outbound_alter() again here, extract the prefix
  354. // from url().
  355. url('', array('prefix' => &$prefix));
  356. $cur_path = str_replace($base_url.base_path().$prefix, '', $_GET['current_path']);
  357. // dsm($cur_path, 'cur_path');
  358. $cur_is_list_path = strpos($cur_path, $list_path);
  359. // dsm($matches, '$matches');
  360. if($cur_is_list_path === false){
  361. $rep = array(
  362. "redirect" => $base_url.base_path().$prefix.$path,
  363. );
  364. drupal_json_output($rep);
  365. exit;
  366. }
  367. }
  368. // get results
  369. menu_set_active_item($path);
  370. $return = menu_execute_active_handler($path, FALSE);
  371. dsm($return, '$return');
  372. if (is_int($return)) {
  373. switch ($return) {
  374. case MENU_NOT_FOUND :
  375. drupal_add_http_header('Status', '404 Not Found');
  376. break;
  377. case MENU_ACCESS_DENIED :
  378. drupal_add_http_header('Status', '403 Forbidden');
  379. break;
  380. case MENU_SITE_OFFLINE :
  381. drupal_add_http_header('Status', '503 Service unavailable');
  382. break;
  383. }
  384. } elseif (isset($return)) {
  385. if (is_array($return)) {
  386. $rendered = drupal_render($return);
  387. }
  388. $rep = array(
  389. 'rendered' => $rendered,
  390. 'path' => $path,
  391. 'title' => drupal_get_title(),
  392. );
  393. if ($debug) {
  394. dsm($rep, 'rep');
  395. return "debug display";
  396. }else{
  397. drupal_json_output($rep);
  398. }
  399. }
  400. }