materio_flag.pages.inc 13 KB

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