materio_search_api.module 48 KB

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