materio_search_api.module 53 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638
  1. <?php
  2. /**
  3. * @file
  4. * This is the file description for Materiobasemod module.
  5. *
  6. * In this more verbose, multi-line description, you can specify what this
  7. * file does exactly. Make sure to wrap your documentation in column 78 so
  8. * that the file can be displayed nicely in default-sized consoles.
  9. */
  10. // define(MATERIO_SEARCH_API_RESULTS_PATH, 'explore');
  11. /**
  12. * Implements hook_permission().
  13. */
  14. function materio_search_api_permission() {
  15. return array(
  16. 'access to materio database' => array(
  17. 'title' => t('Access to materio database'),
  18. 'description' => t('Access to materio database.'),
  19. ),
  20. 'use materio search api' => array(
  21. 'title' => t('Use materio search api'),
  22. 'description' => t('Use materio search api.'),
  23. ),
  24. 'use materio search api for breves' => array(
  25. 'title' => t('Use materio search api for breves'),
  26. 'description' => t('Use materio search api for breves.'),
  27. ),
  28. 'use materio search api autocomplete' => array(
  29. 'title' => t('Use materio search api autocomplete'),
  30. 'description' => t('Use materio search api autocomplete.'),
  31. ),
  32. 'use materio search api viewmode selection' => array(
  33. 'title' => t('Use materio search api viewmode selection'),
  34. 'description' => t('Use materio search api viewmode selection.'),
  35. ),
  36. 'administer materio_search_api' => array(
  37. 'title' => t('administer Materio search api'),
  38. 'description' => t('Administer materio search api.'),
  39. ),
  40. 'use materio search api filters' => array(
  41. 'title' => t('use Materio search api filters'),
  42. 'description' => t('Use materio search api filters.'),
  43. ),
  44. 'use materio advanced search' => array(
  45. 'title' => t('Use materio advanced search'),
  46. 'description' => t('Use materio advanced search.'),
  47. ),
  48. );
  49. }
  50. /**
  51. * Implements hook_menu().
  52. */
  53. function materio_search_api_menu() {
  54. $items = array();
  55. $base = array(
  56. 'type' => MENU_CALLBACK,
  57. 'file' => 'materio_search_api.pages.inc',
  58. );
  59. $items['admin/config/search/search_api/materiosearchapi'] = array(
  60. 'title' => 'Materio Search Api',
  61. 'page callback' => 'drupal_get_form',
  62. 'page arguments' => array('materio_search_api_settings'),
  63. 'access arguments' => array('administer materio_search_api'),
  64. 'file' => 'materio_search_api.admin.inc',
  65. 'type' => MENU_LOCAL_TASK,
  66. );
  67. $items['materiosearchapi/autocomplete/dbselect'] = $base+array(
  68. 'access arguments' => array('use materio search api autocomplete'),
  69. 'page callback' => 'materio_search_api_autocomplete_dbselect',
  70. );
  71. $items['materiosearchapi/autocomplete/searchapi'] = $base+array(
  72. 'access arguments' => array('use materio search api autocomplete'),
  73. 'page callback' => 'materio_search_api_autocomplete_searchapi',
  74. );
  75. $items['explore'] = $base+array(
  76. // 'access arguments' => array('use materio search api'),
  77. 'access callback' => 'materio_search_api_access_search',
  78. 'page callback' => 'materio_search_api_results_search',
  79. 'title' => t('Explore'),
  80. //'page argument' => array(1,2,3),
  81. );
  82. $items['actuality'] = $base+array(
  83. // 'access arguments' => array(),
  84. 'page callback' => 'materio_search_api_actuality',
  85. // 'page argument' => array(),
  86. 'access callback' => TRUE,
  87. );
  88. $items['materiosearchapi/viewmode/change'] = $base+array(
  89. 'access arguments' => array('use materio search api viewmode selection'),
  90. 'page callback' => 'materio_search_api_viewmode_change',
  91. 'page argument' => array(3),
  92. );
  93. return $items;
  94. }
  95. function materio_search_api_access_search(){
  96. return true;
  97. // return user_access('use materio search api for breves') || user_access('use materio search api');
  98. }
  99. /**
  100. * - - - - - - - - - - - - SOLR - - - - - - - - - - - -
  101. */
  102. /**
  103. * Implements hook_search_api_data_type_info().
  104. *
  105. * Declare our new type to Search API so it can be selected for a field.
  106. */
  107. function materio_search_api_search_api_data_type_info() {
  108. return array(
  109. 'edge_n2_kw_text' => array(
  110. 'name' => t('Fulltext (partial matching)'),
  111. 'fallback' => 'text',
  112. 'prefix' => 'tem',
  113. 'always multiValued' => TRUE,
  114. ),
  115. 'edge_n2_kw_mapped_text' => array(
  116. 'name' => t('Fulltext without accents (partial matching)'),
  117. 'fallback' => 'text',
  118. 'prefix' => 'temmap',
  119. 'always multiValued' => TRUE,
  120. ),
  121. // 'term_tid' => array(
  122. // 'name' => t('Taxonomy term tid'),
  123. // 'fallback' => 'integer',
  124. // 'prefix' => 'im',
  125. // 'always multiValued' => TRUE,
  126. // ),
  127. );
  128. }
  129. /**
  130. * Implements hook_search_api_solr_dynamic_field_info().
  131. *
  132. * Tell Search API Solr how to index our new data type.
  133. */
  134. function materio_search_api_search_api_solr_dynamic_field_info() {
  135. return array(
  136. 'edge_n2_kw_text' => array(
  137. 'prefix' => 'tem',
  138. 'always multiValued' => TRUE,
  139. ),
  140. 'edge_n2_kw_mapped_text' => array(
  141. 'prefix' => 'temmap',
  142. 'always multiValued' => TRUE,
  143. ),
  144. // 'term_tid' => array(
  145. // 'prefix' => 'im',
  146. // 'always multiValued' => TRUE,
  147. // ),
  148. );
  149. }
  150. /**
  151. * hook_entity_property_info_alter().
  152. *
  153. * define own fields
  154. * - main taxonomy terms, the purpose is to be able to attribute differente boost on serach api depending on term order
  155. * - companies infos (address) : retrieve for materiau content type the companie info from the tode node
  156. *
  157. */
  158. function materio_search_api_entity_property_info_alter(&$info){
  159. // dsm($info, 'hook_entity_property_info_alter | info');
  160. // watchdog('materio solr', 'materio_search_api_entity_property_info_alter', array());
  161. // NODE PROPERTIES
  162. $node_props = &$info['node']['properties'];
  163. $fields = msa_get_custom_onto_taxo_searchable_fields();
  164. foreach ($fields['termname'] as $field) {
  165. $node_props[$field['name']] = array(
  166. 'type'=>$field['type'],
  167. 'label'=> $field['label'],
  168. 'getter callback'=>$field['getter']
  169. );
  170. }
  171. foreach ($fields['tid'] as $field) {
  172. $node_props[$field['name']] = array(
  173. 'type'=>$field['type'],
  174. 'label'=> $field['label'],
  175. 'getter callback'=>$field['getter']
  176. );
  177. }
  178. // for ($i=1; $i <= 5 ; $i++) {
  179. // $node_props['materio_search_api_onthologie_term_'.$i.'_text'] = array(
  180. // 'type'=>'text',
  181. // 'label'=> t('Main onthologie term '.$i. ' as text (+ synonyms)'),
  182. // 'getter callback'=>'materio_search_api_get_onthologie_term_'.$i.'_text',
  183. // );
  184. // $node_props['materio_search_api_onthologie_term_'.$i.'_tid'] = array(
  185. // 'type'=>'text',
  186. // 'label'=> t('Main onthologie term '.$i. ' as tid'),
  187. // 'getter callback'=>'materio_search_api_get_onthologie_term_'.$i.'_tid',
  188. // );
  189. // }
  190. //
  191. // $node_props['materio_search_api_onthologie_term_others_text'] = array(
  192. // 'type'=>'list<text>',
  193. // 'label'=> t('Others onthologie terms as text (+ synonyms)'),
  194. // 'getter callback'=>'materio_search_api_get_taxonomy_terms_others_text',
  195. // );
  196. // $node_props['materio_search_api_onthologie_term_others_tid'] = array(
  197. // 'type'=>'list<text>',
  198. // 'label'=> t('Others onthologie terms as tid'),
  199. // 'getter callback'=>'materio_search_api_get_taxonomy_terms_others_tid',
  200. // );
  201. $node_props['materio_search_api_taglibres_text'] = array(
  202. 'type'=>'list<text>',
  203. 'label'=> t('Tag libres terms as text (+ synonyms)'),
  204. 'getter callback'=>'materio_search_api_get_taglibres_terms_text',
  205. );
  206. $node_materiau_props = &$info['node']['bundles']['materiau']['properties'];
  207. $node_materiau_props['materio_search_api_node_propertie_companie'] = array(
  208. 'type'=>'list<text>',
  209. 'label'=> t('Manufacturers and distributors companies localised (dont addresse)'),
  210. 'getter callback'=>'materio_search_api_get_node_propertie_companie',
  211. );
  212. // TAXONOMIE TERMS PROPERTIES
  213. $term_props = &$info['taxonomy_term']['properties'];
  214. $term_props['materio_search_api_term_property_dup_name'] = array(
  215. 'label' => t("Term name field dup"),
  216. 'description' => t("Duplicate the term name field to apply different filter in solr (remove accents for instance)"),
  217. 'type' => 'text',
  218. 'getter callback' => 'materio_search_api_term_property_dup_name',
  219. );
  220. // $term_props['materio_search_api_term_property_name_translated'] = array(
  221. // 'label' => t("Term name translated"),
  222. // 'description' => t(""),
  223. // 'type' => 'text',
  224. // 'getter callback' => 'materio_search_api_term_property_name_translated',
  225. // );
  226. }
  227. function msa_get_custom_onto_taxo_searchable_fields(){
  228. for ($i=1; $i <= 5 ; $i++) {
  229. $fields_name['termname'][] = array(
  230. 'type' => 'text',
  231. 'name' => 'materio_search_api_onthologie_term_'.$i.'_text',
  232. 'label' => t('Main onthologie term '.$i. ' as text (+ synonyms)'),
  233. 'getter' => 'materio_search_api_get_onthologie_term_'.$i.'_text'
  234. );
  235. $fields_name['tid'][] = array(
  236. 'type' => 'text',
  237. 'name' => 'materio_search_api_onthologie_term_'.$i.'_tid',
  238. 'label' => t('Main onthologie term '.$i. ' as tid'),
  239. 'getter' => 'materio_search_api_get_onthologie_term_'.$i.'_tid'
  240. );
  241. }
  242. $fields_name['termname'][] = array(
  243. 'type'=>'list<text>',
  244. 'name' => 'materio_search_api_onthologie_term_others_text',
  245. 'label' => t('Others onthologie terms as text (+ synonyms)'),
  246. 'getter' => 'materio_search_api_get_taxonomy_terms_others_text'
  247. );
  248. $fields_name['tid'][] = array(
  249. 'type'=>'list<text>',
  250. 'name' => 'materio_search_api_onthologie_term_others_tid',
  251. 'label' => t('Others onthologie terms as tid'),
  252. 'getter' => 'materio_search_api_get_taxonomy_terms_others_tid'
  253. );
  254. return $fields_name;
  255. }
  256. /**
  257. * taxonomy propoerties as text + synonymes
  258. */
  259. function materio_search_api_get_onthologie_term_1_text($item){
  260. // dsm($item, 'item');
  261. return materio_search_api_get_taxo_term_field_text($item, "field_onthologie", 0);
  262. }
  263. function materio_search_api_get_onthologie_term_2_text($item){
  264. // dsm($item, 'item');
  265. return materio_search_api_get_taxo_term_field_text($item, "field_onthologie", 1);
  266. }
  267. function materio_search_api_get_onthologie_term_3_text($item){
  268. // dsm($item, 'item');
  269. return materio_search_api_get_taxo_term_field_text($item, "field_onthologie", 2);
  270. }
  271. function materio_search_api_get_onthologie_term_4_text($item){
  272. // dsm($item, 'item');
  273. return materio_search_api_get_taxo_term_field_text($item, "field_onthologie", 3);
  274. }
  275. function materio_search_api_get_onthologie_term_5_text($item){
  276. // dsm($item, 'item');
  277. return materio_search_api_get_taxo_term_field_text($item, "field_onthologie", 4);
  278. }
  279. function materio_search_api_get_taxonomy_terms_others_text($item){
  280. // dsm($item, 'item');
  281. $delta = 5;
  282. $terms = array();
  283. while( isset($item->field_onthologie['und'][$delta]) ){
  284. $terms[] = materio_search_api_get_taxo_term_field_text($item, "field_onthologie", $delta);
  285. $delta++;
  286. }
  287. return $terms;
  288. }
  289. function materio_search_api_get_taglibres_terms_text($item){
  290. // dsm($item, 'item');
  291. $delta = 0;
  292. $terms = array();
  293. while( isset($item->field_tags_libres['und'][$delta]) ){
  294. $terms[] = materio_search_api_get_taxo_term_field_text($item, "field_tags_libres", $delta);
  295. $delta++;
  296. }
  297. return $terms;
  298. }
  299. function materio_search_api_get_taxo_term_field_text($item, $field_name, $delta){
  300. // dsm($item, 'item');
  301. // dsm($delta, 'delta');
  302. if(isset($item->{$field_name}['und'][$delta])){
  303. // print '** item **'."\n";
  304. // print_r($item);
  305. $term = taxonomy_term_load($item->{$field_name}['und'][$delta]['tid']);
  306. // use entity metadata wrappers as they are SMART
  307. $wrapper = entity_metadata_wrapper('taxonomy_term', $term);
  308. // print '** wrapper **'."\n";
  309. // print_r($wrapper);
  310. // print "\n";
  311. $keywords[] = $wrapper->language($item->language)->name_field->raw();
  312. // print '** term_name **'."\n";
  313. // print_r($term->name);
  314. // print "\n";
  315. // print_r($term_name);
  316. // print "\n";
  317. $synonymes = array();
  318. foreach ($wrapper->language($item->language)->synonyms_synonym->value() as $index => $synonym) {
  319. // print '** synonym **'."\n";
  320. // print_r($synonym);
  321. // print "\n";
  322. $keywords[] = $synonym;
  323. }
  324. return implode(" ", $keywords);
  325. }
  326. return null;
  327. }
  328. /**
  329. * taxonomy properties as tid
  330. */
  331. function materio_search_api_get_onthologie_term_1_tid($item){
  332. // dsm($item, 'item');
  333. return materio_search_api_get_taxo_term_field_tid($item, "field_onthologie", 0);
  334. }
  335. function materio_search_api_get_onthologie_term_2_tid($item){
  336. // dsm($item, 'item');
  337. return materio_search_api_get_taxo_term_field_tid($item, "field_onthologie", 1);
  338. }
  339. function materio_search_api_get_onthologie_term_3_tid($item){
  340. // dsm($item, 'item');
  341. return materio_search_api_get_taxo_term_field_tid($item, "field_onthologie", 2);
  342. }
  343. function materio_search_api_get_onthologie_term_4_tid($item){
  344. // dsm($item, 'item');
  345. return materio_search_api_get_taxo_term_field_tid($item, "field_onthologie", 3);
  346. }
  347. function materio_search_api_get_onthologie_term_5_tid($item){
  348. // dsm($item, 'item');
  349. return materio_search_api_get_taxo_term_field_tid($item, "field_onthologie", 4);
  350. }
  351. function materio_search_api_get_taxonomy_terms_others_tid($item){
  352. // dsm($item, 'item');
  353. $delta = 5;
  354. $terms = array();
  355. while( isset($item->field_onthologie['und'][$delta]) ){
  356. $terms[] = materio_search_api_get_taxo_term_field_tid($item, "field_onthologie", $delta);
  357. $delta++;
  358. }
  359. return $terms;
  360. }
  361. function materio_search_api_get_taxo_term_field_tid($item, $field_name, $delta){
  362. // dsm($item, 'item');
  363. // dsm($delta, 'delta');
  364. $tid = null;
  365. if(isset($item->{$field_name}['und'][$delta]))
  366. $tid = $item->{$field_name}['und'][$delta]['tid'];
  367. // print 'tid = '.$tid."\n";
  368. return $tid;
  369. }
  370. /**
  371. *
  372. */
  373. function materio_search_api_get_node_propertie_companie($item){
  374. // dsm($item, '$item');
  375. $lang = "fr";//$item->language;
  376. $languages = language_list();
  377. global $language;
  378. $language = $languages[$lang];
  379. $cies = array();
  380. $cie_fields = array("field_company_fab", "field_company_distrib");
  381. foreach ($cie_fields as $cie_field_name) {
  382. $delta = 0;
  383. while( isset($item->{$cie_field_name}['und'][$delta]) ){
  384. $tid = $item->{$cie_field_name}['und'][$delta]['tid'];
  385. $cie_term = taxonomy_term_load($tid);
  386. if($cie_node = company_get_tode_node($cie_term)){
  387. $cie_node->language = $lang;
  388. $cie_view = node_view($cie_node, "full", $lang);
  389. $cie_rendered = rip_tags(drupal_render($cie_view));
  390. // dsm($cie_rendered, 'cie_rendered');
  391. $cies[] = $cie_rendered;
  392. }
  393. $delta++;
  394. }
  395. }
  396. return $cies;
  397. }
  398. function company_get_tode_node($term){
  399. if(module_exists('tode'))
  400. if( $entitys = tode_get_nids_from_term($term))
  401. if(isset($entitys['node']))
  402. foreach ($entitys['node'] as $nid => $n)
  403. return node_load($nid);
  404. return false;
  405. }
  406. function rip_tags($string) {
  407. // ----- remove HTML TAGs -----
  408. $string = preg_replace ('/<[^>]*>/', ' ', $string);
  409. // ----- remove control characters -----
  410. $string = str_replace("\r", '', $string); // --- replace with empty space
  411. $string = str_replace("\n", ' ', $string); // --- replace with space
  412. $string = str_replace("\t", ' ', $string); // --- replace with space
  413. // $string = str_replace("&nbsp;", ' ', $string); // --- replace with space
  414. // $string = str_replace("&#039;", '\'', $string); // --- replace with space
  415. // ----- remove multiple spaces -----
  416. $string = trim(preg_replace('/ {2,}/', ' ', $string));
  417. // ----- remove html entities
  418. preg_match_all('/&[^;]+;/', $string, $entities);
  419. foreach ($entities[0] as $entity) {
  420. $string = str_replace($entity, mb_convert_encoding($entity, 'UTF-8', 'HTML-ENTITIES'), $string);
  421. }
  422. return $string;
  423. }
  424. /**
  425. * Taxonomy terms properties
  426. */
  427. function materio_search_api_term_property_dup_name($term){
  428. $lang = $term->language;
  429. return $term->name_field[$lang][0]['value'];
  430. }
  431. // function materio_search_api_term_property_name_translated($term){
  432. // if($term->vid == 4){
  433. // print_r(get_defined_vars());
  434. //
  435. // // print_r($term);
  436. //
  437. // $trans = module_exists('i18n_taxonomy')
  438. // ? i18n_taxonomy_term_name($term, 'fr')
  439. // : $term->name;
  440. //
  441. // print_r($trans);
  442. // print "\n";
  443. // }
  444. // }
  445. // ____ __ __
  446. // / __ )/ /___ _____/ /_______
  447. // / __ / / __ \/ ___/ //_/ ___/
  448. // / /_/ / / /_/ / /__/ ,< (__ )
  449. // /_____/_/\____/\___/_/|_/____/
  450. /**
  451. * Implements hook_block_info().
  452. */
  453. function materio_search_api_block_info() {
  454. $blocks['materio_search_api_search'] = array(
  455. 'info' => t('Materio search api search'),
  456. 'cache' => DRUPAL_NO_CACHE
  457. );
  458. $blocks['mo_searchapi_advanced_search'] = array(
  459. 'info' => t('Materio search api advanced search'),
  460. 'cache' => DRUPAL_NO_CACHE
  461. );
  462. # NOT USED
  463. $blocks['materio_search_api_viewmode'] = array(
  464. 'info' => t('Materio search api view mode selection'),
  465. 'cache' => DRUPAL_NO_CACHE
  466. );
  467. // $blocks['materio_search_api_filters'] = array(
  468. // 'info' => t('Materio search api filters'),
  469. // 'cache' => DRUPAL_NO_CACHE
  470. // );
  471. return $blocks;
  472. }
  473. /**
  474. * Implements hook_block_view().
  475. */
  476. function materio_search_api_block_view($delta = '') {
  477. // This example comes from node.module. Note that you can also return a
  478. // renderable array rather than rendered HTML for 'content'.
  479. $block = array();
  480. switch ($delta) {
  481. case 'materio_search_api_search':
  482. if (user_access('use materio search api') && user_access('use materio advanced search')) {
  483. $block['subject'] = t('Search');
  484. $block['content'] = theme('materio_search_api_double_search_block', array());
  485. }else if (user_access('use materio search api') || user_access('use materio search api for breves')) {
  486. $block['subject'] = t('Search');
  487. $block['content'] = theme('materio_search_api_search_block', array());
  488. }
  489. break;
  490. case 'mo_searchapi_advanced_search':
  491. if (user_access('use materio advanced search')) {
  492. $block['subject'] = t('Advanced Search');
  493. $block['content'] = theme('materio_search_api_advanced_search_block', array());
  494. }
  495. break;
  496. case 'materio_search_api_viewmode':
  497. // restrict display on node page for materiau and breve
  498. $block_is_visible = true;
  499. $q = parse_url($_GET['q']);
  500. $parts = explode('/', $q['path']);
  501. if($parts[0] == "node"){
  502. $node = node_load($parts[1]);
  503. $block_is_visible = in_array($node->type, array('materiau','breve')) ? true : false;
  504. }
  505. if (user_access('use materio search api viewmode selection') && $block_is_visible) {
  506. $block['subject'] = t('View mode');
  507. $block['content'] = theme('materio_search_api_select_viewmode_block', array());
  508. }
  509. break;
  510. // case 'materio_search_api_filters':
  511. // if (user_access('use materio search api filters')) {
  512. // $block['subject'] = t('Filters');
  513. // $block['content'] = theme('materio_search_api_filters_block', array());
  514. // }
  515. // break;
  516. }
  517. return $block;
  518. }
  519. /**
  520. * Implements hook_entity_info_alter().
  521. */
  522. function materio_search_api_entity_info_alter(&$entity_info) {
  523. $entity_info['node']['view modes']['cardsmall'] = array(
  524. 'label' => t('Small cards'),
  525. 'custom settings' => TRUE,
  526. );
  527. $entity_info['node']['view modes']['cardmedium'] = array(
  528. 'label' => t('Medium cards'),
  529. 'custom settings' => TRUE,
  530. );
  531. $entity_info['node']['view modes']['cardbig'] = array(
  532. 'label' => t('Big cards'),
  533. 'custom settings' => TRUE,
  534. );
  535. $entity_info['node']['view modes']['cardfull'] = array(
  536. 'label' => t('Full cards'),
  537. 'custom settings' => TRUE,
  538. );
  539. }
  540. /**
  541. * Implements hook_node_view_alter().
  542. */
  543. function materio_search_api_node_view_alter(&$build) {
  544. $node = $build['#node'];
  545. if (arg(0) == 'node' && arg(1) == $node->nid) {
  546. // dsm($build, 'build');
  547. global $user;
  548. $viewmode = isset($user->data['materiosearchapi_viewmode']) ? $user->data['materiosearchapi_viewmode'] : variable_get('defaultviewmode', 'full');
  549. // dsm($viewmode, 'viewmode');
  550. $node = $build['#node'];
  551. if($build['#view_mode'] != $viewmode && $build['#view_mode'] != "bookmark" & in_array($node->type, array('breve', 'materiau'))){
  552. $build = node_view($node, $viewmode);
  553. }
  554. }
  555. }
  556. /**
  557. * Implements hook_node_view().
  558. */
  559. function materio_search_api_node_view($node, $view_mode, $langcode) {
  560. if (in_array($view_mode, array('cardsmall','cardmedium', 'cardbig', 'cardfull'))) {
  561. # PRINT 7.1
  562. // print_node_view($node, 'full');
  563. // print_pdf_node_view($node, 'full');
  564. # PRINT 7.2-beta
  565. // call for hook_node_view of print module to insert link
  566. print_ui_node_view($node, 'full');
  567. // dsm($node, 'node');
  568. }
  569. }
  570. // _____ __ ______
  571. // / ___/___ ____ ___________/ /_ / ____/___ _________ ___
  572. // \__ \/ _ \/ __ `/ ___/ ___/ __ \ / /_ / __ \/ ___/ __ `__ \
  573. // ___/ / __/ /_/ / / / /__/ / / / / __/ / /_/ / / / / / / / /
  574. // /____/\___/\__,_/_/ \___/_/ /_/ /_/ \____/_/ /_/ /_/ /_/
  575. /**
  576. * materiobase_search_form()
  577. */
  578. function materio_search_api_search_form($form, &$form_state){
  579. // dsm($form_state, 'form_state');
  580. // dsm($form, 'form');
  581. global $user;
  582. $form = array();
  583. $args = arg();
  584. $path = array_shift($args);
  585. if (isset($args[0]) && $args[0] == "filters") {
  586. $path .= '/'.array_shift($args);
  587. }
  588. $is_explore = $path == 'explore' || $path == 'explore/filters';
  589. $keys = implode('/', $args);
  590. if(user_access('use materio search api autocomplete')){ // use materio search api autocomplete | use materio search api filters
  591. $query = new EntityFieldQuery();
  592. $query->entityCondition('entity_type', 'node')
  593. ->entityCondition('bundle', 'materiau')
  594. ->propertyCondition('status', 1);
  595. $count = $query->count()->execute();
  596. $default_value = t("search among our !fiches cards", array("!fiches"=>$count));
  597. }else{
  598. $default_value = '';
  599. }
  600. $form['searchfield'] = array(
  601. '#type' => 'textfield',
  602. '#default_value' => $is_explore ? $keys : $default_value, // TODO: set the search page path global or a variable in settings
  603. '#autocomplete_path' => 'materiosearchapi/autocomplete/searchapi',
  604. //'#autocomplete_path' => 'materiosearchapi/autocomplete/dbselect',
  605. '#size' => 30,
  606. '#maxlength' => 1024,
  607. '#attributes' => array("default"=>$default_value),
  608. );
  609. if(user_access('use materio search api filters')){
  610. $index = search_api_index_load(variable_get('mainsearchindex', -1));
  611. $indexed_bundles = $index->options['data_alter_callbacks']['search_api_alter_bundle_filter']['settings']['bundles'];
  612. foreach ($indexed_bundles as $bundle) {
  613. $bundles_options[$bundle] = $bundle;
  614. $default_bundles[] = $bundle;
  615. }
  616. $user_bundles_filter = isset($user->data['materiosearchapi_bundlesfilter']) ? $user->data['materiosearchapi_bundlesfilter'] : $default_bundles;
  617. $form['bundles_filter'] = array(
  618. '#type'=>'checkboxes',
  619. '#options' => $bundles_options,
  620. '#default_value' => $user_bundles_filter,
  621. // '#attributes' => array('class'=>array('btn-group')),
  622. );
  623. }
  624. $form['create'] = array(
  625. '#type' => 'image_button',
  626. '#src' => drupal_get_path('module', 'materio_search_api') . '/images/search.png',
  627. '#value' => t('Search'),
  628. '#prefix' => '<div class="search-btn-wrapper">',
  629. '#suffix'=> '</div>'
  630. );
  631. // add a back to search/base home bouton if on explore/foo
  632. if ($path == 'explore' || $path == 'explore/filters' || $path == 'bookmarks' || $path == 'lists' ) {
  633. // buttons form container
  634. $form['rightcol'] = array(
  635. '#type' => 'container',
  636. );
  637. $link = l('<i class="fi-home"></i>', base_path().'actuality', array(
  638. 'html'=>true,
  639. 'attributes'=>array('class'=>'back-search-home')
  640. ));
  641. $form['rightcol']['actu'] = array(
  642. '#type' => 'markup',
  643. '#markup' => $link,
  644. //'<a href="'.base_path().'actuality" class="back-search-home"><i class="fi-home"></i></a>'
  645. );
  646. }
  647. return $form;
  648. }
  649. function materio_search_api_search_form_validate($form, &$form_state){
  650. // dsm($form, '$form');
  651. // dsm($form_state, '$form_state');
  652. // dsm(strlen($form_state['values']['searchfield']));
  653. if (strlen(trim($form_state['values']['searchfield'])) <= 1) {
  654. form_set_error('searchfield', 'Please enter at least 2 characters keyword.');
  655. }
  656. }
  657. function materio_search_api_search_form_submit($form, &$form_state){
  658. // dsm($form_state, 'form_state');
  659. global $user;
  660. if(user_access('use materio search api filters')){
  661. foreach($form_state['values']['bundles_filter'] as $bundle => $value)
  662. if($value)
  663. $bundles[] = $bundle;
  664. # if no filter checked we checked them all by default
  665. if(!isset($bundles))
  666. foreach($form_state['values']['bundles_filter'] as $bundle => $value)
  667. $bundles[] = $bundle;
  668. }else{
  669. # if user have no access to filters
  670. $index = search_api_index_load(variable_get('mainsearchindex', -1));
  671. $indexed_bundles = $index->options['data_alter_callbacks']['search_api_alter_bundle_filter']['settings']['bundles'];
  672. foreach ($indexed_bundles as $bundle) {
  673. $bundles[] = $bundle;
  674. }
  675. }
  676. user_save($user, array("data"=>array('materiosearchapi_bundlesfilter' => $bundles)));
  677. $form_state['redirect'] = 'explore/'.$form_state['values']['searchfield'];
  678. }
  679. // ___ __ __ _____ __
  680. // / | ____/ / ______ _____ ________ ____/ / / ___/___ ____ ___________/ /_
  681. // / /| |/ __ / | / / __ `/ __ \/ ___/ _ \/ __ / \__ \/ _ \/ __ `/ ___/ ___/ __ \
  682. // / ___ / /_/ /| |/ / /_/ / / / / /__/ __/ /_/ / ___/ / __/ /_/ / / / /__/ / / /
  683. // /_/ |_\__,_/ |___/\__,_/_/ /_/\___/\___/\__,_/ /____/\___/\__,_/_/ \___/_/ /_/
  684. /**
  685. * materio_search_api_advanced_search_form()
  686. */
  687. function materio_search_api_advanced_search_form($form, &$form_state){
  688. // dsm($form_state, 'form_state');
  689. // dsm($form, 'form');
  690. // get vid from settings
  691. $vid = variable_get('msa-advancedsearchvocabulary', null);
  692. if($vid){
  693. $voc = taxonomy_vocabulary_load($vid);
  694. $voc_machinename = $voc->machine_name;
  695. // dsm($voc, "voc");
  696. $hidden_terms = variable_get('msa-hiddentaxoterms', array());
  697. // dsm($hidden_terms, 'hidden_terms');
  698. $values = isset($form_state['values']) ? $form_state['values'] : null;
  699. // dsm($values, "values");
  700. $args = arg();
  701. // dsm($args, 'args');
  702. // get the first elemt of url args
  703. $path = array_shift($args);
  704. if (isset($args[0]) && $args[0] == "filters") {
  705. $path .= '/'.array_shift($args);
  706. }
  707. // get the keywords from args and reconstruct the filter lines with default values
  708. $args_values = array();
  709. if($path == 'explore/filters' && $args[0] !== "ajax"){
  710. // WORKING WITH TIDS instead of term name
  711. $tids = explode("+", $args[0]);
  712. foreach ($tids as $key => $tid) {
  713. $term = taxonomy_term_load($tid);
  714. // dsm($term, $term->tid.' : '.$term->name);
  715. // get parents
  716. if(isset($term->tid)){
  717. $parents = taxonomy_get_parents_all($tid);
  718. // dsm($parents, 'parents');
  719. // build default values
  720. for ($p=count($parents)-1; $p >=0 ; $p--) {
  721. $args_values[$key][] = $parents[$p]->tid;
  722. }
  723. }
  724. }
  725. }
  726. // dsm($args_values, 'args_values');
  727. // TODO: show result count live
  728. // TODO: filter next select form item
  729. // define number of lines,
  730. // 1 by default
  731. // or from url arguments
  732. // or let it as it is if already chanched by ajax
  733. if(!isset($form_state['filterlines'])){
  734. if(count($args_values)){
  735. $form_state['filterlines'] = count($args_values);
  736. }else{
  737. $form_state['filterlines'] = 1;
  738. }
  739. }
  740. // dsm($form_state['filterlines'], "form_state['filterlines']");
  741. // filters form container
  742. $form['filters'] = array(
  743. '#type' => 'container',
  744. '#attributes' => array('class' => array("advancedsearch-filters")),
  745. '#prefix' => '<div id="advancedsearch-filters-wrapper">',
  746. '#suffix' => '</div>',
  747. );
  748. $form['filters']['filters-table'] = array(
  749. '#type' => 'container',
  750. '#attributes' => array('class' => array("advanced-search-form-table"))
  751. );
  752. // form headers
  753. // $form['filters']['filters-table']['headers'] = array(
  754. // '#type' => 'container',
  755. // '#attributes' => array('class' => array("advanced-search-form-headers")),
  756. // 'category' => array(
  757. // '#markup' => t("Category"),
  758. // '#prefix' => '<div>','#suffix' => '</div>',
  759. // ),
  760. // 'main' => array(
  761. // '#markup' => t("Main Filter"),
  762. // '#prefix' => '<div>','#suffix' => '</div>',
  763. // ),
  764. // 'child' => array(
  765. // '#markup' => t("Child Filter"),
  766. // '#prefix' => '<div>','#suffix' => '</div>',
  767. // )
  768. // );
  769. $trigger = FALSE;
  770. if (!empty($form_state['triggering_element']['#name'])) {
  771. $trigger = $form_state['triggering_element'];
  772. }
  773. $rmline = false;
  774. if ($trigger) {
  775. // dsm($form_state['triggering_element'], $form_state['triggering_element']['#name']);
  776. // dsm($values, 'form state values');
  777. // add a line if needed
  778. if($trigger['#name'] == 'addfilters'){
  779. $form_state['filterlines'] = $form_state['filterlines']+1;
  780. }
  781. // remove a line if needed
  782. if(strpos($trigger['#name'], 'rm-filter-') === 0){
  783. // $form_state['filterlines'] = $form_state['filterlines']+1;
  784. $rmline = $trigger['#return_value'];
  785. // dsm($rmline, "rmline");
  786. }
  787. }
  788. // build first level taxonomy tree and select options
  789. $taxotree = taxonomy_get_tree($vid, 0, 1, true);
  790. // $taxotree = msa_get_taxo_tree($vid, 0, 1);
  791. // dsm($taxotree, "taxotree");
  792. // return $form;
  793. foreach ($taxotree as $index => $term) {
  794. // TODO: get translated tag name
  795. if(!in_array($term->tid, $hidden_terms)){
  796. $level_0_ops[$term->tid] = $term->name;
  797. }
  798. }
  799. // build filters lines (ajax enabled)
  800. for ($l=0; $l < $form_state['filterlines']; $l++) {
  801. // if rmline this line,
  802. // continue the loop without creating the line
  803. if($l === $rmline){
  804. continue;
  805. }
  806. // if we are after the removed line,
  807. // create a second counter ($ll) to keep a continuous order for form items
  808. // and keep the current counter ($l) to retrieve de current values
  809. // if before removed line or no removed line at all, $l = $ll
  810. if($rmline !== false && $l >= $rmline){
  811. $ll = $l-1;
  812. }else{
  813. $ll = $l;
  814. }
  815. // dsm("l : ".$l." | ll : ".$ll);
  816. $form['filters']['filters-table']['filterline-'.$ll] = array(
  817. '#type' => 'container',
  818. '#attributes' => array('class' => array("filter-line"))
  819. );
  820. // get the default value
  821. // from from_state values if ajax
  822. // or from url args
  823. // or 0
  824. $default0 = null;
  825. if(isset($values['filter-'.$l.'-0']) && $values['filter-'.$l.'-0']){
  826. $default0 = $values['filter-'.$l.'-0'];
  827. }elseif(isset($args_values[$l][0])){
  828. $default0 = $args_values[$l][0];
  829. }
  830. // dsm($default0, "default 0");
  831. // form select element for first level
  832. $form['filters']['filters-table']['filterline-'.$ll]['filter-'.$ll.'-0'] = array(
  833. '#type' => 'select',
  834. '#options' => $level_0_ops,
  835. '#name' => 'filter-'.$ll.'-0',
  836. '#empty_option' => t("Choose a category"),
  837. // '#empty_values' => 0,
  838. '#default_value' => $default0,
  839. '#ajax' => array(
  840. 'callback' => 'materio_search_api_advanced_search_select_callback',
  841. 'wrapper' => 'advancedsearch-filters-wrapper',
  842. // 'progress'=>array('message'=>''),
  843. ),
  844. );
  845. // without this line, form api will mess with default values
  846. $form_state["input"]['filter-'.$ll.'-0'] = $default0;
  847. // add level 1 filter if level 0 chosen
  848. if(is_null($default0)){
  849. // $form['filters']['filterline-'.$ll]['filter-'.$ll.'-1']
  850. $form['filters']['filters-table']['filterline-'.$ll]['filter-'.$ll.'-1'] = array(
  851. '#markup' => t("Main Filter"),
  852. '#prefix' => '<div class="filter-markup"><span>',
  853. '#suffix' => '</span></div>',
  854. );
  855. // the definition of this markup appears twice, i may avoid that ...
  856. $form['filters']['filters-table']['filterline-'.$ll]['filter-'.$ll.'-2'] = array(
  857. '#markup' => t("Child Filter"),
  858. '#prefix' => '<div class="filter-markup"><span>',
  859. '#suffix' => '</span></div>',
  860. );
  861. }else{
  862. // build second level taxonomy tree and select options
  863. $taxotree1 = taxonomy_get_tree($vid, $default0, 1, true);
  864. // dsm($taxotree1, 'taxotree1');
  865. $level_1_ops = array();
  866. foreach ($taxotree1 as $index => $term) {
  867. // TODO: get translated tag name
  868. // $term = taxonomy_term_load($tid);
  869. $level_1_ops[$term->tid] = $term->name;
  870. }
  871. // get the default value
  872. // from from_state values if ajax
  873. // or from url args
  874. // or 0
  875. $default1 = null;
  876. if(isset($values['filter-'.$l.'-1']) && $values['filter-'.$l.'-1']){
  877. $default1 = $values['filter-'.$l.'-1'];
  878. }elseif(isset($args_values[$l][1])){
  879. $default1 = $args_values[$l][1];
  880. }
  881. // dsm($default1, "default 1");
  882. // form select element for second level
  883. $form['filters']['filters-table']['filterline-'.$ll]['filter-'.$ll.'-1'] = array(
  884. '#type' => 'select',
  885. '#options' => $level_1_ops,
  886. '#name' => 'filter-'.$ll.'-1',
  887. '#empty_option' => t("Choose a main filter"),
  888. // '#empty_values' => 0,
  889. '#default_value' => $default1,
  890. '#ajax' => array(
  891. 'callback' => 'materio_search_api_advanced_search_select_callback',
  892. 'wrapper' => 'advancedsearch-filters-wrapper',
  893. ),
  894. );
  895. // without this line, form api will mess with default values
  896. $form_state["input"]['filter-'.$ll.'-1'] = $default1;
  897. // add level 2 filter if level 1 chosen
  898. if(is_null($default1)){
  899. $form['filters']['filters-table']['filterline-'.$ll]['filter-'.$ll.'-2'] = array(
  900. '#markup' => t("Child Filter"),
  901. '#prefix' => '<div class="filter-markup"><span>',
  902. '#suffix' => '</span></div>',
  903. );
  904. }else{
  905. // build second level taxonomy tree and select options
  906. $taxotree2 = taxonomy_get_tree($vid, $default1, 1, true);
  907. if(!count($taxotree2)){
  908. $form['filters']['filters-table']['filterline-'.$ll]['filter-'.$ll.'-2'] = array(
  909. '#markup' => t("No Child"),
  910. '#prefix' => '<div class="filter-markup"><span>',
  911. '#suffix' => '</span></div>',
  912. );
  913. }else{
  914. // $default2 = isset($args_values[$l][2]) ? $args_values[$l][2] : 0;
  915. $default2 = null;
  916. if(isset($values['filter-'.$l.'-2']) && $values['filter-'.$l.'-2']){
  917. $default2 = $values['filter-'.$l.'-2'];
  918. }elseif(isset($args_values[$l][2])){
  919. $default2 = $args_values[$l][2];
  920. }
  921. // dsm($default2, "default 2");
  922. $level_2_ops = array();
  923. foreach ($taxotree2 as $index => $term) {
  924. // TODO: get translated tag name
  925. $level_2_ops[$term->tid] = t($term->name);
  926. }
  927. // form select element for third level
  928. $form['filters']['filters-table']['filterline-'.$ll]['filter-'.$ll.'-2'] = array(
  929. '#type' => 'select',
  930. '#options' => $level_2_ops,
  931. '#name' => 'filter-'.$ll.'-2',
  932. '#empty_option' => t("Refine with a child filter"),
  933. // '#empty_values' => 0,
  934. '#default_value' => $default2,
  935. );
  936. // without this line, form api will mess with default values
  937. $form_state["input"]['filter-'.$ll.'-2'] = $default2;
  938. }
  939. }
  940. }
  941. // add remove the line button
  942. $form['filters']['filters-table']['filterline-'.$ll]['rm-filter-'.$ll] = array(
  943. '#type' => 'image_button',
  944. '#src' => drupal_get_path('module', 'materio_search_api') . '/images/rmfilter.png',
  945. '#title' => t('Remove this filter line'),
  946. '#name' => 'rm-filter-'.$ll,
  947. '#return_value' => $ll,
  948. '#executes_submit_callback' => false,
  949. '#ajax' => array(
  950. 'callback' => 'materio_search_api_advanced_search_rmline_callback',
  951. 'wrapper' => 'advancedsearch-filters-wrapper',
  952. // 'trigger_as' => array("name"=>"rmfilters".$l),
  953. ),
  954. // "#attributes" => array('class'=>array('rm-btn')),
  955. '#prefix'=> '<div class="rm-btn">', '#suffix'=> '</div>',
  956. );
  957. } // end of lines loop
  958. if($rmline !== false){
  959. $form_state['filterlines'] = $form_state['filterlines']-1;
  960. }
  961. // button to add a new line of criterias
  962. $form['filters']['addfilters'] = array(
  963. '#type' => 'image_button',
  964. '#src' => drupal_get_path('module', 'materio_search_api') . '/images/addfilter.png',
  965. '#title' => t('Add a filter line'),
  966. '#name' => "addfilters",
  967. '#return_value' => "add",
  968. '#ajax' => array(
  969. 'callback' => 'materio_search_api_advanced_search_addfilter_callback',
  970. 'wrapper' => 'advancedsearch-filters-wrapper',
  971. ),
  972. '#executes_submit_callback' => false,
  973. '#attributes' => array('class'=>array('add-filter'))
  974. );
  975. // buttons form container
  976. $form['rightcol'] = array(
  977. '#type' => 'container',
  978. "#attributes" => array('class'=>array('right-col')),
  979. );
  980. // add a back to search/base home bouton if on explore/foo
  981. if ($path == 'explore' || $path == 'explore/filters' || $path == 'bookmarks' || $path == 'lists' ) {
  982. $link = l('<i class="fi-home"></i>', base_path().'actuality', array(
  983. 'html'=>true,
  984. 'attributes'=>array('class'=>'back-search-home')
  985. ));
  986. $form['rightcol']['actu'] = array(
  987. '#type' => 'markup',
  988. '#markup' => $link,
  989. );
  990. }
  991. // submit the search
  992. $form['rightcol']['search'] = array(
  993. '#type' => 'image_button',
  994. '#src' => drupal_get_path('module', 'materio_search_api') . '/images/search.png',
  995. '#title' => t('Search'),
  996. '#name' => "search",
  997. '#return_value' => "search",
  998. '#prefix' => '<div class="search-btn-wrapper">',
  999. '#suffix'=> '</div>'
  1000. );
  1001. }else{
  1002. $form['novoc'] = array(
  1003. '#markup' => "Please choose a vocabulary to search from in materio search api settings"
  1004. );
  1005. }
  1006. // dsm($form, 'form');
  1007. return $form;
  1008. }
  1009. function msa_get_taxo_tree($vid, $parent, $depth){
  1010. $query = new EntityFieldQuery();
  1011. $query->entityCondition('entity_type', 'taxonomy_term')
  1012. ->propertyCondition('vid', $vid);
  1013. // ->propertyCondition('parent', $parent);
  1014. // ->propertyCondition('status', 1);
  1015. // ->entityCondition('bundle', $voc_machinename)
  1016. $result = $query->execute();
  1017. if (isset($result['taxonomy_term'])) {
  1018. $terms = $result['taxonomy_term'];
  1019. // At first we need to get field's id. If you already know field id, you can ommit this step
  1020. // Get all fields attached to a given node type
  1021. $fields = field_info_instances('taxonomy_term');
  1022. // Get id of body field
  1023. // $field_id = $fields['name']['field_id'];
  1024. // Attach a field of selected id only to get value for it
  1025. field_attach_load('taxonomy_term', $terms, FIELD_LOAD_CURRENT, array('field_id' => 'name'));
  1026. // Get values of our node field
  1027. // $output = field_get_items('node', $stories, 'field_story_image');
  1028. }
  1029. return $terms;
  1030. }
  1031. function materio_search_api_advanced_search_select_callback($form, &$form_state) {
  1032. return $form['filters'];
  1033. }
  1034. function materio_search_api_advanced_search_addfilter_callback($form, &$form_state) {
  1035. return $form['filters'];
  1036. }
  1037. function materio_search_api_advanced_search_rmline_callback($form, &$form_state) {
  1038. return $form['filters'];
  1039. }
  1040. function materio_search_api_advanced_search_form_validate($form, &$form_state){
  1041. if($form_state['triggering_element']['#name'] == 'search'){
  1042. // dsm($form, '$form');
  1043. dsm($form_state, 'form_state');
  1044. if (!isset($form_state['values']['filter-0-1']) || $form_state['values']['filter-0-1'] == '') {
  1045. form_set_error('filters', 'Please choose at least 1 keyword.');
  1046. }
  1047. }
  1048. }
  1049. function materio_search_api_advanced_search_form_submit($form, &$form_state){
  1050. // dsm($form_state, 'form_state');
  1051. $values = $form_state['values'];
  1052. $filterlines = isset($form_state['filterlines']) ? $form_state['filterlines'] : 0;
  1053. // $keywords = array();
  1054. // extract last tag of each lines
  1055. for ($l=0; $l < $filterlines ; $l++) {
  1056. for ($i=2; $i > 0 ; $i--) {
  1057. if(isset($values['filter-'.$l.'-'.$i]) && $values['filter-'.$l.'-'.$i]){
  1058. // $term = taxonomy_term_load($values['filter-'.$l.'-'.$i]);
  1059. // $keywords[$term->tid] = '"'.$term->name.'"';
  1060. // $tids[] = '"'.$values['filter-'.$l.'-'.$i].'"';
  1061. $tids[] = $values['filter-'.$l.'-'.$i];
  1062. continue 2;
  1063. }else{
  1064. continue;
  1065. }
  1066. }
  1067. }
  1068. // dsm($keywords, 'keywordstid');
  1069. $keywords_str = implode("+", $tids);
  1070. // dsm($keywords_str);
  1071. $form_state['redirect'] = 'explore/filters/'.$keywords_str; //rawurlencode($keywords_str);
  1072. }
  1073. /**
  1074. * viewmode
  1075. */
  1076. function _materio_search_api_change_viewmode($vm){
  1077. // dsm($vm, '_materio_search_api_change_viewmode');
  1078. global $user;
  1079. // dsm($user, 'user');
  1080. $entity_infos = entity_get_info();
  1081. // dsm($entity_infos, 'entity_infos');
  1082. if (in_array($vm, variable_get('availableviewmodes', array()))) {
  1083. user_save($user, array("data"=>array('materiosearchapi_viewmode' => $vm)));
  1084. $rep = array('statut'=>'saved');
  1085. }else{
  1086. $rep = array('statut'=>'viewmode not allowed');
  1087. }
  1088. return $rep;
  1089. }
  1090. /**
  1091. * - - - - - - - - - - - - THEME - - - - - - - - - - - -
  1092. */
  1093. /**
  1094. * Implements hook_theme().
  1095. */
  1096. function materio_search_api_theme($existing, $type, $theme, $path) {
  1097. return array(
  1098. 'materio_search_api_search_block' => array(
  1099. 'arguments' => array(),
  1100. 'template' => 'materio-search-api-search-block',
  1101. 'path' => drupal_get_path('module', 'materio_search_api').'/templates',
  1102. ),
  1103. 'materio_search_api_advanced_search_block' => array(
  1104. 'arguments' => array(),
  1105. 'template' => 'materio-search-api-advanced-search-block',
  1106. 'path' => drupal_get_path('module', 'materio_search_api').'/templates',
  1107. ),
  1108. 'materio_search_api_double_search_block' => array(
  1109. 'arguments' => array(),
  1110. 'template' => 'materio-search-api-double-search-block',
  1111. 'path' => drupal_get_path('module', 'materio_search_api').'/templates',
  1112. ),
  1113. 'materio_search_api_select_viewmode_block' => array(
  1114. 'arguments' => array(),
  1115. 'template' => 'materio-search-api-select-viewmode-block',
  1116. 'path' => drupal_get_path('module', 'materio_search_api').'/templates',
  1117. ),
  1118. // 'materio_search_api_filters_block' => array(
  1119. // 'arguments' => array(),
  1120. // 'template' => 'materio-search-api-filters-block',
  1121. // 'path' => drupal_get_path('module', 'materio_search_api').'/templates',
  1122. // ),
  1123. 'materio_search_api_results' => array(
  1124. 'arguments' => array(),
  1125. 'template' => 'materio-search-api-results',
  1126. 'path' => drupal_get_path('module', 'materio_search_api').'/templates',
  1127. 'variables' => array(
  1128. 'index' => NULL,
  1129. 'results' => array('result count' => 0),
  1130. 'items' => array(),
  1131. 'view_mode' => 'teaser',
  1132. 'keys' => '',
  1133. // 'page_machine_name' => NULL,
  1134. 'spellcheck' => NULL,
  1135. 'pager' => NULL,
  1136. ),
  1137. ),
  1138. // 'materio_search_api_performance' => array(
  1139. // 'render element' => 'element',
  1140. // ),
  1141. 'materio_search_api_actuality' => array(
  1142. 'template' => 'materio-search-api-actuality',
  1143. 'path' => drupal_get_path('module', 'materio_search_api').'/templates',
  1144. 'arguments' => array(
  1145. 'items' => array(),
  1146. 'view_mode' => "teaser",
  1147. 'pager' => NULL,
  1148. 'count' => 0,
  1149. )
  1150. ),
  1151. 'materio_search_api_form_element' => array(
  1152. 'render element' => 'element',
  1153. ),
  1154. );
  1155. }
  1156. /**
  1157. * Implements theme for rendering search-performance
  1158. */
  1159. // function theme_materio_search_api_performance($variables) {
  1160. // $element = array_shift($variables);
  1161. // return $element['#markup'];
  1162. // }
  1163. /**
  1164. * template_preprocess_materiobase_advanced_search_block();
  1165. */
  1166. function template_preprocess_materio_search_api_search_block(&$vars){
  1167. // dsm($vars, '$vars');
  1168. $vars['searchform'] = drupal_get_form("materio_search_api_search_form");
  1169. }
  1170. /**
  1171. * template_preprocess_materiobase_advanced_search_block();
  1172. */
  1173. function template_preprocess_materio_search_api_advanced_search_block(&$vars){
  1174. // dsm($vars, '$vars');
  1175. $vars['searchform'] = drupal_get_form("materio_search_api_advanced_search_form");
  1176. }
  1177. /**
  1178. * template_preprocess_materiobase_search_block();
  1179. */
  1180. function template_preprocess_materio_search_api_double_search_block(&$vars){
  1181. // dsm($vars, '$vars');
  1182. $vars['search_title'] = t("Text search");
  1183. $vars['searchform'] = drupal_get_form("materio_search_api_search_form");
  1184. $vars['advanced_search_title'] = t("Filtered search");
  1185. $vars['advanced_searchform'] = drupal_get_form("materio_search_api_advanced_search_form");
  1186. }
  1187. function template_preprocess_materio_search_api_select_viewmode_block(&$vars){
  1188. global $user;
  1189. $active = isset($user->data['materiosearchapi_viewmode']) ? $user->data['materiosearchapi_viewmode'] : variable_get('defaultviewmode', 'full');
  1190. $availableviewmodes = variable_get('availableviewmodes', -1);
  1191. // dsm($availableviewmodes);
  1192. $entity_infos = entity_get_info();
  1193. // dsm($entity_infos, 'entity_infos');
  1194. $content = '<div class="btn-group btn-group-vertical">';
  1195. foreach ($entity_infos['node']['view modes'] as $viewmode => $value) {
  1196. if(in_array($viewmode, $availableviewmodes)){
  1197. $link = l(
  1198. '<i class="icon-materio-viewmode-'.$viewmode.($active == $viewmode ? " active" : '').'"></i><span class="inner">'.$value['label'].'</span>',
  1199. 'materiosearchapi/viewmode/change/'.$viewmode,
  1200. array(
  1201. 'query' => drupal_get_destination(),
  1202. 'html' => true,
  1203. 'attributes' => array(
  1204. 'class' => array(
  1205. 'viewmode-link',
  1206. 'viewmode-'.$viewmode,
  1207. $active == $viewmode ? " active" : ''
  1208. ),
  1209. 'rel' => $viewmode
  1210. )
  1211. )
  1212. );
  1213. $content .= $link;
  1214. }
  1215. }
  1216. $content .= '</div>';
  1217. $vars['content'] = $content;
  1218. }
  1219. // function template_preprocess_materio_search_api_filters_block(&$vars){
  1220. // $index_machine_name = variable_get('mainsearchindex', -1);
  1221. // $index = search_api_index_load($index_machine_name);
  1222. // dsm($index, 'index');
  1223. // // $entity_infos = entity_get_info($index->item_type);
  1224. // // dsm($entity_infos, 'entity_infos');
  1225. // $indexed_bundles = $index->options['data_alter_callbacks']['search_api_alter_bundle_filter']['settings']['bundles'];
  1226. // dsm($indexed_bundles, 'indexed_bundles');
  1227. // $vars['content'] = drupal_get_form('materio_search_api_filters_form', $indexed_bundles);
  1228. // }
  1229. // function materio_search_api_filters_form($form, $form_state, $bundles){
  1230. // $form = array();
  1231. // dsm($bundles, 'bundles');
  1232. // foreach ($bundles as $bundle) {
  1233. // $form[$bundle . '_filter'] = array(
  1234. // '#type'=>'checkbox',
  1235. // '#default_value' => -1,
  1236. // '#title' => $bundle,
  1237. // );
  1238. // }
  1239. // return $form;
  1240. // }
  1241. /**
  1242. * Function for preprocessing the variables for the search_api_page_results
  1243. * template.
  1244. *
  1245. * @param array $variables
  1246. * An associative array containing:
  1247. * - $index: The index this search was executed on.
  1248. * - $results: An array of search results, as returned by
  1249. * SearchApiQueryInterface::execute().
  1250. * - $keys: The keywords of the executed search.
  1251. *
  1252. * @see materio-search-api-results.tpl.php
  1253. */
  1254. function template_preprocess_materio_search_api_results(array &$vars) {
  1255. // dsm($vars, 'vars');
  1256. $results = $vars['results'];
  1257. if(isset($results['result count'])){
  1258. $vars['result_count'] = $results['result count'];
  1259. $vars['sec'] = round($results['performance']['complete'], 3);
  1260. $vars['keywords'] = $vars['keys'];
  1261. if(isset($results['breves count'])){
  1262. // for free users
  1263. // dsm("free users");
  1264. $vars['perfascard'] = true;
  1265. $perfoutput = format_plural(
  1266. $results['breves count'],
  1267. 'The search found 1 news ',
  1268. 'The search found @count news '
  1269. );
  1270. $perfoutput .= format_plural(
  1271. $vars['result_count'] - $results['breves count'],
  1272. 'with 1 associated matter.',
  1273. 'with @count associated matters.'
  1274. );
  1275. $perfoutput .= '<br />';
  1276. $perfoutput .= format_plural(
  1277. $results['could results']['result count'],
  1278. ' You could have found 1 result with a ',
  1279. ' You could have found <strong>@count results</strong> with a '
  1280. );
  1281. $perfoutput .= l(t('full access to materiO\'.'), 'node/11187');
  1282. $perfoutput .= l(t('Pricing'), 'node/11187', array("attributes"=>array("class"=>array('button'))));
  1283. $vars['search_performance'] = $perfoutput;
  1284. }else{
  1285. // for members
  1286. $vars['perfascard'] = false;
  1287. // $vars['search_performance'] = format_plural(
  1288. // // $results['result count'],
  1289. // $vars['result_count'],
  1290. // 'The search found 1 result ',
  1291. // 'The search found @count results '
  1292. // )
  1293. $vars['search_performance'] = t('The search found @count result(s) with keywords @keys.', array(
  1294. "@count"=>$vars['result_count'],
  1295. "@keys"=>$vars['keywords'])
  1296. );
  1297. // TODO indicate how many materials and how many news
  1298. // $vars['search_performance'] = t('The search found @count result(s) with keywords @keys. @mat materials and @new news.', array(
  1299. // "@count"=>$vars['result_count'],
  1300. // "@keys"=>$vars['keywords'],
  1301. // "@mat"=>4,
  1302. // "@new"=>7)
  1303. // );
  1304. }
  1305. }else{
  1306. //for anonymous
  1307. $vars['perfascard'] = true;
  1308. $perfoutput = t('You can\'t access to the materiO\' search tool without an account.').'<br />';
  1309. $perfoutput .= format_plural(
  1310. $results['could results']['result count'],
  1311. ' You could have found 1 result with a ',
  1312. ' You could have found <strong>@count results</strong> with a '
  1313. );
  1314. $perfoutput .= l(t('full access to materiO\'.'), 'node/11187');
  1315. $perfoutput .= l(t('Pricing'), 'node/11187', array("attributes"=>array("class"=>array('button'))));
  1316. $vars['search_performance'] = $perfoutput;
  1317. }
  1318. //dsm($vars, '$vars');
  1319. }
  1320. function template_preprocess_materio_search_api_actuality(&$vars){
  1321. // dsm($vars, 'template_preprocess_materio_search_api_actuality | vars');
  1322. // $vars['actualities_infos'] = t('Actualities by materiO\'');
  1323. $vars['actualities_infos'] = t('');
  1324. }
  1325. function theme_materio_search_api_form_element($variables) {
  1326. // dsm($variables, 'variables');
  1327. $element = &$variables['element'];
  1328. // This function is invoked as theme wrapper, but the rendered form element
  1329. // may not necessarily have been processed by form_builder().
  1330. $element += array(
  1331. '#title_display' => 'before',
  1332. );
  1333. // Add element #id for #type 'item'.
  1334. if (isset($element['#markup']) && !empty($element['#id'])) {
  1335. $attributes['id'] = $element['#id'];
  1336. }
  1337. // Add element's #type and #name as class to aid with JS/CSS selectors.
  1338. $attributes['class'] = array('form-item');
  1339. if (!empty($element['#type'])) {
  1340. $attributes['class'][] = 'form-type-' . strtr($element['#type'], '_', '-');
  1341. }
  1342. if (!empty($element['#name'])) {
  1343. $attributes['class'][] = 'form-item-' . strtr($element['#name'], array(' ' => '-', '_' => '-', '[' => '-', ']' => ''));
  1344. }
  1345. // Add a class for disabled elements to facilitate cross-browser styling.
  1346. if (!empty($element['#attributes']['disabled'])) {
  1347. $attributes['class'][] = 'form-disabled';
  1348. }
  1349. # adding element class to wrapper
  1350. foreach ($element['#attributes']['class'] as $value) {
  1351. $attributes['class'][] = $value;
  1352. }
  1353. foreach ($element['#attributes'] as $attr => $value) {
  1354. if(!in_array($attr, array('class', 'multiple', 'id', 'type', 'name'))){
  1355. $attributes[$attr] = $value;
  1356. }
  1357. }
  1358. $output = '<div' . drupal_attributes($attributes) . '>' . "\n";
  1359. // If #title is not set, we don't display any label or required marker.
  1360. if (!isset($element['#title'])) {
  1361. $element['#title_display'] = 'none';
  1362. }
  1363. $prefix = isset($element['#field_prefix']) ? '<span class="field-prefix">' . $element['#field_prefix'] . '</span> ' : '';
  1364. $suffix = isset($element['#field_suffix']) ? ' <span class="field-suffix">' . $element['#field_suffix'] . '</span>' : '';
  1365. switch ($element['#title_display']) {
  1366. case 'before':
  1367. case 'invisible':
  1368. $output .= ' ' . theme('form_element_label', $variables);
  1369. $output .= ' ' . $prefix . $element['#children'] . $suffix . "\n";
  1370. break;
  1371. case 'after':
  1372. $output .= ' ' . $prefix . $element['#children'] . $suffix;
  1373. $output .= ' ' . theme('form_element_label', $variables) . "\n";
  1374. break;
  1375. case 'none':
  1376. case 'attribute':
  1377. // Output no label and no required marker, only the children.
  1378. $output .= ' ' . $prefix . $element['#children'] . $suffix . "\n";
  1379. break;
  1380. }
  1381. if (!empty($element['#description'])) {
  1382. $output .= '<div class="description">' . $element['#description'] . "</div>\n";
  1383. }
  1384. $output .= "</div>\n";
  1385. return $output;
  1386. }
  1387. /**
  1388. * - - - - - - - - - - - - SEARCH API PAGE - - - - - - - - - - - -
  1389. */
  1390. /**
  1391. * Implements hook_block_view_alter().
  1392. */
  1393. function materio_search_api_block_view_alter(&$data, $block) {
  1394. // this alter prepopulate the search form provide by search_api_page
  1395. if ($block->module == 'search_api_page') {
  1396. $page = search_api_page_load($block->delta);
  1397. $item = menu_get_item();
  1398. if (isset($page->path) && $page->path == $item['path']) {
  1399. $keys = arg(count(arg(NULL, $page->path)));
  1400. if ($keys) {
  1401. $data['content']['keys_' . $page->id]['#default_value'] = $keys;
  1402. $data['content']['keys_' . $page->id]['#value'] = $keys;
  1403. }
  1404. }
  1405. }
  1406. }