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