search_api.admin.inc 86 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406
  1. <?php
  2. /**
  3. * @file
  4. * Administration page callbacks for the Search API module.
  5. */
  6. /**
  7. * Page callback that shows an overview of defined servers and indexes.
  8. *
  9. * @see search_api_menu()
  10. */
  11. function search_api_admin_overview() {
  12. $base_path = drupal_get_path('module', 'search_api') . '/';
  13. drupal_add_css($base_path . 'search_api.admin.css');
  14. drupal_add_js($base_path . 'search_api.admin.js');
  15. $servers = search_api_server_load_multiple(FALSE);
  16. $indexes = array();
  17. // When any entity was not normally created in the database, then show status
  18. // for all.
  19. $show_config_status = FALSE;
  20. foreach (search_api_index_load_multiple(FALSE) as $index) {
  21. $indexes[$index->server][$index->machine_name] = $index;
  22. if (!$show_config_status && $index->status != ENTITY_CUSTOM) {
  23. $show_config_status = TRUE;
  24. }
  25. }
  26. // Show disabled servers after enabled ones.
  27. foreach ($servers as $id => $server) {
  28. if (!$server->enabled) {
  29. unset($servers[$id]);
  30. $servers[$id] = $server;
  31. }
  32. if (!$show_config_status && $server->status != ENTITY_CUSTOM) {
  33. $show_config_status = TRUE;
  34. }
  35. }
  36. $rows = array();
  37. $t_server = array('data' => t('Server'), 'colspan' => 2);
  38. $t_index = t('Index');
  39. $t_enabled['data'] = array(
  40. '#theme' => 'image',
  41. '#path' => $base_path . 'enabled.png',
  42. '#alt' => t('enabled'),
  43. '#title' => t('enabled'),
  44. );
  45. $t_enabled['class'] = array('search-api-status');
  46. $t_disabled['data'] = array(
  47. '#theme' => 'image',
  48. '#path' => $base_path . 'disabled.png',
  49. '#alt' => t('disabled'),
  50. '#title' => t('disabled'),
  51. );
  52. $t_disabled['class'] = array('search-api-status');
  53. $t_enable = t('Enable');
  54. $pre_server = 'admin/config/search/search_api/server';
  55. $pre_index = 'admin/config/search/search_api/index';
  56. $enable = '/enable';
  57. foreach ($servers as $server) {
  58. $url = $pre_server . '/' . $server->machine_name;
  59. $row = array();
  60. $row[] = $server->enabled ? $t_enabled : $t_disabled;
  61. if ($show_config_status) {
  62. $row[] = theme('entity_status', array('status' => $server->status));
  63. }
  64. $row[] = $t_server;
  65. $row[] = l($server->name, $url);
  66. $links = array();
  67. // The "Enable" function has no menu link, since a token is required. We add
  68. // it as the first link, since it will most likely be the most useful link
  69. // for a disabled server. (Same for indexes below.)
  70. if (!$server->enabled) {
  71. $links[] = array(
  72. 'title' => $t_enable,
  73. 'href' => $url . $enable,
  74. 'query' => array('token' => drupal_get_token($server->machine_name))
  75. );
  76. }
  77. $links = array_merge($links, menu_contextual_links('search-api-server', $pre_server, array($server->machine_name)));
  78. $row[] = theme('search_api_dropbutton', array('links' => $links));
  79. $rows[] = _search_api_deep_copy($row);
  80. if (!empty($indexes[$server->machine_name])) {
  81. foreach ($indexes[$server->machine_name] as $index) {
  82. $url = $pre_index . '/' . $index->machine_name;
  83. $row = array();
  84. $row[] = $index->enabled ? $t_enabled : $t_disabled;
  85. if ($show_config_status) {
  86. $row[] = theme('entity_status', array('status' => $index->status));
  87. }
  88. $row[] = ' ';
  89. $row[] = $t_index;
  90. $row[] = l($index->name, $url);
  91. $links = array();
  92. if (!$index->enabled && $server->enabled) {
  93. $links[] = array(
  94. 'title' => $t_enable,
  95. 'href' => $url . $enable,
  96. 'query' => array('token' => drupal_get_token($index->machine_name))
  97. );
  98. }
  99. $links = array_merge($links, menu_contextual_links('search-api-index', $pre_index, array($index->machine_name)));
  100. $row[] = theme('search_api_dropbutton', array('links' => $links));
  101. $rows[] = _search_api_deep_copy($row);
  102. }
  103. }
  104. }
  105. if (!empty($indexes[''])) {
  106. foreach ($indexes[''] as $index) {
  107. $url = $pre_index . '/' . $index->machine_name;
  108. $row = array();
  109. $row[] = $t_disabled;
  110. if ($show_config_status) {
  111. $row[] = theme('entity_status', array('status' => $index->status));
  112. }
  113. $row[] = array('data' => $t_index, 'colspan' => 2);
  114. $row[] = l($index->name, $url);
  115. $links = menu_contextual_links('search-api-index', $pre_index, array($index->machine_name));
  116. $row[] = theme('search_api_dropbutton', array('links' => $links));
  117. $rows[] = _search_api_deep_copy($row);
  118. }
  119. }
  120. $header = array();
  121. $header[] = t('Status');
  122. if ($show_config_status) {
  123. $header[] = t('Configuration');
  124. }
  125. $header[] = array('data' => t('Type'), 'colspan' => 2);
  126. $header[] = t('Name');
  127. $header[] = array('data' => t('Operations'));
  128. return array(
  129. '#theme' => 'table',
  130. '#header' => $header,
  131. '#rows' => $rows,
  132. '#attributes' => array('class' => array('search-api-overview')),
  133. '#empty' => t('There are no search servers or indexes defined yet.'),
  134. );
  135. }
  136. /**
  137. * Returns HTML for a drobutton list of links.
  138. *
  139. * When using this, you have to
  140. *
  141. * @param array $variables
  142. * An associative array containing the following keys:
  143. * - links: An array of links, as expected by theme_links().
  144. *
  145. * @return string
  146. * HTML for the dropbutton link list.
  147. */
  148. function theme_search_api_dropbutton(array &$variables) {
  149. $base_path = drupal_get_path('module', 'search_api') . '/';
  150. drupal_add_css($base_path . 'search_api.admin.css');
  151. drupal_add_js($base_path . 'search_api.admin.js');
  152. $variables['attributes']['class'][] = 'dropbutton';
  153. $list = theme('links', $variables);
  154. return "<div class=\"dropbutton-wrapper\">
  155. <div class=\"dropbutton-widget\">
  156. $list
  157. </div>
  158. </div>";
  159. }
  160. /**
  161. * Form callback showing a form for adding a server.
  162. */
  163. function search_api_admin_add_server(array $form, array &$form_state) {
  164. drupal_set_title(t('Add server'));
  165. $class = empty($form_state['values']['class']) ? '' : $form_state['values']['class'];
  166. $form_state['server'] = entity_create('search_api_server', array());
  167. if (empty($form_state['storage']['step_one'])) {
  168. $form['name'] = array(
  169. '#type' => 'textfield',
  170. '#title' => t('Server name'),
  171. '#description' => t('Enter the displayed name for the new server.'),
  172. '#maxlength' => 50,
  173. '#required' => TRUE,
  174. );
  175. $form['machine_name'] = array(
  176. '#type' => 'machine_name',
  177. '#maxlength' => 50,
  178. '#machine_name' => array(
  179. 'exists' => 'search_api_server_load',
  180. ),
  181. );
  182. $form['enabled'] = array(
  183. '#type' => 'checkbox',
  184. '#title' => t('Enabled'),
  185. '#description' => t('Select if the new server will be enabled after creation.'),
  186. '#default_value' => TRUE,
  187. );
  188. $form['description'] = array(
  189. '#type' => 'textarea',
  190. '#title' => t('Server description'),
  191. '#description' => t('Enter a description for the new server.'),
  192. );
  193. $form['class'] = array(
  194. '#type' => 'select',
  195. '#title' => t('Service class'),
  196. '#description' => t('Choose a service class to use for this server.'),
  197. '#options' => array('' => '< ' . t('Choose a service class') . ' >'),
  198. '#required' => TRUE,
  199. '#default_value' => $class,
  200. '#ajax' => array(
  201. 'callback' => 'search_api_admin_add_server_ajax_callback',
  202. 'wrapper' => 'search-api-class-options',
  203. ),
  204. );
  205. }
  206. elseif (!$class) {
  207. $class = $form_state['storage']['step_one']['class'];
  208. }
  209. foreach (search_api_get_service_info() as $id => $info) {
  210. if (empty($form_state['storage']['step_one'])) {
  211. $form['class']['#options'][$id] = $info['name'];
  212. }
  213. if (!$class || $class != $id) {
  214. continue;
  215. }
  216. $service = NULL;
  217. if (class_exists($info['class'])) {
  218. $service = new $info['class']($form_state['server']);
  219. }
  220. if (!($service instanceof SearchApiServiceInterface)) {
  221. watchdog('search_api', t('Service class @id specifies an illegal class: @class', array('@id' => $id, '@class' => $info['class'])), NULL, WATCHDOG_ERROR);
  222. continue;
  223. }
  224. $service_form = isset($form['options']['form']) ? $form['options']['form'] : array();
  225. $service_form = $service->configurationForm($service_form, $form_state);
  226. $form['options']['form'] = $service_form ? $service_form : array('#markup' => t('There are no configuration options for this service class.'));
  227. $form['options']['class']['#type'] = 'value';
  228. $form['options']['class']['#value'] = $class;
  229. $form['options']['#type'] = 'fieldset';
  230. $form['options']['#tree'] = TRUE;
  231. $form['options']['#collapsible'] = TRUE;
  232. $form['options']['#title'] = $info['name'];
  233. $form['options']['#description'] = $info['description'];
  234. }
  235. $form['options']['#prefix'] = '<div id="search-api-class-options">';
  236. $form['options']['#suffix'] = '</div>';
  237. // If $info is not set, there are no service classes. Display an error message
  238. // telling the user how to change that and return an empty form.
  239. if (!isset($info)) {
  240. drupal_set_message(t('There are no service classes available for the Search API. Please install a <a href="@url">module that provides a service class</a> to proceed.', array('@url' => url('https://www.drupal.org/node/1254698'))), 'error');
  241. return array();
  242. }
  243. $form['submit'] = array(
  244. '#type' => 'submit',
  245. '#value' => t('Create server'),
  246. );
  247. return $form;
  248. }
  249. /**
  250. * Form AJAX handler for search_api_admin_add_server().
  251. *
  252. * Just returns the "options" array of the already built form array.
  253. */
  254. function search_api_admin_add_server_ajax_callback(array $form, array &$form_state) {
  255. return $form['options'];
  256. }
  257. /**
  258. * Form validation handler for adding a server.
  259. *
  260. * Validates the machine name and calls the service class' validation handler.
  261. */
  262. function search_api_admin_add_server_validate(array $form, array &$form_state) {
  263. if (!empty($form_state['values']['machine_name'])) {
  264. $name = $form_state['values']['machine_name'];
  265. if (is_numeric($name)) {
  266. form_set_error('machine_name', t('The machine name must not be a pure number.'));
  267. }
  268. }
  269. if (empty($form_state['values']['options']['class'])) {
  270. return;
  271. }
  272. $class = $form_state['values']['options']['class'];
  273. $info = search_api_get_service_info($class);
  274. $service = NULL;
  275. if (class_exists($info['class'])) {
  276. $service = new $info['class']($form_state['server']);
  277. }
  278. if (!($service instanceof SearchApiServiceInterface)) {
  279. form_set_error('class', t('There seems to be something wrong with the selected service class.'));
  280. return;
  281. }
  282. $form_state['values']['options']['service'] = $service;
  283. if (!empty($form_state['values']['options']['form'])) {
  284. $service->configurationFormValidate($form['options']['form'], $form_state['values']['options']['form'], $form_state);
  285. }
  286. }
  287. /**
  288. * Form submission handler for adding a server.
  289. */
  290. function search_api_admin_add_server_submit(array $form, array &$form_state) {
  291. form_state_values_clean($form_state);
  292. $values = $form_state['values'];
  293. if (!empty($form_state['storage']['step_one'])) {
  294. $values += $form_state['storage']['step_one'];
  295. unset($form_state['storage']);
  296. }
  297. if (empty($values['options']) || ($values['class'] != $values['options']['class'])) {
  298. unset($values['options']);
  299. $form_state['storage']['step_one'] = $values;
  300. $form_state['rebuild'] = TRUE;
  301. drupal_set_message(t('Please configure the used service.'));
  302. return;
  303. }
  304. $options = isset($values['options']['form']) ? $values['options']['form'] : array();
  305. unset($values['options']);
  306. $form_state['server'] = $server = entity_create('search_api_server', $values);
  307. $server->configurationFormSubmit($form['options']['form'], $options, $form_state);
  308. $server->save();
  309. $form_state['redirect'] = 'admin/config/search/search_api/server/' . $server->machine_name;
  310. drupal_set_message(t('The server was successfully created.'));
  311. }
  312. /**
  313. * Page callback: Displays information about a server.
  314. *
  315. * @param SearchApiServer $server
  316. * The server to display.
  317. * @param string|null $action
  318. * (optional) An action to execute for the server. One of 'enable', 'disable'
  319. * or 'clear'.
  320. *
  321. * @see search_api_menu()
  322. */
  323. function search_api_admin_server_view(SearchApiServer $server, $action = NULL) {
  324. if (!empty($action)) {
  325. if ($action == 'enable') {
  326. if (isset($_GET['token']) && drupal_valid_token($_GET['token'], $server->machine_name)) {
  327. if ($server->update(array('enabled' => 1))) {
  328. drupal_set_message(t('The server was successfully enabled.'));
  329. }
  330. else {
  331. drupal_set_message(t('The server could not be enabled. Check the logs for details.'), 'error');
  332. }
  333. drupal_goto('admin/config/search/search_api/server/' . $server->machine_name);
  334. }
  335. else {
  336. return MENU_ACCESS_DENIED;
  337. }
  338. }
  339. else {
  340. $ret = drupal_get_form('search_api_admin_confirm', 'server', $action, $server);
  341. if (!empty($ret['actions'])) {
  342. return $ret;
  343. }
  344. }
  345. }
  346. drupal_set_title(search_api_admin_item_title($server));
  347. $class = search_api_get_service_info($server->class);
  348. $options = $server->viewSettings();
  349. $indexes = array();
  350. foreach (search_api_index_load_multiple(FALSE, array('server' => $server->machine_name)) as $index) {
  351. if (!$indexes) {
  352. $indexes['#theme'] = 'links';
  353. $indexes['#attributes']['class'] = array('inline');
  354. }
  355. $indexes['#links'][] = array(
  356. 'title' => $index->name,
  357. 'href' => 'admin/config/search/search_api/index/' . $index->machine_name,
  358. );
  359. }
  360. $render['view'] = array(
  361. '#theme' => 'search_api_server',
  362. '#id' => $server->id,
  363. '#name' => $server->name,
  364. '#machine_name' => $server->machine_name,
  365. '#description' => $server->description,
  366. '#enabled' => $server->enabled,
  367. '#class_id' => $server->class,
  368. '#class_name' => $class['name'],
  369. '#class_description' => $class['description'],
  370. '#indexes' => $indexes,
  371. '#options' => $options,
  372. '#status' => $server->status,
  373. '#extra' => $server->getExtraInformation(),
  374. );
  375. $render['#attached']['css'][] = drupal_get_path('module', 'search_api') . '/search_api.admin.css';
  376. if ($server->enabled) {
  377. $render['form'] = drupal_get_form('search_api_server_status_form', $server);
  378. }
  379. return $render;
  380. }
  381. /**
  382. * Returns HTML for displaying a server.
  383. *
  384. * @param array $variables
  385. * An associative array containing:
  386. * - id: The server's id.
  387. * - name: The server's name.
  388. * - machine_name: The server's machine name.
  389. * - description: The server's description.
  390. * - enabled: Boolean indicating whether the server is enabled.
  391. * - class_id: The used service class' ID.
  392. * - class_name: The used service class' display name.
  393. * - class_description: The used service class' description.
  394. * - indexes: A list of indexes associated with this server, either as an HTML
  395. * string or a render array.
  396. * - options: An HTML string or render array containing information about the
  397. * server's service-specific settings.
  398. * - status: The entity configuration status (in database, in code, etc.).
  399. * - extra: An array of additional server information in the format specified
  400. * by SearchApiAbstractService::getExtraInformation().
  401. *
  402. * @return string
  403. * HTML for displaying a server.
  404. *
  405. * @ingroup themeable
  406. */
  407. function theme_search_api_server(array $variables) {
  408. $machine_name = $variables['machine_name'];
  409. $description = $variables['description'];
  410. $enabled = $variables['enabled'];
  411. $class_id = $variables['class_id'];
  412. $class_name = $variables['class_name'];
  413. $indexes = $variables['indexes'];
  414. $options = $variables['options'];
  415. $status = $variables['status'];
  416. $extra = $variables['extra'];
  417. // First, output the index description if there is one set.
  418. $output = '';
  419. if ($description) {
  420. $output .= '<p class="description">' . nl2br(check_plain($description)) . '</p>';
  421. }
  422. // Then, display a table summarizing the index's status.
  423. $rows = array();
  424. // Create a row template with references so we don't have to deal with the
  425. // complicated structure for each individual row.
  426. $row = array(
  427. 'data' => array(
  428. array('header' => TRUE),
  429. '',
  430. ),
  431. 'class' => array(''),
  432. );
  433. $label = & $row['data'][0]['data'];
  434. $info = & $row['data'][1];
  435. $class = & $row['class'][0];
  436. if ($enabled) {
  437. $class = 'ok';
  438. $info = t('enabled (!disable_link)', array('!disable_link' => l(t('disable'), 'admin/config/search/search_api/server/' . $machine_name . '/disable')));
  439. }
  440. else {
  441. $class = 'warning';
  442. $info = t('disabled (!enable_link)', array('!enable_link' => l(t('enable'), 'admin/config/search/search_api/server/' . $machine_name . '/enable', array('query' => array('token' => drupal_get_token($machine_name))))));
  443. }
  444. $label = t('Status');
  445. $rows[] = _search_api_deep_copy($row);
  446. $class = '';
  447. $label = t('Service class');
  448. if (module_exists('help')) {
  449. $url_options['fragment'] = drupal_clean_css_identifier($class_id);
  450. $info = l($class_name, 'admin/help/search_api', $url_options);
  451. }
  452. else {
  453. $info = check_plain($class_name);
  454. }
  455. $rows[] = _search_api_deep_copy($row);
  456. if ($indexes) {
  457. $label = t('Search indexes');
  458. $info = render($indexes);
  459. $rows[] = _search_api_deep_copy($row);
  460. }
  461. if ($options) {
  462. $label = t('Service options');
  463. $info = render($options);
  464. $rows[] = _search_api_deep_copy($row);
  465. }
  466. if ($status != ENTITY_CUSTOM) {
  467. $label = t('Configuration status');
  468. $info = theme('entity_status', array('status' => $status));
  469. $class = ($status == ENTITY_OVERRIDDEN) ? 'warning' : 'ok';
  470. $rows[] = _search_api_deep_copy($row);
  471. $class = '';
  472. }
  473. if ($extra) {
  474. foreach ($extra as $information) {
  475. $label = $information['label'];
  476. $info = $information['info'];
  477. $class = !empty($information['status']) ? $information['status'] : '';
  478. $rows[] = _search_api_deep_copy($row);
  479. }
  480. }
  481. $theme['rows'] = $rows;
  482. $theme['attributes']['class'][] = 'search-api-summary';
  483. $theme['attributes']['class'][] = 'search-api-server-summary';
  484. $theme['attributes']['class'][] = 'system-status-report';
  485. $output .= theme('table', $theme);
  486. return $output;
  487. }
  488. /**
  489. * Form constructor for server operations.
  490. *
  491. * @param SearchApiServer $server
  492. * The server for which the form is displayed.
  493. *
  494. * @ingroup forms
  495. *
  496. * @see search_api_server_status_form_submit()
  497. */
  498. function search_api_server_status_form(array $form, array &$form_state, SearchApiServer $server) {
  499. $form_state['server'] = $server;
  500. $form['clear'] = array(
  501. '#type' => 'submit',
  502. '#value' => t('Delete all indexed data on this server'),
  503. '#submit' => array('search_api_server_status_form_clear_submit')
  504. );
  505. $count = $server->enabled ? search_api_server_tasks_count($server) : 0;
  506. if ($count) {
  507. $message = format_plural($count, '@count pending task must be executed before indexing.', '@count pending tasks must be executed before indexing.');
  508. drupal_set_message($message, 'warning', FALSE);
  509. $form['execute_pending_tasks'] = array(
  510. '#type' => 'submit',
  511. '#value' => t('Execute all pending tasks on this server'),
  512. '#submit' => array('search_api_server_status_form_execute_pending_tasks_submit')
  513. );
  514. }
  515. return $form;
  516. }
  517. /**
  518. * Form submission handler for search_api_server_status_form().
  519. *
  520. * Used for the "Execute all pending tasks" button.
  521. */
  522. function search_api_server_status_form_execute_pending_tasks_submit($form, &$form_state) {
  523. $server_id = $form_state['server']->machine_name;
  524. $form_state['redirect'] = "admin/config/search/search_api/server/$server_id/execute-tasks";
  525. }
  526. /**
  527. * Form submission handler for search_api_server_status_form().
  528. *
  529. * Used for the "Delete all indexed data" button.
  530. */
  531. function search_api_server_status_form_clear_submit(array $form, array &$form_state) {
  532. $server_id = $form_state['server']->machine_name;
  533. $form_state['redirect'] = "admin/config/search/search_api/server/$server_id/clear";
  534. }
  535. /**
  536. * Form constructor for editing a server's settings.
  537. *
  538. * @param SearchApiServer $server
  539. * The server to edit.
  540. *
  541. * @ingroup forms
  542. *
  543. * @see search_api_admin_server_edit_validate()
  544. * @see search_api_admin_server_edit_submit()
  545. */
  546. function search_api_admin_server_edit(array $form, array &$form_state, SearchApiServer $server) {
  547. $form_state['server'] = $server;
  548. $form['name'] = array(
  549. '#type' => 'textfield',
  550. '#title' => t('Server name'),
  551. '#description' => t('Enter the displayed name for the server.'),
  552. '#maxlength' => 50,
  553. '#default_value' => $server->name,
  554. '#required' => TRUE,
  555. );
  556. $form['enabled'] = array(
  557. '#type' => 'checkbox',
  558. '#title' => t('Enabled'),
  559. '#default_value' => $server->enabled,
  560. );
  561. $form['description'] = array(
  562. '#type' => 'textarea',
  563. '#title' => t('Server description'),
  564. '#description' => t('Enter a description for the new server.'),
  565. '#default_value' => $server->description,
  566. );
  567. $class = search_api_get_service_info($server->class);
  568. $service_options = array();
  569. $service_options = $server->configurationForm($service_options, $form_state);
  570. if ($service_options) {
  571. $form['options']['form'] = $service_options;
  572. }
  573. $form['options']['#type'] = 'fieldset';
  574. $form['options']['#tree'] = TRUE;
  575. $form['options']['#collapsible'] = TRUE;
  576. $form['options']['#title'] = $class['name'];
  577. $form['options']['#description'] = $class['description'];
  578. $form['actions']['#type'] = 'actions';
  579. $form['actions']['submit'] = array(
  580. '#type' => 'submit',
  581. '#value' => t('Save settings'),
  582. );
  583. $form['actions']['delete'] = array(
  584. '#type' => 'submit',
  585. '#value' => t('Delete'),
  586. '#submit' => array('search_api_admin_form_delete_submit'),
  587. '#limit_validation_errors' => array(),
  588. );
  589. return $form;
  590. }
  591. /**
  592. * Form validation handler for search_api_admin_server_edit().
  593. *
  594. * @see search_api_admin_server_edit_submit()
  595. */
  596. function search_api_admin_server_edit_validate(array $form, array &$form_state) {
  597. if (!empty($form['options']['form']) && !empty($form_state['values']['options']['form'])) {
  598. $form_state['server']->configurationFormValidate($form['options']['form'], $form_state['values']['options']['form'], $form_state);
  599. }
  600. }
  601. /**
  602. * Form submission handler for search_api_admin_server_edit().
  603. *
  604. * @see search_api_admin_server_edit_validate()
  605. */
  606. function search_api_admin_server_edit_submit(array $form, array &$form_state) {
  607. form_state_values_clean($form_state);
  608. $values = $form_state['values'];
  609. $server = $form_state['server'];
  610. if (isset($values['options'])) {
  611. $server->configurationFormSubmit($form['options']['form'], $values['options']['form'], $form_state);
  612. }
  613. unset($values['options']);
  614. $server->update($values);
  615. $form_state['redirect'] = 'admin/config/search/search_api/server/' . $server->machine_name;
  616. drupal_set_message(t('The search server was successfully edited.'));
  617. }
  618. /**
  619. * Form submission handler for search_api_admin_server_edit().
  620. *
  621. * Handles the 'Delete' button on the server and index edit forms.
  622. *
  623. * @see search_api_admin_server_edit()
  624. * @see search_api_admin_index_edit()
  625. */
  626. function search_api_admin_form_delete_submit($form, &$form_state) {
  627. $destination = array();
  628. if (isset($_GET['destination'])) {
  629. $destination = drupal_get_destination();
  630. unset($_GET['destination']);
  631. }
  632. if (isset($form_state['server'])) {
  633. $server = $form_state['server'];
  634. $form_state['redirect'] = array('admin/config/search/search_api/server/' . $server->machine_name . '/delete', array('query' => $destination));
  635. }
  636. elseif (isset($form_state['index'])) {
  637. $index = $form_state['index'];
  638. $form_state['redirect'] = array('admin/config/search/search_api/index/' . $index->machine_name . '/delete', array('query' => $destination));
  639. }
  640. }
  641. /**
  642. * Form constructor for adding an index.
  643. *
  644. * @ingroup forms
  645. *
  646. * @see search_api_admin_add_index_ajax_callback()
  647. * @see search_api_admin_add_index_validate()
  648. * @see search_api_admin_add_index_submit()
  649. */
  650. function search_api_admin_add_index(array $form, array &$form_state) {
  651. drupal_set_title(t('Add index'));
  652. $old_type = empty($form_state['values']['item_type']) ? '' : $form_state['values']['item_type'];
  653. $form['#attached']['css'][] = drupal_get_path('module', 'search_api') . '/search_api.admin.css';
  654. $form['#tree'] = TRUE;
  655. if (empty($form_state['step_one'])) {
  656. $form['name'] = array(
  657. '#type' => 'textfield',
  658. '#title' => t('Index name'),
  659. '#maxlength' => 50,
  660. '#required' => TRUE,
  661. );
  662. $form['machine_name'] = array(
  663. '#type' => 'machine_name',
  664. '#maxlength' => 50,
  665. '#machine_name' => array(
  666. 'exists' => 'search_api_index_load',
  667. ),
  668. );
  669. $form['item_type'] = array(
  670. '#type' => 'select',
  671. '#title' => t('Item type'),
  672. '#description' => t('Select the type of items that will be indexed in this index. ' .
  673. 'This setting cannot be changed afterwards.'),
  674. '#options' => array(),
  675. '#required' => TRUE,
  676. '#ajax' => array(
  677. 'callback' => 'search_api_admin_add_index_ajax_callback',
  678. 'wrapper' => 'search-api-datasource-options',
  679. ),
  680. );
  681. $form['datasource'] = array();
  682. foreach (search_api_get_item_type_info() as $type => $info) {
  683. $form['item_type']['#options'][$type] = $info['name'];
  684. }
  685. $form['enabled'] = array(
  686. '#type' => 'checkbox',
  687. '#title' => t('Enabled'),
  688. '#description' => t('This will only take effect if you also select a server for the index.'),
  689. '#default_value' => TRUE,
  690. );
  691. $form['description'] = array(
  692. '#type' => 'textarea',
  693. '#title' => t('Index description'),
  694. );
  695. $form['server'] = array(
  696. '#type' => 'select',
  697. '#title' => t('Server'),
  698. '#description' => t('Select the server this index should reside on.'),
  699. '#default_value' => '',
  700. '#options' => array('' => t('< No server >'))
  701. );
  702. $servers = search_api_server_load_multiple(FALSE, array('enabled' => 1));
  703. // List enabled servers first.
  704. foreach ($servers as $server) {
  705. if ($server->enabled) {
  706. $form['server']['#options'][$server->machine_name] = $server->name;
  707. }
  708. }
  709. foreach ($servers as $server) {
  710. if (!$server->enabled) {
  711. $form['server']['#options'][$server->machine_name] = t('@server_name (disabled)', array('@server_name' => $server->name));
  712. }
  713. }
  714. $form['read_only'] = array(
  715. '#type' => 'checkbox',
  716. '#title' => t('Read only'),
  717. '#description' => t('Do not write to this index or track the status of items in this index.'),
  718. '#default_value' => FALSE,
  719. );
  720. $form['options']['index_directly'] = array(
  721. '#type' => 'checkbox',
  722. '#title' => t('Index items immediately'),
  723. '#description' => t('Immediately index new or updated items instead of waiting for the next cron run. ' .
  724. 'This might have serious performance drawbacks and is generally not advised for larger sites.'),
  725. '#default_value' => FALSE,
  726. );
  727. $form['options']['cron_limit'] = array(
  728. '#type' => 'textfield',
  729. '#title' => t('Cron batch size'),
  730. '#description' => t('Set how many items will be indexed at once when indexing items during a cron run. ' .
  731. '"0" means that no items will be indexed by cron for this index, "-1" means that cron should index all items at once.'),
  732. '#default_value' => SEARCH_API_DEFAULT_CRON_LIMIT,
  733. '#size' => 4,
  734. '#attributes' => array('class' => array('search-api-cron-limit')),
  735. '#element_validate' => array('element_validate_integer'),
  736. );
  737. }
  738. elseif (!$old_type) {
  739. $old_type = $form_state['step_one']['item_type'];
  740. }
  741. if ($old_type) {
  742. $datasource = search_api_get_datasource_controller($old_type);
  743. $datasource_form = array();
  744. $datasource_form = $datasource->configurationForm($datasource_form, $form_state);
  745. if ($datasource_form) {
  746. $form['datasource'] = $datasource_form;
  747. $form['datasource']['#parents'] = array('options', 'datasource');
  748. }
  749. }
  750. $form['datasource']['#prefix'] = '<div id="search-api-datasource-options">';
  751. $form['datasource']['#suffix'] = '</div>';
  752. $form['old_type'] = array(
  753. '#type' => 'value',
  754. '#value' => $old_type,
  755. );
  756. $form['datasource_config'] = array(
  757. '#type' => 'value',
  758. '#value' => !empty($datasource_form),
  759. );
  760. $form['submit'] = array(
  761. '#type' => 'submit',
  762. '#value' => t('Create index'),
  763. );
  764. return $form;
  765. }
  766. /**
  767. * AJAX submit callback for search_api_admin_add_index().
  768. *
  769. * Used for displaying the matching datasource configuration form for the
  770. * selected item type.
  771. */
  772. function search_api_admin_add_index_ajax_callback(array $form, array &$form_state) {
  773. return $form['datasource'];
  774. }
  775. /**
  776. * Form validation handler for search_api_admin_add_index().
  777. *
  778. * @see search_api_admin_add_index_submit()
  779. */
  780. function search_api_admin_add_index_validate(array $form, array &$form_state) {
  781. $values = $form_state['values'];
  782. $name = $values['machine_name'];
  783. if (is_numeric($name)) {
  784. form_set_error('machine_name', t('The machine name must not be a pure number.'));
  785. }
  786. if (!$values['datasource_config'] || empty($values['item_type']) || $values['item_type'] != $values['old_type']) {
  787. return;
  788. }
  789. $datasource = search_api_get_datasource_controller($values['item_type']);
  790. $datasource->configurationFormValidate($form['datasource'], $form_state['values']['options']['datasource'], $form_state);
  791. }
  792. /**
  793. * Form submission handler for search_api_admin_add_index().
  794. *
  795. * @see search_api_admin_add_index_validate()
  796. */
  797. function search_api_admin_add_index_submit(array $form, array &$form_state) {
  798. form_state_values_clean($form_state);
  799. $values = $form_state['values'];
  800. if (!empty($form_state['step_one'])) {
  801. $values += $form_state['step_one'];
  802. unset($form_state['step_one']);
  803. }
  804. // The type was changed (or the form submitted without JS for the first time).
  805. // If the new type has a configuration form, we have to display it now.
  806. $datasource = search_api_get_datasource_controller($values['item_type']);
  807. if ($values['item_type'] != $values['old_type']) {
  808. $datasource_form = array();
  809. if ($datasource->configurationForm($datasource_form, $form_state)) {
  810. unset($values['options']['datasource']);
  811. $form_state['step_one'] = $values;
  812. $form_state['rebuild'] = TRUE;
  813. drupal_set_message(t('Please specify further configuration options.'));
  814. return;
  815. }
  816. }
  817. // If the current type has a configuration form, call the datasource
  818. // controller's config submit callback.
  819. if ($values['datasource_config']) {
  820. $datasource->configurationFormSubmit($form['datasource'], $values['options']['datasource'], $form_state);
  821. }
  822. // Validation of whether a server is set for the index is done in the
  823. // SearchApiIndex::save() method.
  824. search_api_index_insert($values);
  825. drupal_set_message(t('The index was successfully created. Please set up its indexed fields now.'));
  826. $form_state['redirect'] = 'admin/config/search/search_api/index/' . $values['machine_name'] . '/fields';
  827. }
  828. /**
  829. * Page callback for displaying an index's status.
  830. *
  831. * @param SearchApiIndex $index
  832. * The index to display.
  833. * @param string|null $action
  834. * (optional) An action to execute for the index. One of "reindex", "clear",
  835. * "enable" or "disable". For "disable", a confirm dialog will be shown.
  836. *
  837. * @see search_api_menu()
  838. */
  839. function search_api_admin_index_view(SearchApiIndex $index, $action = NULL) {
  840. if (!empty($action)) {
  841. if ($action == 'enable') {
  842. if (isset($_GET['token']) && drupal_valid_token($_GET['token'], $index->machine_name)) {
  843. if ($index->update(array('enabled' => 1))) {
  844. drupal_set_message(t('The index was successfully enabled.'));
  845. }
  846. else {
  847. drupal_set_message(t('The index could not be enabled. Check the logs for details.'), 'error');
  848. }
  849. drupal_goto('admin/config/search/search_api/index/' . $index->machine_name);
  850. }
  851. else {
  852. return MENU_ACCESS_DENIED;
  853. }
  854. }
  855. else {
  856. $ret = drupal_get_form('search_api_admin_confirm', 'index', $action, $index);
  857. if (!empty($ret['actions'])) {
  858. return $ret;
  859. }
  860. }
  861. }
  862. $status = search_api_index_status($index);
  863. try {
  864. $server = $index->server();
  865. }
  866. catch (SearchApiException $e) {
  867. $server = NULL;
  868. $vars['%server'] = $index->server;
  869. $message = t('The index has an unknown server (ID: %server) set. Please check the index settings.', $vars);
  870. drupal_set_message($message, 'error');
  871. }
  872. $ret['view'] = array(
  873. '#theme' => 'search_api_index',
  874. '#id' => $index->id,
  875. '#name' => $index->name,
  876. '#machine_name' => $index->machine_name,
  877. '#description' => $index->description,
  878. '#item_type' => $index->item_type,
  879. '#datasource_config' => $index->datasource()->getConfigurationSummary($index),
  880. '#enabled' => $index->enabled,
  881. '#server' => $server,
  882. '#options' => $index->options,
  883. '#fields' => $index->getFields(),
  884. '#indexed_items' => $status['indexed'],
  885. '#on_server' => NULL,
  886. '#total_items' => $status['total'],
  887. '#status' => $index->status,
  888. '#read_only' => $index->read_only,
  889. );
  890. try{
  891. $ret['view']['#on_server'] = _search_api_get_items_on_server($index);
  892. }
  893. catch (SearchApiException $e) {
  894. watchdog_exception('search_api', $e);
  895. }
  896. if ($index->enabled && !$index->read_only) {
  897. $ret['form'] = drupal_get_form('search_api_admin_index_status_form', $index, $status);
  898. }
  899. return $ret;
  900. }
  901. /**
  902. * Returns HTML for a search index.
  903. *
  904. * @param array $variables
  905. * An associative array containing:
  906. * - id: The index's id.
  907. * - name: The index' name.
  908. * - machine_name: The index' machine name.
  909. * - description: The index' description.
  910. * - item_type: The type of items stored in this index.
  911. * - datasource_config: A summary of the datasource's configuration.
  912. * - enabled: Boolean indicating whether the index is enabled.
  913. * - server: The server this index currently rests on, if any.
  914. * - options: The index' options, like cron limit.
  915. * - fields: All indexed fields of the index.
  916. * - indexed_items: The number of items already indexed in their latest
  917. * version on this index.
  918. * - on_server: The number of items actually indexed on the server. NULL if
  919. * the search for finding out the item count failed.
  920. * - total_items: The total number of items that have to be indexed for this
  921. * index.
  922. * - status: The entity configuration status (in database, in code, etc.).
  923. * - read_only: Boolean indicating whether this index is read only.
  924. *
  925. * @return string
  926. * HTML for a search index.
  927. *
  928. * @ingroup themeable
  929. */
  930. function theme_search_api_index(array $variables) {
  931. $machine_name = $variables['machine_name'];
  932. $description = $variables['description'];
  933. $enabled = $variables['enabled'];
  934. $item_type = $variables['item_type'];
  935. $datasource_config = $variables['datasource_config'];
  936. $server = $variables['server'];
  937. $options = $variables['options'];
  938. $status = $variables['status'];
  939. $indexed_items = $variables['indexed_items'];
  940. $on_server = $variables['on_server'];
  941. $total_items = $variables['total_items'];
  942. // First, output the index description if there is one set.
  943. $output = '';
  944. if ($description) {
  945. $output .= '<p class="description">' . nl2br(check_plain($description)) . '</p>';
  946. }
  947. // Then, display a table summarizing the index's status.
  948. $rows = array();
  949. // Create a row template with references so we don't have to deal with the
  950. // complicated structure for each individual row.
  951. $row = array(
  952. 'data' => array(
  953. array('header' => TRUE),
  954. '',
  955. ),
  956. 'class' => array(''),
  957. );
  958. $label = &$row['data'][0]['data'];
  959. $info = &$row['data'][1];
  960. $class = &$row['class'][0];
  961. $class = 'warning';
  962. if ($enabled) {
  963. $info = t('enabled (!disable_link)', array('!disable_link' => l(t('disable'), 'admin/config/search/search_api/index/' . $machine_name . '/disable')));
  964. $class = 'ok';
  965. }
  966. elseif ($server) {
  967. $info = t('disabled (!enable_link)', array('!enable_link' => l(t('enable'), 'admin/config/search/search_api/index/' . $machine_name . '/enable', array('query' => array('token' => drupal_get_token($machine_name))))));
  968. }
  969. else {
  970. $info = t('disabled');
  971. }
  972. $label = t('Status');
  973. $rows[] = _search_api_deep_copy($row);
  974. $class = '';
  975. $label = t('Item type');
  976. $type = search_api_get_item_type_info($item_type);
  977. $item_type = !empty($type['name']) ? $type['name'] : $item_type;
  978. $info = check_plain($item_type);
  979. $rows[] = _search_api_deep_copy($row);
  980. if ($datasource_config) {
  981. $label = t('Item type configuration');
  982. $info = check_plain($datasource_config);
  983. $rows[] = _search_api_deep_copy($row);
  984. }
  985. if ($server) {
  986. $label = t('Server');
  987. $info = l($server->name, 'admin/config/search/search_api/server/' . $server->machine_name);
  988. $rows[] = _search_api_deep_copy($row);
  989. }
  990. if ($enabled) {
  991. $options += array('cron_limit' => SEARCH_API_DEFAULT_CRON_LIMIT);
  992. if ($options['cron_limit']) {
  993. $class = 'ok';
  994. $info = format_plural(
  995. $options['cron_limit'],
  996. 'During cron runs, 1 item will be indexed per batch.',
  997. 'During cron runs, @count items will be indexed per batch.'
  998. );
  999. }
  1000. else {
  1001. $class = 'warning';
  1002. $info = t('No items will be indexed during cron runs.');
  1003. }
  1004. $label = t('Cron batch size');
  1005. $rows[] = _search_api_deep_copy($row);
  1006. $theme = array(
  1007. 'percent' => $total_items ? (int) (100 * $indexed_items / $total_items) : 100,
  1008. 'message' => t('@indexed/@total indexed', array('@indexed' => $indexed_items, '@total' => $total_items)),
  1009. );
  1010. $output .= '<h3>' . t('Index status') . '</h3>';
  1011. $output .= '<div class="search-api-index-status">' . theme('progress_bar', $theme) . '</div>';
  1012. if (!isset($on_server)) {
  1013. $info = t('An error occurred while trying to determine the server index status. Please check the logs for details.');
  1014. $class = 'error';
  1015. }
  1016. else {
  1017. $vars['@url'] = url('https://drupal.org/node/2009804#server-index-status');
  1018. $info = format_plural($on_server, 'There is 1 item indexed on the server for this index. (<a href="@url">More information</a>)', 'There are @count items indexed on the server for this index. (<a href="@url">More information</a>)', $vars);
  1019. $class = '';
  1020. }
  1021. $label = t('Server index status');
  1022. $rows[] = _search_api_deep_copy($row);
  1023. }
  1024. if ($status != ENTITY_CUSTOM) {
  1025. $label = t('Configuration status');
  1026. $info = theme('entity_status', array('status' => $status));
  1027. $class = ($status == ENTITY_OVERRIDDEN) ? 'warning' : 'ok';
  1028. $rows[] = _search_api_deep_copy($row);
  1029. }
  1030. $theme['rows'] = $rows;
  1031. $theme['attributes']['class'][] = 'search-api-summary';
  1032. $theme['attributes']['class'][] = 'search-api-index-summary';
  1033. $theme['attributes']['class'][] = 'system-status-report';
  1034. $output .= theme('table', $theme);
  1035. return $output;
  1036. }
  1037. /**
  1038. * Form constructor for an index status form.
  1039. *
  1040. * Should only be used for enabled indexes which aren't read-only.
  1041. *
  1042. * @param SearchApiIndex $index
  1043. * The index whose status should be displayed.
  1044. * @param array $status
  1045. * The indexing status of the index, as returned by search_api_index_status().
  1046. *
  1047. * @ingroup forms
  1048. *
  1049. * @see search_api_admin_index_status_form_validate()
  1050. * @see search_api_admin_index_status_form_submit()
  1051. */
  1052. function search_api_admin_index_status_form(array $form, array &$form_state, SearchApiIndex $index, array $status) {
  1053. $form['#attached']['css'][] = drupal_get_path('module', 'search_api') . '/search_api.admin.css';
  1054. $form_state['index'] = $index;
  1055. $form['index'] = array(
  1056. '#type' => 'fieldset',
  1057. '#title' => t('Index now'),
  1058. );
  1059. $form['index']['#attributes']['class'][] = 'container-inline';
  1060. $allow_indexing = ($status['indexed'] < $status['total']);
  1061. $all = t('all', array(), array('context' => 'items to index'));
  1062. $limit = array(
  1063. '#type' => 'textfield',
  1064. '#default_value' => $all,
  1065. '#size' => 4,
  1066. '#attributes' => array('class' => array('search-api-limit')),
  1067. '#disabled' => !$allow_indexing,
  1068. );
  1069. $batch_size = empty($index->options['cron_limit']) ? SEARCH_API_DEFAULT_CRON_LIMIT : $index->options['cron_limit'];
  1070. $batch_size = $batch_size > 0 ? $batch_size : $all;
  1071. $batch_size = array(
  1072. '#type' => 'textfield',
  1073. '#default_value' => $batch_size,
  1074. '#size' => 4,
  1075. '#attributes' => array('class' => array('search-api-batch-size')),
  1076. '#disabled' => !$allow_indexing,
  1077. );
  1078. // Here it gets complicated. We want to build a sentence from the form input
  1079. // elements, but to translate that we have to make the two form elements (for
  1080. // limit and batch size) pseudo-variables in the t() call. Since we can't
  1081. // pass them directly, we split the translated sentence (which still has the
  1082. // two tokens), figure out their order and then put the pieces together again
  1083. // using the form elements' #prefix and #suffix properties.
  1084. $sentence = t('Index @limit items in batches of @batch_size items');
  1085. $sentence = preg_split('/@(limit|batch_size)/', $sentence, -1, PREG_SPLIT_DELIM_CAPTURE);
  1086. if (count($sentence) == 5) {
  1087. $first = $sentence[1];
  1088. $form['index'][$first] = $$first;
  1089. $form['index'][$first]['#prefix'] = $sentence[0];
  1090. $form['index'][$first]['#suffix'] = $sentence[2];
  1091. $second = $sentence[3];
  1092. $form['index'][$second] = $$second;
  1093. $form['index'][$second]['#suffix'] = $sentence[4] . ' ';
  1094. }
  1095. else {
  1096. // PANIC!
  1097. $limit['#title'] = t('Number of items to index');
  1098. $form['index']['limit'] = $limit;
  1099. $batch_size['#title'] = t('Number of items per batch run');
  1100. $form['index']['batch_size'] = $batch_size;
  1101. }
  1102. $form['index']['button'] = array(
  1103. '#type' => 'submit',
  1104. '#value' => t('Index now'),
  1105. '#disabled' => !$allow_indexing,
  1106. );
  1107. $form['index']['total'] = array(
  1108. '#type' => 'value',
  1109. '#value' => $status['total'],
  1110. );
  1111. $form['index']['remaining'] = array(
  1112. '#type' => 'value',
  1113. '#value' => $status['total'] - $status['indexed'],
  1114. );
  1115. $form['index']['all'] = array(
  1116. '#type' => 'value',
  1117. '#value' => $all,
  1118. );
  1119. $form['reindex'] = array(
  1120. '#type' => 'submit',
  1121. '#value' => t('Queue all items for reindexing'),
  1122. '#prefix' => '<div>',
  1123. '#suffix' => '</div>',
  1124. );
  1125. $form['clear'] = array(
  1126. '#type' => 'submit',
  1127. '#value' => t('Clear all indexed data'),
  1128. '#prefix' => '<div>',
  1129. '#suffix' => '</div>',
  1130. );
  1131. return $form;
  1132. }
  1133. /**
  1134. * Form validation handler for search_api_admin_index_status_form().
  1135. *
  1136. * @see search_api_admin_index_status_form_submit()
  1137. */
  1138. function search_api_admin_index_status_form_validate(array $form, array &$form_state) {
  1139. $values = $form_state['values'];
  1140. if ($values['op'] == t('Index now')) {
  1141. $all_lower = drupal_strtolower($values['all']);
  1142. foreach (array('limit', 'batch_size') as $field) {
  1143. $val = trim($values[$field]);
  1144. if (drupal_strtolower($val) == $all_lower) {
  1145. $val = -1;
  1146. }
  1147. elseif (!$val || !is_numeric($val) || ((int) $val) != $val) {
  1148. form_error($form['index'][$field], t('Enter a non-zero integer. Use "-1" or "@all" for "all items".', array('@all' => $values['all'])));
  1149. }
  1150. else {
  1151. $val = (int) $val;
  1152. }
  1153. $form_state['values'][$field] = $val;
  1154. }
  1155. }
  1156. }
  1157. /**
  1158. * Form submission handler for search_api_admin_index_status_form().
  1159. *
  1160. * @see search_api_admin_index_status_form_validate()
  1161. */
  1162. function search_api_admin_index_status_form_submit(array $form, array &$form_state) {
  1163. $values = $form_state['values'];
  1164. $index = $form_state['index'];
  1165. $form_state['redirect'] = 'admin/config/search/search_api/index/' . $index->machine_name;
  1166. // There is a Form API bug here that will let a user submit the form via the
  1167. // "Index now" button even if it is disabled, and then just set "op" to the
  1168. // value of an arbitrary other button. We therefore have to take care to spot
  1169. // this case ourselves.
  1170. if ($form_state['input']['op'] == t('Index now') && !empty($form['index']['button']['#disabled'])) {
  1171. drupal_set_message(t('All items have already been indexed.'), 'warning');
  1172. return;
  1173. }
  1174. switch ($values['op']) {
  1175. case t('Index now'):
  1176. if (!_search_api_batch_indexing_create($index, $values['batch_size'], $values['limit'], $values['remaining'])) {
  1177. drupal_set_message(t("Couldn't create a batch, please check the batch size and limit."), 'warning');
  1178. }
  1179. break;
  1180. case t('Queue all items for reindexing'):
  1181. $form_state['redirect'] .= '/reindex';
  1182. break;
  1183. case t('Clear all indexed data'):
  1184. $form_state['redirect'] .= '/clear';
  1185. break;
  1186. }
  1187. }
  1188. /**
  1189. * Form constructor for editing an index's settings.
  1190. *
  1191. * @param SearchApiIndex $index
  1192. * The index to edit.
  1193. *
  1194. * @ingroup forms
  1195. *
  1196. * @see search_api_admin_index_edit_validate()
  1197. * @see search_api_admin_index_edit_submit()
  1198. */
  1199. function search_api_admin_index_edit(array $form, array &$form_state, SearchApiIndex $index) {
  1200. $form_state['index'] = $index;
  1201. $form['#attached']['css'][] = drupal_get_path('module', 'search_api') . '/search_api.admin.css';
  1202. $form['#tree'] = TRUE;
  1203. $form['name'] = array(
  1204. '#type' => 'textfield',
  1205. '#title' => t('Index name'),
  1206. '#maxlength' => 50,
  1207. '#default_value' => $index->name,
  1208. '#required' => TRUE,
  1209. );
  1210. try {
  1211. $enabled_fixed = !$index->server();
  1212. }
  1213. catch (Exception $e) {
  1214. watchdog_exception('search_api', $e);
  1215. // The exception only occurs if the index is disabled, and for an unknown
  1216. // server we of course want do prevent the index from being enabled.
  1217. $enabled_fixed = TRUE;
  1218. }
  1219. $form['enabled'] = array(
  1220. '#type' => 'checkbox',
  1221. '#title' => t('Enabled'),
  1222. '#default_value' => $index->enabled,
  1223. // Can't enable an index that's not lying on any server.
  1224. '#disabled' => $enabled_fixed,
  1225. );
  1226. $form['description'] = array(
  1227. '#type' => 'textarea',
  1228. '#title' => t('Index description'),
  1229. '#default_value' => $index->description,
  1230. );
  1231. $form['server'] = array(
  1232. '#type' => 'select',
  1233. '#title' => t('Server'),
  1234. '#description' => t('Select the server this index should reside on.'),
  1235. '#default_value' => $index->server,
  1236. '#options' => array('' => t('< No server >'))
  1237. );
  1238. $servers = search_api_server_load_multiple(FALSE, array('enabled' => 1));
  1239. // List enabled servers first.
  1240. foreach ($servers as $server) {
  1241. $form['server']['#options'][$server->machine_name] = $server->name;
  1242. }
  1243. $datasource_form = !empty($form['options']['datasource']) ? $form['options']['datasource'] : array();
  1244. $datasource_form = $index->datasource()->configurationForm($datasource_form, $form_state);
  1245. if ($datasource_form) {
  1246. $form['options']['datasource'] = $datasource_form;
  1247. $form['options']['datasource']['#type'] = 'fieldset';
  1248. $form['options']['datasource']['#title'] = t('Datasource options');
  1249. }
  1250. $form['read_only'] = array(
  1251. '#type' => 'checkbox',
  1252. '#title' => t('Read only'),
  1253. '#description' => t('Do not write to this index or track the status of items in this index.'),
  1254. '#default_value' => $index->read_only,
  1255. );
  1256. $form['options']['index_directly'] = array(
  1257. '#type' => 'checkbox',
  1258. '#title' => t('Index items immediately'),
  1259. '#description' => t('Immediately index new or updated items instead of waiting for the next cron run. ' .
  1260. 'This might have serious performance drawbacks and is generally not advised for larger sites.'),
  1261. '#default_value' => !empty($index->options['index_directly']),
  1262. '#states' => array(
  1263. 'invisible' => array(':input[name="read_only"]' => array('checked' => TRUE)),
  1264. ),
  1265. );
  1266. $form['options']['cron_limit'] = array(
  1267. '#type' => 'textfield',
  1268. '#title' => t('Cron batch size'),
  1269. '#description' => t('Set how many items will be indexed at once when indexing items during a cron run. ' .
  1270. '"0" means that no items will be indexed by cron for this index, "-1" means that cron should index all items at once.'),
  1271. '#default_value' => isset($index->options['cron_limit']) ? $index->options['cron_limit'] : SEARCH_API_DEFAULT_CRON_LIMIT,
  1272. '#size' => 4,
  1273. '#attributes' => array('class' => array('search-api-cron-limit')),
  1274. '#element_validate' => array('_element_validate_integer'),
  1275. '#states' => array(
  1276. 'invisible' => array(':input[name="read_only"]' => array('checked' => TRUE)),
  1277. ),
  1278. );
  1279. $form['actions']['#type'] = 'actions';
  1280. $form['actions']['submit'] = array(
  1281. '#type' => 'submit',
  1282. '#value' => t('Save settings'),
  1283. );
  1284. $form['actions']['delete'] = array(
  1285. '#type' => 'submit',
  1286. '#value' => t('Delete'),
  1287. '#submit' => array('search_api_admin_form_delete_submit'),
  1288. '#limit_validation_errors' => array(),
  1289. );
  1290. return $form;
  1291. }
  1292. /**
  1293. * Form validation handler for search_api_admin_index_edit().
  1294. *
  1295. * @see search_api_admin_index_edit_submit()
  1296. */
  1297. function search_api_admin_index_edit_validate(array $form, array &$form_state) {
  1298. if (!empty($form['options']['datasource'])) {
  1299. $form_state['values']['options'] += array('datasource' => array());
  1300. $form_state['index']->datasource()->configurationFormValidate($form['options']['datasource'], $form_state['values']['options']['datasource'], $form_state);
  1301. }
  1302. }
  1303. /**
  1304. * Form submission handler for search_api_admin_index_edit().
  1305. *
  1306. * @see search_api_admin_index_edit_validate()
  1307. */
  1308. function search_api_admin_index_edit_submit(array $form, array &$form_state) {
  1309. form_state_values_clean($form_state);
  1310. $values = $form_state['values'];
  1311. /** @var SearchApiIndex $index */
  1312. $index = $form_state['index'];
  1313. if (!empty($form['options']['datasource'])) {
  1314. $index->datasource()->configurationFormSubmit($form['options']['datasource'], $values['options']['datasource'], $form_state);
  1315. }
  1316. $values['options'] += $index->options;
  1317. $ret = $index->update($values);
  1318. $form_state['redirect'] = 'admin/config/search/search_api/index/' . $index->machine_name;
  1319. if ($ret) {
  1320. drupal_set_message(t('The search index was successfully edited.'));
  1321. }
  1322. else {
  1323. drupal_set_message(t('No values were changed.'));
  1324. }
  1325. }
  1326. /**
  1327. * Form constructor for editing an index's data alterations and processors.
  1328. *
  1329. * @param SearchApiIndex $index
  1330. * The index to edit.
  1331. *
  1332. * @ingroup forms
  1333. *
  1334. * @see search_api_admin_index_workflow_validate()
  1335. * @see search_api_admin_index_workflow_submit()
  1336. */
  1337. // Copied from filter_admin_format_form()
  1338. function search_api_admin_index_workflow(array $form, array &$form_state, SearchApiIndex $index) {
  1339. $callback_info = search_api_get_alter_callbacks();
  1340. $processor_info = search_api_get_processors();
  1341. $options = empty($index->options) ? array() : $index->options;
  1342. $form_state['index'] = $index;
  1343. $form['#tree'] = TRUE;
  1344. $form['#attached']['js'][] = drupal_get_path('module', 'search_api') . '/search_api.admin.js';
  1345. // Callbacks
  1346. $callbacks = empty($options['data_alter_callbacks']) ? array() : $options['data_alter_callbacks'];
  1347. $callback_objects = isset($form_state['callbacks']) ? $form_state['callbacks'] : array();
  1348. foreach ($callback_info as $name => $callback) {
  1349. if (!isset($callbacks[$name])) {
  1350. $callbacks[$name]['status'] = 0;
  1351. $callbacks[$name]['weight'] = $callback['weight'];
  1352. }
  1353. $settings = empty($callbacks[$name]['settings']) ? array() : $callbacks[$name]['settings'];
  1354. if (empty($callback_objects[$name]) && class_exists($callback['class'])) {
  1355. $callback_objects[$name] = new $callback['class']($index, $settings);
  1356. }
  1357. if (!(class_exists($callback['class']) && $callback_objects[$name] instanceof SearchApiAlterCallbackInterface)) {
  1358. watchdog('search_api', t('Data alteration @id specifies illegal callback class @class.', array('@id' => $name, '@class' => $callback['class'])), NULL, WATCHDOG_WARNING);
  1359. unset($callback_info[$name]);
  1360. unset($callbacks[$name]);
  1361. unset($callback_objects[$name]);
  1362. continue;
  1363. }
  1364. if (!$callback_objects[$name]->supportsIndex($index)) {
  1365. unset($callback_info[$name]);
  1366. unset($callbacks[$name]);
  1367. unset($callback_objects[$name]);
  1368. continue;
  1369. }
  1370. }
  1371. $form_state['callbacks'] = $callback_objects;
  1372. $form['#callbacks'] = $callbacks;
  1373. $form['callbacks'] = array(
  1374. '#type' => 'fieldset',
  1375. '#title' => t('Data alterations'),
  1376. '#description' => t('Select the alterations that will be executed on indexed items, and their order.'),
  1377. '#collapsible' => TRUE,
  1378. );
  1379. // Callback status.
  1380. $form['callbacks']['status'] = array(
  1381. '#type' => 'item',
  1382. '#title' => t('Enabled data alterations'),
  1383. '#prefix' => '<div class="search-api-status-wrapper">',
  1384. '#suffix' => '</div>',
  1385. );
  1386. foreach ($callback_info as $name => $callback) {
  1387. $form['callbacks']['status'][$name] = array(
  1388. '#type' => 'checkbox',
  1389. '#title' => $callback['name'],
  1390. '#default_value' => $callbacks[$name]['status'],
  1391. '#parents' => array('callbacks', $name, 'status'),
  1392. '#description' => $callback['description'],
  1393. '#weight' => $callback['weight'],
  1394. );
  1395. }
  1396. // Callback order (tabledrag).
  1397. $form['callbacks']['order'] = array(
  1398. '#type' => 'item',
  1399. '#title' => t('Data alteration processing order'),
  1400. '#theme' => 'search_api_admin_item_order',
  1401. '#table_id' => 'search-api-callbacks-order-table',
  1402. );
  1403. foreach ($callback_info as $name => $callback) {
  1404. $form['callbacks']['order'][$name]['item'] = array(
  1405. '#markup' => $callback['name'],
  1406. );
  1407. $form['callbacks']['order'][$name]['weight'] = array(
  1408. '#type' => 'weight',
  1409. '#delta' => 50,
  1410. '#default_value' => $callbacks[$name]['weight'],
  1411. '#parents' => array('callbacks', $name, 'weight'),
  1412. );
  1413. $form['callbacks']['order'][$name]['#weight'] = $callbacks[$name]['weight'];
  1414. }
  1415. // Callback settings.
  1416. $form['callbacks']['settings_title'] = array(
  1417. '#type' => 'item',
  1418. '#title' => t('Callback settings'),
  1419. );
  1420. $form['callbacks']['settings'] = array(
  1421. '#type' => 'vertical_tabs',
  1422. );
  1423. foreach ($callback_info as $name => $callback) {
  1424. $settings_form = $callback_objects[$name]->configurationForm();
  1425. if (!empty($settings_form)) {
  1426. $form['callbacks']['settings'][$name] = array(
  1427. '#type' => 'fieldset',
  1428. '#title' => $callback['name'],
  1429. '#parents' => array('callbacks', $name, 'settings'),
  1430. '#weight' => $callback['weight'],
  1431. );
  1432. $form['callbacks']['settings'][$name] += $settings_form;
  1433. }
  1434. }
  1435. // Processors
  1436. $processors = empty($options['processors']) ? array() : $options['processors'];
  1437. $processor_objects = isset($form_state['processors']) ? $form_state['processors'] : array();
  1438. foreach ($processor_info as $name => $processor) {
  1439. if (!isset($processors[$name])) {
  1440. $processors[$name]['status'] = 0;
  1441. $processors[$name]['weight'] = $processor['weight'];
  1442. }
  1443. $settings = empty($processors[$name]['settings']) ? array() : $processors[$name]['settings'];
  1444. if (empty($processor_objects[$name]) && class_exists($processor['class'])) {
  1445. $processor_objects[$name] = new $processor['class']($index, $settings);
  1446. }
  1447. if (!(class_exists($processor['class']) && $processor_objects[$name] instanceof SearchApiProcessorInterface)) {
  1448. watchdog('search_api', t('Processor @id specifies illegal processor class @class.', array('@id' => $name, '@class' => $processor['class'])), NULL, WATCHDOG_WARNING);
  1449. unset($processor_info[$name]);
  1450. unset($processors[$name]);
  1451. unset($processor_objects[$name]);
  1452. continue;
  1453. }
  1454. if (!$processor_objects[$name]->supportsIndex($index)) {
  1455. unset($processor_info[$name]);
  1456. unset($processors[$name]);
  1457. unset($processor_objects[$name]);
  1458. continue;
  1459. }
  1460. }
  1461. $form_state['processors'] = $processor_objects;
  1462. $form['#processors'] = $processors;
  1463. $form['processors'] = array(
  1464. '#type' => 'fieldset',
  1465. '#title' => t('Processors'),
  1466. '#description' => '<p>' . t("Select processors which will pre- and post-process data at index and search time, and their order. Most processors will only influence fulltext fields, but refer to their individual descriptions for details regarding their effect.<br />Also, some processors shouldn't be used with more advanced search engines (like Solr or Elasticsearch), since the search engine already provides this functionality.") . '</p>',
  1467. '#collapsible' => TRUE,
  1468. );
  1469. if ($index->server) {
  1470. $form['processors']['#description'] .= '<p>' . t("Check the <a href='@server-url'>server's</a> service class description for details.",
  1471. array('@server-url' => url('admin/config/search/search_api/server/' . $index->server . '/edit'))) . '</p>';
  1472. }
  1473. // Processor status.
  1474. $form['processors']['status'] = array(
  1475. '#type' => 'item',
  1476. '#title' => t('Enabled processors'),
  1477. '#prefix' => '<div class="search-api-status-wrapper">',
  1478. '#suffix' => '</div>',
  1479. );
  1480. foreach ($processor_info as $name => $processor) {
  1481. $form['processors']['status'][$name] = array(
  1482. '#type' => 'checkbox',
  1483. '#title' => $processor['name'],
  1484. '#default_value' => $processors[$name]['status'],
  1485. '#parents' => array('processors', $name, 'status'),
  1486. '#description' => $processor['description'],
  1487. '#weight' => $processor['weight'],
  1488. );
  1489. }
  1490. // Processor order (tabledrag).
  1491. $form['processors']['order'] = array(
  1492. '#type' => 'item',
  1493. '#title' => t('Processor processing order'),
  1494. '#description' => t('Set the order in which preprocessing will be done at index and search time. ' .
  1495. 'Postprocessing of search results will be in the exact opposite direction.'),
  1496. '#theme' => 'search_api_admin_item_order',
  1497. '#table_id' => 'search-api-processors-order-table',
  1498. );
  1499. foreach ($processor_info as $name => $processor) {
  1500. $form['processors']['order'][$name]['item'] = array(
  1501. '#markup' => $processor['name'],
  1502. );
  1503. $form['processors']['order'][$name]['weight'] = array(
  1504. '#type' => 'weight',
  1505. '#delta' => 50,
  1506. '#default_value' => $processors[$name]['weight'],
  1507. '#parents' => array('processors', $name, 'weight'),
  1508. );
  1509. $form['processors']['order'][$name]['#weight'] = $processors[$name]['weight'];
  1510. }
  1511. // Processor settings.
  1512. $form['processors']['settings_title'] = array(
  1513. '#type' => 'item',
  1514. '#title' => t('Processor settings'),
  1515. );
  1516. $form['processors']['settings'] = array(
  1517. '#type' => 'vertical_tabs',
  1518. );
  1519. foreach ($processor_info as $name => $processor) {
  1520. $settings_form = $processor_objects[$name]->configurationForm();
  1521. if (!empty($settings_form)) {
  1522. $form['processors']['settings'][$name] = array(
  1523. '#type' => 'fieldset',
  1524. '#title' => $processor['name'],
  1525. '#parents' => array('processors', $name, 'settings'),
  1526. '#weight' => $processor['weight'],
  1527. );
  1528. $form['processors']['settings'][$name] += $settings_form;
  1529. }
  1530. }
  1531. $form['actions'] = array('#type' => 'actions');
  1532. $form['actions']['submit'] = array('#type' => 'submit', '#value' => t('Save configuration'));
  1533. return $form;
  1534. }
  1535. /**
  1536. * Returns HTML for a processor/callback order form.
  1537. *
  1538. * @param array $variables
  1539. * An associative array containing:
  1540. * - element: A render element representing the form.
  1541. */
  1542. function theme_search_api_admin_item_order(array $variables) {
  1543. $element = $variables['element'];
  1544. $rows = array();
  1545. foreach (element_children($element, TRUE) as $name) {
  1546. $element[$name]['weight']['#attributes']['class'][] = 'search-api-order-weight';
  1547. $rows[] = array(
  1548. 'data' => array(
  1549. drupal_render($element[$name]['item']),
  1550. drupal_render($element[$name]['weight']),
  1551. ),
  1552. 'class' => array('draggable'),
  1553. );
  1554. }
  1555. $output = drupal_render_children($element);
  1556. $output .= theme('table', array('rows' => $rows, 'attributes' => array('id' => $element['#table_id'])));
  1557. drupal_add_tabledrag($element['#table_id'], 'order', 'sibling', 'search-api-order-weight', NULL, NULL, TRUE);
  1558. return $output;
  1559. }
  1560. /**
  1561. * Form validation handler for search_api_admin_index_workflow().
  1562. *
  1563. * @see search_api_admin_index_workflow_submit()
  1564. */
  1565. function search_api_admin_index_workflow_validate(array $form, array &$form_state) {
  1566. // Call validation functions.
  1567. foreach ($form_state['callbacks'] as $name => $callback) {
  1568. if (isset($form['callbacks']['settings'][$name]) && isset($form_state['values']['callbacks'][$name]['settings'])) {
  1569. $callback->configurationFormValidate($form['callbacks']['settings'][$name], $form_state['values']['callbacks'][$name]['settings'], $form_state);
  1570. }
  1571. }
  1572. foreach ($form_state['processors'] as $name => $processor) {
  1573. if (isset($form['processors']['settings'][$name]) && isset($form_state['values']['processors'][$name]['settings'])) {
  1574. $processor->configurationFormValidate($form['processors']['settings'][$name], $form_state['values']['processors'][$name]['settings'], $form_state);
  1575. }
  1576. }
  1577. }
  1578. /**
  1579. * Form submission handler for search_api_admin_index_workflow().
  1580. *
  1581. * @see search_api_admin_index_workflow_validate()
  1582. */
  1583. function search_api_admin_index_workflow_submit(array $form, array &$form_state) {
  1584. $values = $form_state['values'];
  1585. unset($values['callbacks']['settings']);
  1586. unset($values['processors']['settings']);
  1587. $index = $form_state['index'];
  1588. $index_path = 'admin/config/search/search_api/index/' . $index->machine_name;
  1589. $options = empty($index->options) ? array() : $index->options;
  1590. // Store callback and processor settings.
  1591. foreach ($form_state['callbacks'] as $name => $callback) {
  1592. $callback_form = isset($form['callbacks']['settings'][$name]) ? $form['callbacks']['settings'][$name] : array();
  1593. $values['callbacks'][$name] += array('settings' => array());
  1594. $values['callbacks'][$name]['settings'] = $callback->configurationFormSubmit($callback_form, $values['callbacks'][$name]['settings'], $form_state);
  1595. }
  1596. foreach ($form_state['processors'] as $name => $processor) {
  1597. $processor_form = isset($form['processors']['settings'][$name]) ? $form['processors']['settings'][$name] : array();
  1598. $values['processors'][$name] += array('settings' => array());
  1599. $values['processors'][$name]['settings'] = $processor->configurationFormSubmit($processor_form, $values['processors'][$name]['settings'], $form_state);
  1600. }
  1601. $types = search_api_field_types();
  1602. foreach ($form_state['callbacks'] as $name => $callback) {
  1603. // Check whether callback status has changed.
  1604. if ($values['callbacks'][$name]['status'] == empty($options['data_alter_callbacks'][$name]['status'])) {
  1605. $callbacks_changed = TRUE;
  1606. if ($values['callbacks'][$name]['status']) {
  1607. // Callback was just enabled, add its fields.
  1608. $properties = $callback->propertyInfo();
  1609. if ($properties) {
  1610. foreach ($properties as $key => $field) {
  1611. $type = $field['type'];
  1612. $inner = search_api_extract_inner_type($type);
  1613. if ($inner != 'token' && empty($types[$inner])) {
  1614. // Someone apparently added a structure or entity as a property in
  1615. // a data alteration.
  1616. continue;
  1617. }
  1618. if ($inner == 'token' || (search_api_is_text_type($inner) && !empty($field['options list']))) {
  1619. $old = $type;
  1620. $type = 'string';
  1621. while (search_api_is_list_type($old)) {
  1622. $old = substr($old, 5, -1);
  1623. $type = "list<$type>";
  1624. }
  1625. }
  1626. $index->options['fields'][$key] = array(
  1627. 'type' => $type,
  1628. );
  1629. }
  1630. }
  1631. }
  1632. }
  1633. }
  1634. if (!isset($options['data_alter_callbacks']) || !isset($options['processors'])
  1635. || $options['data_alter_callbacks'] != $values['callbacks']
  1636. || $options['processors'] != $values['processors']) {
  1637. $index->options['data_alter_callbacks'] = $values['callbacks'];
  1638. $index->options['processors'] = $values['processors'];
  1639. // Save the already sorted arrays to avoid having to sort them at each use.
  1640. uasort($index->options['data_alter_callbacks'], 'search_api_admin_element_compare');
  1641. uasort($index->options['processors'], 'search_api_admin_element_compare');
  1642. // Re-calculate the fields, since they might have changed in hard-to-predict
  1643. // ways.
  1644. search_api_index_recalculate_fields(array($index));
  1645. $index->save();
  1646. $index->reindex();
  1647. $vars = array('@url' => url($index_path));
  1648. drupal_set_message(t('The indexing workflow was successfully edited. All content was scheduled for <a href="@url">re-indexing</a> so the new settings can take effect.', $vars));
  1649. }
  1650. else {
  1651. drupal_set_message(t('No values were changed.'));
  1652. }
  1653. $form_state['redirect'] = $index_path . '/workflow';
  1654. }
  1655. /**
  1656. * Sort callback sorting array elements by their "weight" key, if present.
  1657. *
  1658. * @see element_sort()
  1659. */
  1660. function search_api_admin_element_compare($a, $b) {
  1661. $a_weight = (is_array($a) && isset($a['weight'])) ? $a['weight'] : 0;
  1662. $b_weight = (is_array($b) && isset($b['weight'])) ? $b['weight'] : 0;
  1663. if ($a_weight == $b_weight) {
  1664. return 0;
  1665. }
  1666. return ($a_weight < $b_weight) ? -1 : 1;
  1667. }
  1668. /**
  1669. * Form constructor for setting the indexed fields.
  1670. *
  1671. * @ingroup forms
  1672. *
  1673. * @see search_api_admin_index_fields_submit()
  1674. */
  1675. function search_api_admin_index_fields(array $form, array &$form_state, SearchApiIndex $index) {
  1676. $options = $index->getFields(FALSE, TRUE);
  1677. $fields = $options['fields'];
  1678. $additional = $options['additional fields'];
  1679. // An array of option arrays for types, keyed by nesting level.
  1680. $types = array(0 => search_api_field_types());
  1681. $entity_types = entity_get_info();
  1682. $boosts = drupal_map_assoc(array('0.0', '0.1', '0.2', '0.3', '0.5', '0.8', '1.0', '2.0', '3.0', '5.0', '8.0', '13.0', '21.0'));
  1683. $fulltext_types = array(0 => array('text'));
  1684. // Add all custom data types with fallback "text" to fulltext types as well.
  1685. foreach (search_api_get_data_type_info() as $id => $type) {
  1686. if ($type['fallback'] != 'text') {
  1687. continue;
  1688. }
  1689. $fulltext_types[0][] = $id;
  1690. }
  1691. $form_state['index'] = $index;
  1692. $form['#theme'] = 'search_api_admin_fields_table';
  1693. $form['#tree'] = TRUE;
  1694. $form['description'] = array(
  1695. '#type' => 'item',
  1696. '#title' => t('Select fields to index'),
  1697. '#description' => t('<p>The datatype of a field determines how it can be used for searching and filtering. Fields indexed with type "Fulltext" and multi-valued fields (marked with <sup>1</sup>) cannot be used for sorting. ' .
  1698. 'The boost is used to give additional weight to certain fields, e.g. titles or tags. It only takes effect for fulltext fields.</p>' .
  1699. '<p>Whether detailed field types are supported depends on the type of server this index resides on. ' .
  1700. 'In any case, fields of type "Fulltext" will always be fulltext-searchable.</p>'),
  1701. );
  1702. if ($index->server) {
  1703. $form['description']['#description'] .= '<p>' . t("Check the <a href='@server-url'>server's</a> service class description for details.",
  1704. array('@server-url' => url('admin/config/search/search_api/server/' . $index->server . '/edit'))) . '</p>';
  1705. }
  1706. foreach ($fields as $key => $info) {
  1707. $form['fields'][$key]['title']['#markup'] = check_plain($info['name']);
  1708. if (search_api_is_list_type($info['type'])) {
  1709. $form['fields'][$key]['title']['#markup'] .= ' <sup><a href="#note-multi-valued" class="note-ref">1</a></sup>';
  1710. $multi_valued_field_present = TRUE;
  1711. }
  1712. $form['fields'][$key]['machine_name']['#markup'] = check_plain($key);
  1713. if (isset($info['description'])) {
  1714. $form['fields'][$key]['description'] = array(
  1715. '#type' => 'value',
  1716. '#value' => $info['description'],
  1717. );
  1718. }
  1719. $form['fields'][$key]['indexed'] = array(
  1720. '#type' => 'checkbox',
  1721. '#default_value' => $info['indexed'],
  1722. );
  1723. if (empty($info['entity_type'])) {
  1724. // Determine the correct type options (with the correct nesting level).
  1725. $level = search_api_list_nesting_level($info['type']);
  1726. if (empty($types[$level])) {
  1727. $type_prefix = str_repeat('list<', $level);
  1728. $type_suffix = str_repeat('>', $level);
  1729. $types[$level] = array();
  1730. foreach ($types[0] as $type => $name) {
  1731. // We use the singular name for list types, since the user usually
  1732. // doesn't care about the nesting level.
  1733. $types[$level][$type_prefix . $type . $type_suffix] = $name;
  1734. }
  1735. foreach ($fulltext_types[0] as $type) {
  1736. $fulltext_types[$level][] = $type_prefix . $type . $type_suffix;
  1737. }
  1738. }
  1739. $css_key = '#edit-fields-' . drupal_clean_css_identifier($key);
  1740. $form['fields'][$key]['type'] = array(
  1741. '#type' => 'select',
  1742. '#options' => $types[$level],
  1743. '#default_value' => isset($info['real_type']) ? $info['real_type'] : $info['type'],
  1744. '#states' => array(
  1745. 'visible' => array(
  1746. $css_key . '-indexed' => array('checked' => TRUE),
  1747. ),
  1748. ),
  1749. );
  1750. $form['fields'][$key]['boost'] = array(
  1751. '#type' => 'select',
  1752. '#options' => $boosts,
  1753. '#default_value' => $info['boost'],
  1754. '#states' => array(
  1755. 'visible' => array(
  1756. $css_key . '-indexed' => array('checked' => TRUE),
  1757. ),
  1758. ),
  1759. );
  1760. // Only add the multiple visible states if the VERSION string is >= 7.14.
  1761. // See https://drupal.org/node/1464758.
  1762. if (version_compare(VERSION, '7.14', '>=')) {
  1763. foreach ($fulltext_types[$level] as $type) {
  1764. $form['fields'][$key]['boost']['#states']['visible'][$css_key . '-type'][] = array('value' => $type);
  1765. }
  1766. }
  1767. else {
  1768. $form['fields'][$key]['boost']['#states']['visible'][$css_key . '-type'] = array('value' => reset($fulltext_types[$level]));
  1769. }
  1770. }
  1771. else {
  1772. // This is an entity.
  1773. $label = $entity_types[$info['entity_type']]['label'];
  1774. if (!isset($entity_description_added)) {
  1775. $form['description']['#description'] .= '<p>' .
  1776. t('Note that indexing an entity-valued field (like %field, which has type %type) directly will only index the entity ID. ' .
  1777. 'This will be used for filtering and also sorting (which might not be what you expect). ' .
  1778. 'The entity label will usually be used when displaying the field, though. ' .
  1779. 'Use the "Add related fields" option at the bottom for indexing other fields of related entities.',
  1780. array('%field' => $info['name'], '%type' => $label)) . '</p>';
  1781. $entity_description_added = TRUE;
  1782. }
  1783. $form['fields'][$key]['type'] = array(
  1784. '#type' => 'value',
  1785. '#value' => $info['type'],
  1786. );
  1787. $form['fields'][$key]['entity_type'] = array(
  1788. '#type' => 'value',
  1789. '#value' => $info['entity_type'],
  1790. );
  1791. $form['fields'][$key]['type_name'] = array(
  1792. '#markup' => check_plain($label),
  1793. );
  1794. $form['fields'][$key]['boost'] = array(
  1795. '#type' => 'value',
  1796. '#value' => $info['boost'],
  1797. );
  1798. $form['fields'][$key]['boost_text'] = array(
  1799. '#markup' => '&nbsp;',
  1800. );
  1801. }
  1802. if ($key == 'search_api_language') {
  1803. // Is treated specially to always index the language.
  1804. $form['fields'][$key]['type']['#default_value'] = 'string';
  1805. $form['fields'][$key]['type']['#disabled'] = TRUE;
  1806. $form['fields'][$key]['boost']['#default_value'] = '1.0';
  1807. $form['fields'][$key]['boost']['#disabled'] = TRUE;
  1808. $form['fields'][$key]['indexed']['#default_value'] = 1;
  1809. $form['fields'][$key]['indexed']['#disabled'] = TRUE;
  1810. }
  1811. }
  1812. if (!empty($multi_valued_field_present)) {
  1813. $form['note']['#markup'] = '<div id="note-multi-valued"><small><sup>1</sup> ' . t('Multi-valued field') . '</small></div>';
  1814. }
  1815. $form['submit'] = array(
  1816. '#type' => 'submit',
  1817. '#value' => t('Save changes'),
  1818. );
  1819. if ($additional) {
  1820. asort($additional);
  1821. reset($additional);
  1822. $form['additional'] = array(
  1823. '#type' => 'fieldset',
  1824. '#title' => t('Add related fields'),
  1825. '#description' => t('There are entities related to entities of this type. ' .
  1826. 'You can add their fields to the list above so they can be indexed too.') . '<br />',
  1827. '#collapsible' => TRUE,
  1828. '#collapsed' => TRUE,
  1829. '#attributes' => array('class' => array('container-inline')),
  1830. 'field' => array(
  1831. '#type' => 'select',
  1832. '#options' => $additional,
  1833. '#default_value' => key($additional),
  1834. ),
  1835. 'add' => array(
  1836. '#type' => 'submit',
  1837. '#value' => t('Add fields'),
  1838. ),
  1839. );
  1840. }
  1841. return $form;
  1842. }
  1843. /**
  1844. * Helper function for building the field list for an index.
  1845. *
  1846. * @deprecated Use SearchApiIndex::getFields() instead.
  1847. */
  1848. function _search_api_admin_get_fields(SearchApiIndex $index, EntityMetadataWrapper $wrapper) {
  1849. $fields = empty($index->options['fields']) ? array() : $index->options['fields'];
  1850. $additional = array();
  1851. $entity_types = entity_get_info();
  1852. // First we need all already added prefixes.
  1853. $added = array();
  1854. foreach (array_keys($fields) as $key) {
  1855. $key = substr($key, 0, strrpos($key, ':'));
  1856. $added[$key] = TRUE;
  1857. }
  1858. // Then we walk through all properties and look if they are already contained
  1859. // in one of the arrays. Since this uses an iterative instead of a recursive
  1860. // approach, it is a bit complicated, with three arrays tracking the current
  1861. // depth.
  1862. // A wrapper for a specific field name prefix, e.g. 'user:' mapped to the user
  1863. // wrapper
  1864. $wrappers = array('' => $wrapper);
  1865. // Display names for the prefixes
  1866. $prefix_names = array('' => '');
  1867. // The list nesting level for entities with a certain prefix
  1868. $nesting_levels = array('' => 0);
  1869. $types = search_api_default_field_types();
  1870. $flat = array();
  1871. while ($wrappers) {
  1872. foreach ($wrappers as $prefix => $wrapper) {
  1873. $prefix_name = $prefix_names[$prefix];
  1874. // Deal with lists of entities.
  1875. $nesting_level = $nesting_levels[$prefix];
  1876. $type_prefix = str_repeat('list<', $nesting_level);
  1877. $type_suffix = str_repeat('>', $nesting_level);
  1878. if ($nesting_level) {
  1879. $info = $wrapper->info();
  1880. // The real nesting level of the wrapper, not the accumulated one.
  1881. $level = search_api_list_nesting_level($info['type']);
  1882. for ($i = 0; $i < $level; ++$i) {
  1883. $wrapper = $wrapper[0];
  1884. }
  1885. }
  1886. // Now look at all properties.
  1887. foreach ($wrapper as $property => $value) {
  1888. $info = $value->info();
  1889. // We hide the complexity of multi-valued types from the user here.
  1890. $type = search_api_extract_inner_type($info['type']);
  1891. // Treat Entity API type "token" as our "string" type.
  1892. // Also let text fields with limited options be of type "string" by
  1893. // default.
  1894. if ($type == 'token' || ($type == 'text' && !empty($info['options list']))) {
  1895. // Inner type is changed to "string".
  1896. $type = 'string';
  1897. // Set the field type accordingly.
  1898. $info['type'] = search_api_nest_type('string', $info['type']);
  1899. }
  1900. $info['type'] = $type_prefix . $info['type'] . $type_suffix;
  1901. $key = $prefix . $property;
  1902. if (isset($types[$type]) || isset($entity_types[$type])) {
  1903. if (isset($fields[$key])) {
  1904. // This field is already known in the index configuration.
  1905. $fields[$key]['name'] = $prefix_name . $info['label'];
  1906. $fields[$key]['description'] = empty($info['description']) ? NULL : $info['description'];
  1907. $flat[$key] = $fields[$key];
  1908. // Update its type.
  1909. if (isset($entity_types[$type])) {
  1910. // Always enforce the proper entity type.
  1911. $flat[$key]['type'] = $info['type'];
  1912. }
  1913. else {
  1914. // Else, only update the nesting level.
  1915. $set_type = search_api_extract_inner_type(isset($flat[$key]['real_type']) ? $flat[$key]['real_type'] : $flat[$key]['type']);
  1916. $flat[$key]['type'] = $info['type'];
  1917. $flat[$key]['real_type'] = search_api_nest_type($set_type, $info['type']);
  1918. }
  1919. }
  1920. else {
  1921. $flat[$key] = array(
  1922. 'name' => $prefix_name . $info['label'],
  1923. 'description' => empty($info['description']) ? NULL : $info['description'],
  1924. 'type' => $info['type'],
  1925. 'boost' => '1.0',
  1926. 'indexed' => FALSE,
  1927. );
  1928. }
  1929. }
  1930. if (empty($types[$type])) {
  1931. if (isset($added[$key])) {
  1932. // Visit this entity/struct in a later iteration.
  1933. $wrappers[$key . ':'] = $value;
  1934. $prefix_names[$key . ':'] = $prefix_name . $info['label'] . ' » ';
  1935. $nesting_levels[$key . ':'] = search_api_list_nesting_level($info['type']);
  1936. }
  1937. else {
  1938. $name = $prefix_name . $info['label'];
  1939. // Add machine names to discern fields with identical labels.
  1940. if (isset($used_names[$name])) {
  1941. if ($used_names[$name] !== FALSE) {
  1942. $additional[$used_names[$name]] .= ' [' . $used_names[$name] . ']';
  1943. $used_names[$name] = FALSE;
  1944. }
  1945. $name .= ' [' . $key . ']';
  1946. }
  1947. $additional[$key] = $name;
  1948. $used_names[$name] = $key;
  1949. }
  1950. }
  1951. }
  1952. unset($wrappers[$prefix]);
  1953. }
  1954. }
  1955. $options = array();
  1956. $options['fields'] = $flat;
  1957. $options['additional fields'] = $additional;
  1958. return $options;
  1959. }
  1960. /**
  1961. * Returns HTML for a field list form.
  1962. *
  1963. * @param array $variables
  1964. * An associative array containing:
  1965. * - element: A render element representing the form.
  1966. *
  1967. * @return string
  1968. * The HTML for a field list form.
  1969. */
  1970. function theme_search_api_admin_fields_table($variables) {
  1971. $form = $variables['element'];
  1972. $header = array(t('Field'), t('Machine name'), t('Indexed'), t('Type'), t('Boost'));
  1973. $rows = array();
  1974. foreach (element_children($form['fields']) as $name) {
  1975. $row = array();
  1976. foreach (element_children($form['fields'][$name]) as $field) {
  1977. if ($cell = render($form['fields'][$name][$field])) {
  1978. $row[] = $cell;
  1979. }
  1980. }
  1981. if (empty($form['fields'][$name]['description']['#value'])) {
  1982. $rows[] = _search_api_deep_copy($row);
  1983. }
  1984. else {
  1985. $rows[] = array(
  1986. 'data' => $row,
  1987. 'title' => strip_tags($form['fields'][$name]['description']['#value']),
  1988. );
  1989. }
  1990. }
  1991. $note = isset($form['note']) ? $form['note'] : '';
  1992. $submit = $form['submit'];
  1993. $additional = isset($form['additional']) ? $form['additional'] : FALSE;
  1994. unset($form['note'], $form['submit'], $form['additional']);
  1995. $output = drupal_render_children($form);
  1996. $output .= theme('table', array('header' => $header, 'rows' => $rows));
  1997. $output .= render($note);
  1998. $output .= render($submit);
  1999. if ($additional) {
  2000. $output .= render($additional);
  2001. }
  2002. return $output;
  2003. }
  2004. /**
  2005. * Form submission handler for search_api_admin_index_fields().
  2006. */
  2007. function search_api_admin_index_fields_submit(array $form, array &$form_state) {
  2008. $index = $form_state['index'];
  2009. $options = isset($index->options) ? $index->options : array();
  2010. $index_path = 'admin/config/search/search_api/index/' . $index->machine_name;
  2011. if ($form_state['values']['op'] == t('Save changes')) {
  2012. $fields = $form_state['values']['fields'];
  2013. $default_types = search_api_default_field_types();
  2014. $custom_types = search_api_get_data_type_info();
  2015. foreach ($fields as $name => $field) {
  2016. if (empty($field['indexed'])) {
  2017. unset($fields[$name]);
  2018. }
  2019. else {
  2020. // Don't store the description. "indexed" is implied.
  2021. unset($fields[$name]['description'], $fields[$name]['indexed']);
  2022. // For non-default types, set type to the fallback and only real_type to
  2023. // the custom type.
  2024. $inner_type = search_api_extract_inner_type($field['type']);
  2025. if (!isset($default_types[$inner_type])) {
  2026. $fields[$name]['real_type'] = $field['type'];
  2027. $fields[$name]['type'] = search_api_nest_type($custom_types[$inner_type]['fallback'], $field['type']);
  2028. }
  2029. // Boost defaults to 1.0.
  2030. if ($field['boost'] == '1.0') {
  2031. unset($fields[$name]['boost']);
  2032. }
  2033. }
  2034. }
  2035. $options['fields'] = $fields;
  2036. unset($options['additional fields']);
  2037. $ret = $index->update(array('options' => $options));
  2038. if ($ret) {
  2039. $vars = array('@url' => $index_path);
  2040. drupal_set_message(t('The indexed fields were successfully changed. The index was cleared and will have to be <a href="@url">re-indexed</a> with the new settings.', $vars));
  2041. }
  2042. else {
  2043. drupal_set_message(t('No values were changed.'));
  2044. }
  2045. if (isset($index->options['data_alter_callbacks']) || isset($index->options['processors'])) {
  2046. $form_state['redirect'] = $index_path . '/fields';
  2047. }
  2048. else {
  2049. drupal_set_message(t('Please set up the indexing workflow.'));
  2050. $form_state['redirect'] = $index_path . '/workflow';
  2051. }
  2052. return;
  2053. }
  2054. // Adding a related entity's fields.
  2055. $prefix = $form_state['values']['additional']['field'];
  2056. $options['additional fields'][$prefix] = $prefix;
  2057. $ret = $index->update(array('options' => $options));
  2058. if ($ret) {
  2059. drupal_set_message(t('The available fields were successfully changed.'));
  2060. }
  2061. else {
  2062. drupal_set_message(t('No values were changed.'));
  2063. }
  2064. $form_state['redirect'] = $index_path . '/fields';
  2065. }
  2066. /**
  2067. * Form constructor for a generic confirmation form.
  2068. *
  2069. * @param $type
  2070. * The type of entity (not the real "entity type"). Either "server" or
  2071. * "index".
  2072. * @param $action
  2073. * The action that would be executed for this entity after confirming. One of
  2074. * "reindex" ("index" type only), "clear", "disable" or "delete".
  2075. * @param Entity $entity
  2076. * The entity for which the action would be performed. Must have a "name"
  2077. * property.
  2078. *
  2079. * @return array|false
  2080. * Either a form array, or FALSE if this combination of type and action is
  2081. * not supported.
  2082. */
  2083. function search_api_admin_confirm(array $form, array &$form_state, $type, $action, Entity $entity) {
  2084. switch ($type) {
  2085. case 'server':
  2086. switch ($action) {
  2087. case 'clear':
  2088. $text = array(
  2089. t('Clear server @name', array('@name' => $entity->name)),
  2090. t('Do you really want to clear all indexed data from this server?'),
  2091. t('This will permanently remove all data currently indexed on this server. Before the data is reindexed, searches on the indexes associated with this server will not return any results. This action cannot be undone. <strong>Use with caution!</strong>'),
  2092. t("The server's indexed data was successfully cleared."),
  2093. );
  2094. break;
  2095. case 'disable':
  2096. $text = array(
  2097. t('Disable server @name', array('@name' => $entity->name)),
  2098. t('Do you really want to disable this server?'),
  2099. t('This will disconnect all indexes from this server and disable them. Searches on these indexes will not be available until they are added to another server and re-enabled. All indexed data (except for read-only indexes) on this server will be cleared.'),
  2100. t('The server and its indexes were successfully disabled.'),
  2101. );
  2102. break;
  2103. case 'delete':
  2104. if ($entity->hasStatus(ENTITY_OVERRIDDEN)) {
  2105. $text = array(
  2106. t('Revert server @name', array('@name' => $entity->name)),
  2107. t('Do you really want to revert this server?'),
  2108. t('This will revert all settings for this server back to the defaults. This action cannot be undone.'),
  2109. t('The server settings have been successfully reverted.'),
  2110. );
  2111. }
  2112. else {
  2113. $text = array(
  2114. t('Delete server @name', array('@name' => $entity->name)),
  2115. t('Do you really want to delete this server?'),
  2116. t('This will delete the server and disable all associated indexes. ' .
  2117. "Searches on these indexes won't be available until they are moved to another server and re-enabled."),
  2118. t('The server was successfully deleted.'),
  2119. );
  2120. }
  2121. break;
  2122. default:
  2123. return FALSE;
  2124. }
  2125. break;
  2126. case 'index':
  2127. switch ($action) {
  2128. case 'reindex':
  2129. $text = array(
  2130. t('Re-index index @name', array('@name' => $entity->name)),
  2131. t('Do you really want to queue all items on this index for re-indexing?'),
  2132. t('This will mark all items for this index to be marked as needing to be indexed. Searches on this index will continue to yield results while the items are being re-indexed. This action cannot be undone.'),
  2133. t('The index was successfully marked for re-indexing.'),
  2134. );
  2135. break;
  2136. case 'clear':
  2137. $text = array(
  2138. t('Clear index @name', array('@name' => $entity->name)),
  2139. t('Do you really want to clear the indexed data of this index?'),
  2140. t('This will remove all data currently indexed for this index. Before the data is reindexed, searches on the index will not return any results. This action cannot be undone.'),
  2141. t('The index was successfully cleared.'),
  2142. );
  2143. break;
  2144. case 'disable':
  2145. $text = array(
  2146. t('Disable index @name', array('@name' => $entity->name)),
  2147. t('Do you really want to disable this index?'),
  2148. t("Searches on this index won't be available until it is re-enabled."),
  2149. t('The index was successfully disabled.'),
  2150. );
  2151. break;
  2152. case 'delete':
  2153. if ($entity->hasStatus(ENTITY_OVERRIDDEN)) {
  2154. $text = array(
  2155. t('Revert index @name', array('@name' => $entity->name)),
  2156. t('Do you really want to revert this index?'),
  2157. t('This will revert all settings on this index back to the defaults. This action cannot be undone.'),
  2158. t('The index settings have been successfully reverted.'),
  2159. );
  2160. }
  2161. else {
  2162. $text = array(
  2163. t('Delete index @name', array('@name' => $entity->name)),
  2164. t('Do you really want to delete this index?'),
  2165. t('This will remove the index from the server and delete all settings. ' .
  2166. 'All data on this index will be lost.'),
  2167. t('The index has been successfully deleted.'),
  2168. );
  2169. }
  2170. break;
  2171. default:
  2172. return FALSE;
  2173. }
  2174. break;
  2175. default:
  2176. return FALSE;
  2177. }
  2178. $form = array(
  2179. 'type' => array(
  2180. '#type' => 'value',
  2181. '#value' => $type,
  2182. ),
  2183. 'action' => array(
  2184. '#type' => 'value',
  2185. '#value' => $action,
  2186. ),
  2187. 'id' => array(
  2188. '#type' => 'value',
  2189. '#value' => $entity->machine_name,
  2190. ),
  2191. 'message' => array(
  2192. '#type' => 'value',
  2193. '#value' => $text[3],
  2194. ),
  2195. );
  2196. $desc = "<h3>{$text[1]}</h3><p>{$text[2]}</p>";
  2197. return confirm_form($form, $text[0], "admin/config/search/search_api/$type/{$entity->machine_name}", $desc);
  2198. }
  2199. /**
  2200. * Submit function for search_api_admin_confirm().
  2201. */
  2202. function search_api_admin_confirm_submit(array $form, array &$form_state) {
  2203. $values = $form_state['values'];
  2204. $type = $values['type'];
  2205. $action = $values['action'];
  2206. $id = $values['id'];
  2207. $success = FALSE;
  2208. $function = "search_api_{$type}_{$action}";
  2209. try {
  2210. // Some actions, like disabling, can actually throw an exception.
  2211. $success = $function($id);
  2212. }
  2213. catch (SearchApiException $e) {
  2214. watchdog_exception('search_api', $e);
  2215. }
  2216. if ($success) {
  2217. drupal_set_message($values['message']);
  2218. }
  2219. else {
  2220. drupal_set_message(t('An error has occurred while performing the desired action. Check the logs for details.'), 'error');
  2221. }
  2222. $form_state['redirect'] = $action == 'delete'
  2223. ? "admin/config/search/search_api"
  2224. : "admin/config/search/search_api/$type/$id";
  2225. }