features.admin.inc 56 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436
  1. <?php
  2. /**
  3. * @file
  4. * Forms for Features admin screens
  5. */
  6. /**
  7. * Form constructor for features export form.
  8. *
  9. * Acts as a router based on the form_state.
  10. *
  11. * @param object|null $feature
  12. * The feature object, if available. NULL by default.
  13. *
  14. * @see features_export_build_form_submit()
  15. * @ingroup forms
  16. */
  17. function features_export_form($form, $form_state, $feature = NULL) {
  18. module_load_include('inc', 'features', 'features.export');
  19. features_include();
  20. $feature_name = !empty($feature->name) ? $feature->name : '';
  21. $form = array(
  22. '#attributes' => array('class' => array('features-export-form')),
  23. '#feature' => isset($feature) ? $feature : NULL,
  24. );
  25. $form['info'] = array(
  26. '#type' => 'fieldset',
  27. '#title' => t('General Information'),
  28. '#tree' => FALSE,
  29. '#weight' => 2,
  30. '#collapsible' => FALSE,
  31. '#collapsed' => FALSE,
  32. '#prefix' => "<div id='features-export-info'>",
  33. '#suffix' => '</div>',
  34. );
  35. $form['info']['name'] = array(
  36. '#title' => t('Name'),
  37. '#description' => t('Example: Image gallery') . ' (' . t('Do not begin name with numbers.') . ')',
  38. '#type' => 'textfield',
  39. '#default_value' => !empty($feature->info['name']) ? $feature->info['name'] : '',
  40. '#attributes' => array('class' => array('feature-name')),
  41. );
  42. $form['info']['module_name'] = array(
  43. '#type' => 'textfield',
  44. '#title' => t('Machine-readable name'),
  45. '#description' => t('Example: image_gallery') . '<br/>' . t('May only contain lowercase letters, numbers and underscores. <strong>Try to avoid conflicts with the names of existing Drupal projects.</strong>'),
  46. '#required' => TRUE,
  47. '#default_value' => $feature_name,
  48. '#attributes' => array('class' => array('feature-module-name')),
  49. '#element_validate' => array('features_export_form_validate_field'),
  50. );
  51. // If recreating this feature, disable machine name field and blank out
  52. // js-attachment classes to ensure the machine name cannot be changed.
  53. if (isset($feature)) {
  54. $form['info']['module_name']['#value'] = $feature_name;
  55. $form['info']['module_name']['#disabled'] = TRUE;
  56. $form['info']['name']['#attributes'] = array();
  57. }
  58. $form['info']['description'] = array(
  59. '#title' => t('Description'),
  60. '#description' => t('Provide a short description of what users should expect when they enable your feature.'),
  61. '#type' => 'textfield',
  62. '#default_value' => !empty($feature->info['description']) ? $feature->info['description'] : '',
  63. );
  64. $form['info']['package'] = array(
  65. '#title' => t('Package'),
  66. '#description' => t('Organize your features in groups.'),
  67. '#type' => 'textfield',
  68. '#autocomplete_path' => 'features/autocomplete/packages',
  69. '#default_value' => !empty($feature->info['package']) ? $feature->info['package'] : 'Features',
  70. );
  71. $form['info']['version'] = array(
  72. '#title' => t('Version'),
  73. '#description' => t('Examples: 7.x-1.0, 7.x-1.0-beta1'),
  74. '#type' => 'textfield',
  75. '#required' => FALSE,
  76. '#default_value' => !empty($feature->info['version']) ? $feature->info['version'] : '',
  77. '#size' => 30,
  78. '#element_validate' => array('features_export_form_validate_field'),
  79. );
  80. $form['advanced'] = array(
  81. '#type' => 'fieldset',
  82. '#title' => t('Advanced Options'),
  83. '#tree' => FALSE,
  84. '#collapsible' => TRUE,
  85. '#collapsed' => TRUE,
  86. '#weight' => 10,
  87. '#prefix' => "<div id='features-export-advanced'>",
  88. '#suffix' => '</div>',
  89. );
  90. $form['advanced']['project_status_url'] = array(
  91. '#title' => t('URL of update XML'),
  92. '#description' => t('URL of Feature Server. For Example: http://mywebsite.com/fserver'),
  93. '#type' => 'textfield',
  94. '#required' => FALSE,
  95. '#default_value' => !empty($feature->info['project status url']) ? $feature->info['project status url'] : '',
  96. '#element_validate' => array('features_export_form_validate_field'),
  97. );
  98. $directory = (!empty($feature->filename)) ? dirname($feature->filename) : 'sites/all/modules';
  99. if (!empty($feature_name) && substr_compare($directory, $feature_name, strlen($directory)-strlen($feature_name), strlen($feature_name)) === 0) {
  100. // if path ends with module_name, strip it
  101. $directory = dirname($directory);
  102. }
  103. $form['advanced']['generate_path'] = array(
  104. '#title' => t('Path to Generate feature module'),
  105. '#description' => t('File path for feature module. For Example: sites/all/modules or /tmp. ' .
  106. t('Leave blank for <strong>@path</strong>', array('@path' => $directory))),
  107. '#type' => 'textfield',
  108. '#required' => FALSE,
  109. '#default_value' => !empty($feature->info['project path']) ? $feature->info['project path'] : '',
  110. );
  111. $form['advanced']['generate'] = array(
  112. '#type' => 'submit',
  113. '#value' => t('Generate feature'),
  114. '#submit' => array('features_export_build_form_submit'),
  115. );
  116. // build the Component Listing panel on the right
  117. _features_export_form_components($form, $form_state);
  118. $form['advanced']['info-preview'] = array(
  119. '#type' => 'button',
  120. '#value' => t('Preview .info file'),
  121. '#ajax' => array(
  122. 'callback' => 'features_info_file_preview',
  123. 'wrapper' => 'features-export-wrapper',
  124. ),
  125. );
  126. //Info dialog
  127. $form['advanced']['info-file'] = array(
  128. '#prefix' => '<div id="features-info-file" title="Export .info file preview">',
  129. 'text' => array(
  130. '#type' => 'textarea',
  131. '#default_value' => '',
  132. '#resizable' => FALSE,
  133. ),
  134. '#suffix' => '</div>',
  135. );
  136. $form['buttons'] = array(
  137. '#theme' => 'features_form_buttons',
  138. '#tree' => FALSE,
  139. '#weight' => 99,
  140. '#prefix' => "<div id='features-export-buttons'>",
  141. '#suffix' => '</div>',
  142. );
  143. $form['buttons']['submit'] = array(
  144. '#type' => 'submit',
  145. '#value' => t('Download feature'),
  146. '#weight' => 10,
  147. '#submit' => array('features_export_build_form_submit'),
  148. );
  149. $form['#attached']['library'][] = array('system', 'ui.dialog');
  150. return $form;
  151. }
  152. /**
  153. * Return the render array elements for the Components selection on the Export form
  154. * @param array $feature - feature associative array
  155. * @param array $components - array of components in feature
  156. */
  157. function _features_export_form_components(&$form, &$form_state) {
  158. global $features_ignore_conflicts;
  159. drupal_add_css(drupal_get_path('module', 'features') . '/features.css');
  160. drupal_add_js(drupal_get_path('module', 'features') . '/features.js');
  161. $feature = $form['#feature'];
  162. // keep the allow_conflict variable around in the session
  163. if (isset($form_state['values']['features_allow_conflicts'])) {
  164. $_SESSION['features_allow_conflicts'] = $form_state['values']['features_allow_conflicts'];
  165. $features_ignore_conflicts = $_SESSION['features_allow_conflicts'];
  166. }
  167. $form['export'] = array(
  168. '#type' => 'fieldset',
  169. '#title' => t('Components'),
  170. '#description' => t('Expand each component section and select which items should be included in this feature export.'),
  171. '#tree' => FALSE,
  172. '#prefix' => "<div id='features-export-wrapper'>",
  173. '#suffix' => '</div>',
  174. '#collapsible' => FALSE,
  175. '#collapsed' => FALSE,
  176. '#weight' => 1,
  177. );
  178. // filter field used in javascript, so javascript will unhide it
  179. $form['export']['features_filter_wrapper'] = array(
  180. '#type' => 'fieldset',
  181. '#title' => t('Filters'),
  182. '#tree' => FALSE,
  183. '#prefix' => "<div id='features-filter' class='element-invisible'>",
  184. '#suffix' => '</div>',
  185. '#collapsible' => FALSE,
  186. '#collapsed' => FALSE,
  187. '#weight' => -10,
  188. );
  189. $form['export']['features_filter_wrapper']['features_filter'] = array(
  190. '#type' => 'textfield',
  191. '#title' => t('Search'),
  192. '#hidden' => TRUE,
  193. '#default_value' => '',
  194. '#suffix' => "<span class='features-filter-clear'>". t('Clear') ."</span>",
  195. );
  196. $form['export']['features_filter_wrapper']['checkall'] = array(
  197. '#type' => 'checkbox',
  198. '#default_value' => FALSE,
  199. '#hidden' => TRUE,
  200. '#title' => t('Select all'),
  201. '#attributes' => array(
  202. 'class' => array('features-checkall'),
  203. )
  204. );
  205. $form['advanced']['features_autodetect_wrapper'] = array(
  206. '#type' => 'fieldset',
  207. '#tree' => FALSE,
  208. '#prefix' => "<div id='features-autodetect'>",
  209. '#suffix' => '</div>',
  210. '#collapsible' => FALSE,
  211. '#collapsed' => FALSE,
  212. );
  213. $form['advanced']['features_autodetect_wrapper']['autodetect'] = array(
  214. '#title' => t('Add auto-detected dependencies'),
  215. '#type' => 'checkbox',
  216. '#default_value' => !empty($feature->info['no autodetect']) ? FALSE : TRUE,
  217. );
  218. // this refresh button will rebuild the form.
  219. // this button is hidden by javascript since it is only needed when
  220. // javascript is not available
  221. $form['advanced']['features_autodetect_wrapper']['features_refresh'] = array(
  222. '#type' => 'submit',
  223. '#value' => t('Refresh'),
  224. '#name' => 'features-refresh',
  225. '#attributes' => array(
  226. 'title' => t("Refresh the list of auto-detected items."),
  227. 'class' => array('features-refresh-button'),
  228. ),
  229. '#submit' => array('features_export_form_rebuild'),
  230. '#prefix' => "<div class='features-refresh-wrapper'>",
  231. '#suffix' => "</div>",
  232. '#ajax' => array(
  233. 'callback' => 'features_export_form_ajax',
  234. 'wrapper' => 'features-export-wrapper',
  235. ),
  236. );
  237. // generate the export array for the current feature and user selections
  238. $export = _features_export_build($feature, $form_state);
  239. $form['advanced']['features_allow_conflicts'] = array(
  240. '#title' => t('Allow conflicts to be added'),
  241. '#type' => 'checkbox',
  242. '#default_value' => $features_ignore_conflicts,
  243. '#ajax' => array(
  244. 'callback' => 'features_export_form_ajax',
  245. 'wrapper' => 'features-export-wrapper',
  246. ),
  247. );
  248. if (isset($form_state['values']['op']) && ($form_state['values']['op'] == $form_state['values']['info-preview'])) {
  249. // handle clicking Preview button
  250. module_load_include('inc', 'features', 'features.export');
  251. $feature_export = _features_export_generate($export, $form_state, $feature);
  252. $feature_export = features_export_prepare($feature_export, $feature->name);
  253. $info = features_export_info($feature_export);
  254. drupal_add_js(array('features' => array('info' => $info)), 'setting');
  255. }
  256. $sections = array('included', 'detected', 'added');
  257. foreach ($export['components'] as $component => $component_info) {
  258. $label = (isset($component_info['name']) ?
  259. $component_info['name'] . " <span>(" . check_plain($component) . ")</span>" : check_plain($component));
  260. $count = 0;
  261. foreach ($sections as $section) {
  262. $count += count($component_info['options'][$section]);
  263. }
  264. $extra_class = ($count == 0) ? 'features-export-empty' : '';
  265. $component_name = str_replace('_', '-', check_plain($component));
  266. if ($count + count($component_info['options']['sources']) > 0) {
  267. $form['export'][$component] = array(
  268. '#markup' => '',
  269. '#tree' => TRUE,
  270. );
  271. $form['export'][$component]['sources'] = array(
  272. '#type' => 'fieldset',
  273. '#title' => $label,
  274. '#tree' => TRUE,
  275. '#collapsible' => TRUE,
  276. '#collapsed' => TRUE,
  277. '#attributes' => array('class' => array('features-export-component')),
  278. '#prefix' => "<div class='features-export-parent component-$component'>",
  279. );
  280. $form['export'][$component]['sources']['selected'] = array(
  281. '#type' => 'checkboxes',
  282. '#id' => "edit-sources-$component_name",
  283. '#options' => features_dom_encode_options($component_info['options']['sources']),
  284. '#default_value' => features_dom_encode_options($component_info['selected']['sources'], FALSE),
  285. '#attributes' => array(
  286. 'class' => array('component-select'),
  287. ),
  288. );
  289. foreach ($sections as $section) {
  290. $form['export'][$component][$section] = array(
  291. '#type' => 'checkboxes',
  292. '#options' => !empty($component_info['options'][$section]) ?
  293. features_dom_encode_options($component_info['options'][$section]) : array(),
  294. '#default_value' => !empty($component_info['selected'][$section]) ?
  295. features_dom_encode_options($component_info['selected'][$section], FALSE) : array(),
  296. '#attributes' => array('class' => array('component-' . $section)),
  297. );
  298. }
  299. $form['export'][$component][$sections[0]]['#prefix'] =
  300. "<div class='component-list features-export-list $extra_class'>";
  301. $form['export'][$component][$sections[count($sections)-1]]['#suffix'] = '</div></div>';
  302. }
  303. }
  304. $form['export']['features_legend'] = array(
  305. '#type' => 'fieldset',
  306. '#title' => t('Legend'),
  307. '#tree' => FALSE,
  308. '#prefix' => "<div id='features-legend'>",
  309. '#suffix' => '</div>',
  310. '#collapsible' => FALSE,
  311. '#collapsed' => FALSE,
  312. );
  313. $form['export']['features_legend']['legend'] = array(
  314. '#markup' =>
  315. "<span class='component-included'>Normal</span> " .
  316. "<span class='component-added'>Changed</span> " .
  317. "<span class='component-detected'>Auto detected</span> " .
  318. "<span class='features-conflict'>Conflict</span> ",
  319. );
  320. }
  321. /**
  322. * Return the full feature export array based upon user selections in form_state
  323. * @param array $feature Feature array to be exported
  324. * @param array $form_state Optional form_state information for user selections
  325. * can be updated to reflect new selection status
  326. * @return array New export array to be exported
  327. * array['components'][$component_name] = $component_info
  328. * $component_info['options'][$section] is list of available options
  329. * $component_info['selected'][$section] is option state TRUE/FALSE
  330. * $section = array('sources', included', 'detected', 'added')
  331. * sources - options that are available to be added to the feature
  332. * included - options that have been previously exported to the feature
  333. * detected - options that have been auto-detected
  334. * added - newly added options to the feature
  335. *
  336. * NOTE: This routine gets a bit complex to handle all of the different possible
  337. * user checkbox selections and de-selections.
  338. * Cases to test:
  339. * 1a) uncheck Included item -> mark as Added but unchecked
  340. * 1b) re-check unchecked Added item -> return it to Included check item
  341. * 2a) check Sources item -> mark as Added and checked
  342. * 2b) uncheck Added item -> return it to Sources as unchecked
  343. * 3a) uncheck Included item that still exists as auto-detect -> mark as Detected but unchecked
  344. * 3b) re-check Detected item -> return it to Included and checked
  345. * 4a) check Sources item should also add any auto-detect items as Detected and checked
  346. * 4b) uncheck Sources item with auto-detect and auto-detect items should return to Sources and unchecked
  347. * 5a) uncheck a Detected item -> refreshing page should keep it as unchecked Detected
  348. * 6) when nothing changes, refresh should not change any state
  349. * 7) should never see an unchecked Included item
  350. */
  351. function _features_export_build($feature, &$form_state) {
  352. global $features_ignore_conflicts;
  353. // set a global to effect features_get_component_map when building feature
  354. // hate to use a global, but it's just for an admin screen so probably ok
  355. if (isset($_SESSION['features_allow_conflicts'])) {
  356. $features_ignore_conflicts = $_SESSION['features_allow_conflicts'];
  357. }
  358. $feature_name = isset($feature->name) ? $feature->name : NULL;
  359. $conflicts = _features_get_used($feature_name);
  360. $reset = FALSE;
  361. if (isset($form_state['triggering_element']['#name']) && ($form_state['triggering_element']['#name'] == 'features_allow_conflicts')) {
  362. // when clicking the Allow Conflicts button, reset the feature back to it's original state
  363. $reset = TRUE;
  364. }
  365. module_load_include('inc', 'features', 'features.export');
  366. features_include();
  367. $components = features_get_components();
  368. uasort($components, 'features_compare_component_name');
  369. // Assemble the combined component list
  370. $stub = array();
  371. $sections = array('sources', 'included', 'detected', 'added');
  372. // create a new feature "stub" to populate
  373. $stub_count = array();
  374. foreach ($components as $component => $component_info) {
  375. if ($reset) {
  376. unset($form_state['values'][$component]);
  377. }
  378. // User-selected components take precedence.
  379. $stub[$component] = array();
  380. $stub_count[$component] = 0;
  381. // add selected items from Sources checkboxes
  382. if (!empty($form_state['values'][$component]['sources']['selected'])) {
  383. $stub[$component] = array_merge($stub[$component], features_dom_decode_options(array_filter($form_state['values'][$component]['sources']['selected'])));
  384. $stub_count[$component]++;
  385. }
  386. // add selected items from already Included and newly Added checkboxes
  387. foreach (array('included', 'added') as $section) {
  388. if (!empty($form_state['values'][$component][$section])) {
  389. $stub[$component] = array_merge($stub[$component], features_dom_decode_options(array_filter($form_state['values'][$component][$section])));
  390. $stub_count[$component]++;
  391. }
  392. }
  393. // count any detected items
  394. if (!empty($form_state['values'][$component]['detected'])) {
  395. $stub_count[$component]++;
  396. }
  397. // Only fallback to an existing feature's values if there are no export options for the component.
  398. if ($component == 'dependencies') {
  399. if (($stub_count[$component] == 0) && !empty($feature->info['dependencies'])) {
  400. $stub[$component] = drupal_map_assoc($feature->info['dependencies']);
  401. }
  402. }
  403. elseif (($stub_count[$component] == 0) && !empty($feature->info['features'][$component])) {
  404. $stub[$component] = drupal_map_assoc($feature->info['features'][$component]);
  405. }
  406. }
  407. // Generate new populated feature
  408. $export = features_populate(array('features' => $stub, 'dependencies' => $stub['dependencies']), $feature_name);
  409. // Components that are already exported to feature
  410. $exported_features_info = !empty($feature->info['features']) ? $feature->info['features'] : array();
  411. $exported_features_info['dependencies'] = !empty($feature->info['dependencies']) ? $feature->info['dependencies'] : array();
  412. // Components that should be exported
  413. $new_features_info = !empty($export['features']) ? $export['features'] : array();
  414. $new_features_info['dependencies'] = !empty($export['dependencies']) ? $export['dependencies'] : array();
  415. $excluded = !empty($feature->info['features_exclude']) ? $feature->info['features_exclude'] : array();
  416. // now fill the $export with categorized sections of component options
  417. // based upon user selections and de-selections
  418. foreach ($components as $component => $component_info) {
  419. $component_export = $component_info;
  420. foreach ($sections as $section) {
  421. $component_export['options'][$section] = array();
  422. $component_export['selected'][$section] = array();
  423. }
  424. $options = features_invoke($component, 'features_export_options');
  425. if (!empty($options)) {
  426. $exported_components = !empty($exported_features_info[$component]) ? $exported_features_info[$component] : array();
  427. $new_components = !empty($new_features_info[$component]) ? $new_features_info[$component] : array();
  428. // Find all default components that are not provided by this feature and
  429. // strip them out of the possible options.
  430. if ($map = features_get_default_map($component)) {
  431. foreach ($map as $k => $v) {
  432. if (isset($options[$k]) && (!isset($feature->name) || $v !== $feature->name)) {
  433. unset($options[$k]);
  434. }
  435. }
  436. }
  437. foreach ($options as $key => $value) {
  438. // use the $clean_key when accessing $form_state
  439. $clean_key = features_dom_encode($key);
  440. // if checkbox in Sources is checked, move it to Added section
  441. if (!empty($form_state['values'][$component]['sources']['selected'][$clean_key])) {
  442. unset($form_state['input'][$component]['sources']['selected'][$clean_key]);
  443. $form_state['values'][$component]['sources']['selected'][$clean_key] = FALSE;
  444. $form_state['values'][$component]['added'][$clean_key] = 1;
  445. $form_state['input'][$component]['added'][$clean_key] = $clean_key;
  446. $component_export['options']['added'][$key] = check_plain($value);
  447. $component_export['selected']['added'][$key] = $key;
  448. }
  449. elseif (in_array($key, $new_components)) {
  450. // option is in the New exported array
  451. if (in_array($key, $exported_components)) {
  452. // option was already previously exported
  453. // so it's part of the Included checkboxes
  454. $section = 'included';
  455. $default_value = $key;
  456. if ($reset) {
  457. // leave it included
  458. }
  459. // if Included item was un-selected (removed from export $stub)
  460. // but was re-detected in the $new_components
  461. // means it was an auto-detect that was previously part of the export
  462. // and is now de-selected in UI
  463. elseif (!empty($form_state['values']) &&
  464. (isset($form_state['values'][$component]['included'][$clean_key]) ||
  465. empty($form_state['values'][$component]['detected'][$clean_key])) &&
  466. empty($stub[$component][$key])) {
  467. $section = 'detected';
  468. $default_value = FALSE;
  469. }
  470. // unless it's unchecked in the form, then move it to Newly disabled item
  471. elseif (!empty($form_state['values']) &&
  472. empty($form_state['values'][$component]['added'][$clean_key]) &&
  473. empty($form_state['values'][$component]['detected'][$clean_key]) &&
  474. empty($form_state['values'][$component]['included'][$clean_key])) {
  475. $section = 'added';
  476. $default_value = FALSE;
  477. }
  478. }
  479. else {
  480. // option was in New exported array, but NOT in already exported
  481. // so it's a user-selected or an auto-detect item
  482. $section = 'detected';
  483. // check for item explicity excluded
  484. if (isset($excluded[$component][$key]) && !isset($form_state['values'][$component]['detected'][$clean_key])) {
  485. $default_value = FALSE;
  486. }
  487. else {
  488. $default_value = $key;
  489. }
  490. // if it's already checked in Added or Sources, leave it in Added as checked
  491. if (!empty($form_state['values']) &&
  492. (!empty($form_state['values'][$component]['added'][$clean_key]) ||
  493. !empty($form_state['values'][$component]['sources']['selected'][$clean_key]))) {
  494. $section = 'added';
  495. $default_value = $key;
  496. }
  497. // if it's already been unchecked, leave it unchecked
  498. elseif (!empty($form_state['values']) &&
  499. empty($form_state['values'][$component]['sources']['selected'][$clean_key]) &&
  500. empty($form_state['values'][$component]['detected'][$clean_key]) &&
  501. !isset($form_state['values'][$component]['added'][$clean_key])) {
  502. $section = 'detected';
  503. $default_value = FALSE;
  504. }
  505. }
  506. $component_export['options'][$section][$key] = check_plain($value);
  507. $component_export['selected'][$section][$key] = $default_value;
  508. // save which dependencies are specifically excluded from auto-detection
  509. if (($section == 'detected') && ($default_value === FALSE)) {
  510. $excluded[$component][$key] = $key;
  511. // remove excluded item from export
  512. if ($component == 'dependencies') {
  513. unset($export['dependencies'][$key]);
  514. }
  515. else {
  516. unset($export['features'][$component][$key]);
  517. }
  518. }
  519. else {
  520. unset($excluded[$component][$key]);
  521. }
  522. // remove the 'input' and set the 'values' so Drupal stops looking at 'input'
  523. if (isset($form_state['values'])) {
  524. if (!$default_value) {
  525. unset($form_state['input'][$component][$section][$clean_key]);
  526. $form_state['values'][$component][$section][$clean_key] = FALSE;
  527. }
  528. else {
  529. $form_state['input'][$component][$section][$clean_key] = $clean_key;
  530. $form_state['values'][$component][$section][$clean_key] = 1;
  531. }
  532. }
  533. }
  534. else {
  535. // option was not part of the new export
  536. $added = FALSE;
  537. foreach (array('included', 'added') as $section) {
  538. // restore any user-selected checkboxes
  539. if (!empty($form_state['values'][$component][$section][$clean_key])) {
  540. $component_export['options'][$section][$key] = check_plain($value);
  541. $component_export['selected'][$section][$key] = $key;
  542. $added = TRUE;
  543. }
  544. }
  545. if (!$added) {
  546. // if not Included or Added, then put it back in the unchecked Sources checkboxes
  547. $component_export['options']['sources'][$key] = check_plain($value);
  548. $component_export['selected']['sources'][$key] = FALSE;
  549. }
  550. }
  551. }
  552. }
  553. $export['components'][$component] = $component_export;
  554. }
  555. $export['features_exclude'] = $excluded;
  556. // make excluded list and conflicts available for javascript to pass to our ajax callback
  557. drupal_add_js(array('features' => array(
  558. 'excluded' => $excluded,
  559. 'conflicts' => $conflicts,
  560. )), 'setting');
  561. return $export;
  562. }
  563. /**
  564. * AJAX callback for features_export_form.
  565. */
  566. function features_export_form_ajax($form, &$form_state) {
  567. return $form['export'];
  568. }
  569. /**
  570. * Tells the ajax form submission to rebuild form state.
  571. */
  572. function features_export_form_rebuild($form, &$form_state) {
  573. $form_state['rebuild'] = TRUE;
  574. }
  575. function features_export_components_json($feature_name) {
  576. module_load_include('inc', 'features', 'features.export');
  577. $export = array();
  578. if (!empty($_POST['items'])) {
  579. $excluded = (!empty($_POST['excluded'])) ? $_POST['excluded'] : array();
  580. $stub = array();
  581. foreach ($_POST['items'] as $key) {
  582. preg_match('/^([^\[]+)(\[.+\])?\[(.+)\]\[(.+)\]$/', $key, $matches);
  583. if (!empty($matches[1]) && !empty($matches[4])) {
  584. $component = $matches[1];
  585. $item = features_dom_decode($matches[4]);
  586. if (empty($stub[$component])) {
  587. $stub[$component] = array($item);
  588. }
  589. else {
  590. $stub[$component] = array_merge($stub[$component], array($item));
  591. }
  592. }
  593. }
  594. $export = features_populate(array('features' => $stub, 'dependencies' => $stub['dependencies']), $feature_name);
  595. $export['features']['dependencies'] = $export['dependencies'];
  596. // uncheck any detected item that is in the excluded list
  597. foreach ($export['features'] as $component => $value) {
  598. foreach ($value as $key => $item) {
  599. $clean_key = features_dom_encode($key);
  600. if ($key != $clean_key) {
  601. // need to move key to a cleankey for javascript
  602. $export['features'][$component][$clean_key] = $export['features'][$component][$key];
  603. unset($export['features'][$component][$key]);
  604. }
  605. if (isset($excluded[$component][$key])) {
  606. $export['features'][$component][$clean_key] = FALSE;
  607. }
  608. }
  609. }
  610. }
  611. print drupal_json_encode($export['features']);
  612. }
  613. /**
  614. * AJAX callback to get .info file preview.
  615. */
  616. function features_info_file_preview($form, &$form_state){
  617. return $form['export'];
  618. }
  619. /**
  620. * Render API callback: Validates a project field.
  621. *
  622. * This function is assigned as an #element_validate callback in
  623. * features_export_form().
  624. */
  625. function features_export_form_validate_field($element, &$form_state) {
  626. switch ($element['#name']) {
  627. case 'module_name':
  628. if (!preg_match('!^[a-z0-9_]+$!', $element['#value'])) {
  629. form_error($element, t('The machine-readable name must contain only lowercase letters, numbers, and underscores.'));
  630. }
  631. // If user is filling out the feature name for the first time and uses
  632. // the name of an existing module throw an error.
  633. else if (empty($element['#default_value']) && features_get_info('module', $element['#value'])) {
  634. form_error($element, t('A module by the name @name already exists on your site. Please choose a different name.', array('@name' => $element['#value'])));
  635. }
  636. break;
  637. case 'project_status_url':
  638. if (!empty($element['#value']) && !valid_url($element['#value'])) {
  639. form_error($element, t('The URL %url is invalid. Please enter a fully-qualified URL, such as http://www.example.com/feed.xml.', array('%url' => $element['#value'])));
  640. }
  641. break;
  642. case 'version':
  643. preg_match('/^(?P<core>\d+\.x)-(?P<major>\d+)\.(?P<patch>\d+)-?(?P<extra>\w+)?$/', $element['#value'], $matches);
  644. if (!empty($element['#value']) && !isset($matches['core'], $matches['major'])) {
  645. form_error($element, t('Please enter a valid version with core and major version number. Example: @example', array('@example' => '7.x-1.0')));
  646. };
  647. break;
  648. }
  649. }
  650. /**
  651. * Return the $export array to be rendered for the feature export
  652. */
  653. function _features_export_generate($export, $form_state, $feature = NULL) {
  654. unset($export['components']); // remove the UI data that we are not saving to disk
  655. $module_name = $form_state['values']['module_name'];
  656. // Directly copy the following attributes from form_state
  657. $attr = array('name', 'description', 'package', 'project path');
  658. foreach ($attr as $key) {
  659. $export[$key] = isset($form_state['values'][$key]) ? $form_state['values'][$key] : NULL;
  660. }
  661. // Directly copy the following attributes from the original feature
  662. $attr = array('scripts' , 'stylesheets');
  663. foreach ($attr as $key) {
  664. $export[$key] = isset($feature->info[$key]) ? $feature->info[$key] : NULL;
  665. }
  666. // If either update status-related keys are provided, add a project key
  667. // corresponding to the module name.
  668. if (!empty($form_state['values']['version']) || !empty($form_state['values']['project_status_url'])) {
  669. $export['project'] = $form_state['values']['module_name'];
  670. }
  671. if (!empty($form_state['values']['version'])) {
  672. $export['version'] = $form_state['values']['version'];
  673. }
  674. if (!empty($form_state['values']['project_status_url'])) {
  675. $export['project status url'] = $form_state['values']['project_status_url'];
  676. }
  677. $export['no autodetect'] = empty($form_state['values']['autodetect']) ? 1 : NULL;
  678. $export['project path'] = !empty($form_state['values']['generate_path']) ? $form_state['values']['generate_path'] : NULL;
  679. return $export;
  680. }
  681. /**
  682. * Form submission handler for features_export_form().
  683. */
  684. function features_export_build_form_submit($form, &$form_state) {
  685. $feature = $form['#feature'];
  686. $export = _features_export_build($feature, $form_state);
  687. $export = _features_export_generate($export, $form_state, $feature);
  688. $generate = ($form_state['values']['op'] == $form_state['values']['generate']);
  689. $module_name = $form_state['values']['module_name'];
  690. // Generate download
  691. if ($files = features_export_render($export, $module_name, TRUE)) {
  692. $filename = (!empty($export['version']) ? "{$module_name}-{$export['version']}" : $module_name) . '.tar';
  693. if ($generate) {
  694. $success = TRUE;
  695. $destination = 'sites/all/modules';
  696. $directory = (!empty($export['project path'])) ? $export['project path'] . '/' . $module_name :
  697. (isset($feature->filename) ? dirname($feature->filename) : $destination . '/' . $module_name);
  698. if (!is_dir($directory)) {
  699. if (mkdir($directory, 0777, true) === FALSE) {
  700. $success = FALSE;
  701. }
  702. }
  703. }
  704. else {
  705. // Clear out output buffer to remove any garbage from tar output.
  706. if (ob_get_level()) {
  707. ob_end_clean();
  708. }
  709. drupal_add_http_header('Content-type', 'application/x-tar');
  710. drupal_add_http_header('Content-Disposition', 'attachment; filename="'. $filename .'"');
  711. drupal_send_headers();
  712. }
  713. $tar = array();
  714. $filenames = array();
  715. foreach ($files as $extension => $file_contents) {
  716. if (!in_array($extension, array('module', 'info'))) {
  717. $extension .= '.inc';
  718. }
  719. $filenames[] = "{$module_name}.$extension";
  720. if ($generate) {
  721. if (file_put_contents("{$directory}/{$module_name}.$extension", $file_contents) === FALSE) {
  722. $success = FALSE;
  723. }
  724. }
  725. else {
  726. print features_tar_create("{$module_name}/{$module_name}.$extension", $file_contents);
  727. }
  728. }
  729. if (features_get_modules($module_name, TRUE)) {
  730. $module_path = drupal_get_path('module', $module_name);
  731. // file_scan_directory() can throw warnings when using PHP 5.3, messing
  732. // up the output of our file stream. Suppress errors in this one case in
  733. // order to produce valid output.
  734. foreach (@file_scan_directory($module_path, '/.*/') as $file) {
  735. $filename = substr($file->uri, strlen($module_path) + 1);
  736. if (!in_array($filename, $filenames)) {
  737. // Add this file.
  738. $contents = file_get_contents($file->uri);
  739. if ($generate) {
  740. if (file_put_contents("{$directory}/{$filename}", $contents) === FALSE) {
  741. $success = FALSE;
  742. }
  743. }
  744. else {
  745. print features_tar_create("{$module_name}/{$filename}", $contents);
  746. }
  747. unset($contents);
  748. }
  749. }
  750. }
  751. if ($generate) {
  752. if ($success) {
  753. drupal_set_message(t("Module @name written to @directory",
  754. array('@name' => $export['name'], '@directory' => $directory)));
  755. }
  756. else {
  757. drupal_set_message(
  758. t("Could not write module to @path. ", array('@path' => $directory)) .
  759. t("Ensure your file permissions allow the web server to write to that directory."), "error");
  760. }
  761. }
  762. else {
  763. print pack("a1024","");
  764. exit;
  765. }
  766. }
  767. }
  768. /**
  769. * array_filter() callback for excluding hidden modules.
  770. */
  771. function features_filter_hidden($module) {
  772. return empty($module->info['hidden']);
  773. }
  774. /**
  775. * Form constructor for the features configuration form.
  776. */
  777. function features_admin_form($form, $form_state) {
  778. // Load export functions to use in comparison.
  779. module_load_include('inc', 'features', 'features.export');
  780. // Clear & rebuild key caches
  781. features_get_info(NULL, NULL, TRUE);
  782. features_rebuild();
  783. $modules = array_filter(features_get_modules(), 'features_filter_hidden');
  784. $features = array_filter(features_get_features(), 'features_filter_hidden');
  785. $conflicts = features_get_conflicts();
  786. foreach ($modules as $key => $module) {
  787. if ($module->status && !empty($module->info['dependencies'])) {
  788. foreach ($module->info['dependencies'] as $dependent) {
  789. if (isset($features[$dependent])) {
  790. $features[$dependent]->dependents[$key] = $module->info['name'];
  791. }
  792. }
  793. }
  794. }
  795. if ( empty($features) ) {
  796. $form['no_features'] = array(
  797. '#markup' => t('No Features were found. Please use the !create_link link to create
  798. a new Feature module, or upload an existing Feature to your modules directory.',
  799. array('!create_link' => l(t('Create Feature'), 'admin/structure/features/create'))),
  800. );
  801. return $form ;
  802. }
  803. $form = array('#features' => $features);
  804. // Generate features form. Features are sorted by dependencies, resort alpha
  805. ksort($features);
  806. foreach ($features as $name => $module) {
  807. $package_title = !empty($module->info['package']) ? $module->info['package'] : t('Other');
  808. $package = strtolower(preg_replace('/[^a-zA-Z0-9-]+/', '-', $package_title));
  809. // Set up package elements
  810. if (!isset($form[$package])) {
  811. $form[$package] = array(
  812. '#tree' => FALSE,
  813. '#title' => check_plain($package_title),
  814. '#theme' => 'features_form_package',
  815. '#type' => 'fieldset',
  816. '#group' => 'packages',
  817. );
  818. $form[$package]['links'] =
  819. $form[$package]['version'] =
  820. $form[$package]['weight'] =
  821. $form[$package]['status'] =
  822. $form[$package]['action'] = array('#tree' => TRUE);
  823. }
  824. $disabled = FALSE;
  825. $description = isset($module->info['description']) ? check_plain($module->info['description']) : '';
  826. // Detect unmet dependencies
  827. if (!empty($module->info['dependencies'])) {
  828. $unmet_dependencies = array();
  829. $dependencies = _features_export_maximize_dependencies($module->info['dependencies']);
  830. foreach ($dependencies as $dependency) {
  831. if (empty($modules[$dependency])) {
  832. $unmet_dependencies[] = theme('features_module_status', array('status' => FEATURES_MODULE_MISSING, 'module' => $dependency));
  833. }
  834. }
  835. if (!empty($unmet_dependencies)) {
  836. $description .= "<div class='dependencies'>" . t('Unmet dependencies: @dependencies', array('@dependencies' => implode(', ', $unmet_dependencies))) . "</div>";
  837. $disabled = TRUE;
  838. }
  839. }
  840. if (!empty($module->dependents)) {
  841. $disabled = TRUE;
  842. $description .= "<div class='requirements'>". t('Required by: @dependents', array('@dependents' => implode(', ', $module->dependents))) ."</div>";
  843. }
  844. // Detect potential conflicts
  845. if (!empty($conflicts[$name])) {
  846. $module_conflicts = array();
  847. foreach (array_keys($conflicts[$name]) as $conflict) {
  848. // If conflicting module is disabled, indicate so in feature listing
  849. $status = !module_exists($conflict) ? FEATURES_MODULE_DISABLED : FEATURES_MODULE_CONFLICT;
  850. $module_conflicts[] = theme('features_module_status', array('status' => $status, 'module' => $conflict));
  851. // Only disable modules with conflicts if they are not already enabled.
  852. // If they are already enabled, somehow the user got themselves into a
  853. // bad situation and they need to be able to disable a conflicted module.
  854. if (module_exists($conflict) && !module_exists($name)) {
  855. $disabled = TRUE;
  856. }
  857. }
  858. $description .= "<div class='conflicts'>". t('Conflicts with: !conflicts', array('!conflicts' => implode(', ', $module_conflicts))) ."</div>";
  859. }
  860. $href = "admin/structure/features/{$name}";
  861. $module_name = (user_access('administer features')) ? l($module->info['name'], $href) : $module->info['name'];
  862. $form[$package]['status'][$name] = array(
  863. '#type' => 'checkbox',
  864. '#title' => $module_name,
  865. '#description' => $description,
  866. '#default_value' => $module->status,
  867. '#disabled' => $disabled,
  868. );
  869. if (!empty($module->info['project status url'])) {
  870. $uri = l(truncate_utf8($module->info['project status url'], 35, TRUE, TRUE), $module->info['project status url']);
  871. }
  872. else if (isset($module->info['project'], $module->info['version'], $module->info['datestamp'])) {
  873. $uri = l('http://drupal.org', 'http://drupal.org/project/' . $module->info['project']);
  874. }
  875. else {
  876. $uri = t('Unavailable');
  877. }
  878. $version = !empty($module->info['version']) ? $module->info['version'] : '';
  879. $version = !empty($version) ? "<div class='description'>$version</div>" : '';
  880. $form[$package]['sign'][$name] = array('#markup' => "{$uri} {$version}");
  881. if (user_access('administer features')) {
  882. // Add status link
  883. if ($module->status) {
  884. $state = theme('features_storage_link', array('storage' => FEATURES_CHECKING, 'path' => $href));
  885. $state .= l(t('Check'), "admin/structure/features/{$name}/status", array('attributes' => array('class' => array('admin-check'))));
  886. $state .= theme('features_storage_link', array('storage' => FEATURES_REBUILDING, 'path' => $href));
  887. $state .= theme('features_storage_link', array('storage' => FEATURES_NEEDS_REVIEW, 'path' => $href));
  888. $state .= theme('features_storage_link', array('storage' => FEATURES_OVERRIDDEN, 'path' => $href));
  889. $state .= theme('features_storage_link', array('storage' => FEATURES_DEFAULT, 'path' => $href));
  890. }
  891. elseif (!empty($conflicts[$name])) {
  892. $state = theme('features_storage_link', array('storage' => FEATURES_CONFLICT, 'path' => $href));
  893. }
  894. else {
  895. $state = theme('features_storage_link', array('storage' => FEATURES_DISABLED, 'path' => $href));
  896. }
  897. $form[$package]['state'][$name] = array(
  898. '#markup' => !empty($state) ? $state : '',
  899. );
  900. // Add in recreate link
  901. $form[$package]['actions'][$name] = array(
  902. '#markup' => l(t('Recreate'), "admin/structure/features/{$name}/recreate", array('attributes' => array('class' => array('admin-update')))),
  903. );
  904. }
  905. }
  906. ksort($form);
  907. // As of 7.0 beta 2 it matters where the "vertical_tabs" element lives on the
  908. // the array. We add it late, but at the beginning of the array because that
  909. // keeps us away from trouble.
  910. $form = array('packages' => array('#type' => 'vertical_tabs')) + $form;
  911. $form['buttons'] = array(
  912. '#theme' => 'features_form_buttons',
  913. );
  914. $form['buttons']['submit'] = array(
  915. '#type' => 'submit',
  916. '#value' => t('Save settings'),
  917. '#submit' => array('features_form_submit'),
  918. '#validate' => array('features_form_validate'),
  919. );
  920. return $form;
  921. }
  922. /**
  923. * Display the components of a feature.
  924. */
  925. function features_admin_components($form, $form_state, $feature) {
  926. // Breadcrumb navigation
  927. $breadcrumb[] = l(t('Home'), NULL);
  928. $breadcrumb[] = l(t('Features'), 'admin/structure/features');
  929. drupal_set_breadcrumb($breadcrumb);
  930. module_load_include('inc', 'features', 'features.export');
  931. $form = array();
  932. // Store feature info for theme layer.
  933. $form['module'] = array('#type' => 'value', '#value' => $feature->name);
  934. $form['#info'] = $feature->info;
  935. $form['#dependencies'] = array();
  936. if (!empty($feature->info['dependencies'])) {
  937. foreach ($feature->info['dependencies'] as $dependency) {
  938. $parsed_dependency = drupal_parse_dependency($dependency);
  939. $dependency = $parsed_dependency['name'];
  940. $status = features_get_module_status($dependency);
  941. $form['#dependencies'][$dependency] = $status;
  942. }
  943. }
  944. $conflicts = features_get_conflicts();
  945. if (!module_exists($form['module']['#value']) && isset($form['module']['#value']) && !empty($conflicts[$form['module']['#value']])) {
  946. $module_conflicts = $conflicts[$form['module']['#value']];
  947. $conflicts = array();
  948. foreach ($module_conflicts as $conflict) {
  949. $conflicts = array_merge_recursive($conflict, $conflicts);
  950. }
  951. }
  952. else {
  953. $conflicts = array();
  954. }
  955. $form['#conflicts'] = $conflicts;
  956. $review = $revert = FALSE;
  957. // Iterate over components and retrieve status for display
  958. $states = features_get_component_states(array($feature->name), FALSE);
  959. $form['revert']['#tree'] = TRUE;
  960. foreach ($feature->info['features'] as $component => $items) {
  961. if (user_access('administer features') && array_key_exists($component, $states[$feature->name]) && in_array($states[$feature->name][$component], array(FEATURES_OVERRIDDEN, FEATURES_NEEDS_REVIEW))) {
  962. switch ($states[$feature->name][$component]) {
  963. case FEATURES_OVERRIDDEN:
  964. $revert = TRUE;
  965. break;
  966. case FEATURES_NEEDS_REVIEW:
  967. $review = TRUE;
  968. break;
  969. }
  970. $form['revert'][$component] = array(
  971. '#type' => 'checkbox',
  972. '#default_value' => FALSE,
  973. );
  974. }
  975. if (module_exists('diff')) {
  976. $diffpath = "admin/structure/features/{$feature->name}/diff/{$component}";
  977. $item = menu_get_item($diffpath);
  978. $path = ($item && $item['access']) ? $diffpath : NULL;
  979. }
  980. else {
  981. $path = NULL;
  982. }
  983. $storage = FEATURES_DEFAULT;
  984. if (array_key_exists($component, $states[$feature->name])) {
  985. $storage = $states[$feature->name][$component];
  986. }
  987. else if (array_key_exists($component, $conflicts)) {
  988. $storage = FEATURES_CONFLICT;
  989. }
  990. $form['components'][$component] = array(
  991. '#markup' => theme('features_storage_link', array('storage' => $storage, 'path' => $path)),
  992. );
  993. }
  994. if ($review || $revert) {
  995. $form['buttons'] = array('#theme' => 'features_form_buttons', '#tree' => TRUE);
  996. if ($revert || $review) {
  997. $form['buttons']['revert'] = array(
  998. '#type' => 'submit',
  999. '#value' => t('Revert components'),
  1000. '#submit' => array('features_admin_components_revert'),
  1001. );
  1002. }
  1003. if ($review) {
  1004. $form['buttons']['review'] = array(
  1005. '#type' => 'submit',
  1006. '#value' => t('Mark as reviewed'),
  1007. '#submit' => array('features_admin_components_review'),
  1008. );
  1009. }
  1010. }
  1011. return $form;
  1012. }
  1013. /**
  1014. * Submit handler for revert form.
  1015. */
  1016. function features_admin_components_revert(&$form, &$form_state) {
  1017. module_load_include('inc', 'features', 'features.export');
  1018. features_include();
  1019. $module = $form_state['values']['module'];
  1020. $revert = array();
  1021. foreach (array_filter($form_state['values']['revert']) as $component => $status) {
  1022. $revert[$module][] = $component;
  1023. drupal_set_message(t('Reverted all <strong>@component</strong> components for <strong>@module</strong>.', array('@component' => $component, '@module' => $module)));
  1024. }
  1025. features_revert($revert);
  1026. $form_state['redirect'] = 'admin/structure/features/' . $module;
  1027. }
  1028. /**
  1029. * Submit handler for revert form.
  1030. */
  1031. function features_admin_components_review(&$form, &$form_state) {
  1032. module_load_include('inc', 'features', 'features.export');
  1033. features_include();
  1034. $module = $form_state['values']['module'];
  1035. $revert = array();
  1036. foreach (array_filter($form_state['values']['revert']) as $component => $status) {
  1037. features_set_signature($module, $component);
  1038. drupal_set_message(t('All <strong>@component</strong> components for <strong>@module</strong> reviewed.', array('@component' => $component, '@module' => $module)));
  1039. }
  1040. $form_state['redirect'] = 'admin/structure/features/' . $module;
  1041. }
  1042. /**
  1043. * Validate handler for the 'manage features' form.
  1044. */
  1045. function features_form_validate(&$form, &$form_state) {
  1046. include_once './includes/install.inc';
  1047. $conflicts = features_get_conflicts();
  1048. foreach ($form_state['values']['status'] as $module => $status) {
  1049. if ($status) {
  1050. if (!empty($conflicts[$module])) {
  1051. foreach (array_keys($conflicts[$module]) as $conflict) {
  1052. if (!empty($form_state['values']['status'][$conflict])) {
  1053. form_set_error('status', t('The feature @module cannot be enabled because it conflicts with @conflict.', array('@module' => $module, '@conflict' => $conflict)));
  1054. }
  1055. }
  1056. }
  1057. if (!drupal_check_module($module)) {
  1058. form_set_error('status', t('The feature @module cannot be enabled because it has unmet requirements.', array('@module' => $module)));
  1059. }
  1060. }
  1061. }
  1062. }
  1063. /**
  1064. * Submit handler for the 'manage features' form
  1065. */
  1066. function features_form_submit(&$form, &$form_state) {
  1067. // Clear drupal caches after enabling a feature. We do this in a separate
  1068. // page callback rather than as part of the submit handler as some modules
  1069. // have includes/other directives of importance in hooks that have already
  1070. // been called in this page load.
  1071. $form_state['redirect'] = 'admin/structure/features/cleanup/clear';
  1072. $features = $form['#features'];
  1073. if (!empty($features)) {
  1074. $status = $form_state['values']['status'];
  1075. $install = array_keys(array_filter($status));
  1076. $disable = array_diff(array_keys($status), $install);
  1077. // Disable first. If there are any features that are disabled that are
  1078. // dependencies of features that have been queued for install, they will
  1079. // be re-enabled.
  1080. module_disable($disable);
  1081. features_install_modules($install);
  1082. }
  1083. }
  1084. /**
  1085. * Form for disabling orphaned dependencies.
  1086. */
  1087. function features_cleanup_form($form, $form_state, $cache_clear = FALSE) {
  1088. $form = array();
  1089. // Clear caches if we're getting a post-submit redirect that requests it.
  1090. if ($cache_clear) {
  1091. drupal_flush_all_caches();
  1092. // The following functions need to be run because drupal_flush_all_caches()
  1093. // runs rebuilds in the wrong order. The node type cache is rebuilt *after*
  1094. // the menu is rebuilt, meaning that the menu tree is stale in certain
  1095. // circumstances after drupal_flush_all_caches(). We rebuild again.
  1096. menu_rebuild();
  1097. }
  1098. // Retrieve orphaned modules and provide them as optional modules to be disabled.
  1099. // Exclude any modules that have been added to the 'ignored' list.
  1100. $options = array();
  1101. $orphans = features_get_orphans();
  1102. $ignored = variable_get('features_ignored_orphans', array());
  1103. if (!empty($orphans)) {
  1104. foreach ($orphans as $module) {
  1105. if (!in_array($module->name, $ignored, TRUE)) {
  1106. $options[$module->name] = check_plain($module->info['name']);
  1107. }
  1108. }
  1109. }
  1110. if (!empty($options)) {
  1111. $form['orphans'] = array(
  1112. '#title' => t('Orphaned dependencies'),
  1113. '#description' => t('These modules are dependencies of features that have been disabled. They may be disabled without affecting other components of your website.'),
  1114. '#type' => 'checkboxes',
  1115. '#options' => $options,
  1116. '#default_value' => array_keys($options),
  1117. );
  1118. $form['buttons'] = array('#tree' => TRUE, '#theme' => 'features_form_buttons');
  1119. $form['buttons']['disable'] = array(
  1120. '#type' => 'submit',
  1121. '#value' => t('Disable selected modules'),
  1122. '#submit' => array('features_cleanup_form_disable'),
  1123. );
  1124. $form['buttons']['ignore'] = array(
  1125. '#type' => 'submit',
  1126. '#value' => t('Leave enabled'),
  1127. '#submit' => array('features_cleanup_form_ignore'),
  1128. );
  1129. }
  1130. else {
  1131. drupal_goto('admin/structure/features');
  1132. }
  1133. return $form;
  1134. }
  1135. /**
  1136. * Submit handler for disable action on features_cleanup_form().
  1137. */
  1138. function features_cleanup_form_disable(&$form, &$form_state) {
  1139. if (!empty($form_state['values']['orphans'])) {
  1140. $disable = array_keys(array_filter($form_state['values']['orphans']));
  1141. $ignored = array_diff(array_keys($form_state['values']['orphans']), $disable);
  1142. // Disable any orphans that have been selected.
  1143. module_disable($disable);
  1144. drupal_flush_all_caches();
  1145. // Add enabled modules to ignored orphans list.
  1146. $ignored_orphans = variable_get('features_ignored_orphans', array());
  1147. foreach ($ignored as $module) {
  1148. $ignored_orphans[$module] = $module;
  1149. }
  1150. variable_set('features_ignored_orphans', $ignored_orphans);
  1151. }
  1152. $form_state['redirect'] = 'admin/structure/features/cleanup';
  1153. }
  1154. /**
  1155. * Submit handler for ignore action on features_cleanup_form().
  1156. */
  1157. function features_cleanup_form_ignore(&$form, &$form_state) {
  1158. if (!empty($form_state['values']['orphans'])) {
  1159. $ignored = array_keys($form_state['values']['orphans']);
  1160. $ignored_orphans = variable_get('features_ignored_orphans', array());
  1161. foreach ($ignored as $module) {
  1162. $ignored_orphans[$module] = $module;
  1163. }
  1164. variable_set('features_ignored_orphans', $ignored_orphans);
  1165. }
  1166. $form_state['redirect'] = 'admin/structure/features/cleanup';
  1167. }
  1168. /**
  1169. * Page callback to display the differences between what's in code and
  1170. * what is in the db.
  1171. *
  1172. * @param $feature
  1173. * A loaded feature object to display differences for.
  1174. * @param $component
  1175. * (optional) Specific component to display differences for. If excluded, all
  1176. * components are used.
  1177. *
  1178. * @return
  1179. * Themed display of what is different.
  1180. */
  1181. function features_feature_diff($feature, $component = NULL) {
  1182. drupal_add_css(drupal_get_path('module', 'features') . '/features.css');
  1183. module_load_include('inc', 'features', 'features.export');
  1184. $overrides = features_detect_overrides($feature);
  1185. $output = '';
  1186. if (!empty($overrides)) {
  1187. // Filter overrides down to specified component.
  1188. if (isset($component) && isset($overrides[$component])) {
  1189. $overrides = array($component => $overrides[$component]);
  1190. }
  1191. module_load_include('inc', 'diff', 'diff.engine');
  1192. $formatter = new DrupalDiffFormatter();
  1193. $rows = array();
  1194. foreach ($overrides as $component => $items) {
  1195. $rows[] = array(array('data' => $component, 'colspan' => 4, 'header' => TRUE));
  1196. $diff = new Diff(explode("\n", $items['default']), explode("\n", $items['normal']));
  1197. $rows = array_merge($rows, $formatter->format($diff));
  1198. }
  1199. $header = array(
  1200. array('data' => t('Default'), 'colspan' => 2),
  1201. array('data' => t('Overrides'), 'colspan' => 2),
  1202. );
  1203. $output .= theme('table', array('header' => $header, 'rows' => $rows, 'attributes' => array('class' => array('diff', 'features-diff'))));
  1204. }
  1205. else {
  1206. $output = "<div class='features-empty'>" . t('No changes have been made to this feature.') . "</div>";
  1207. }
  1208. $output = array('page' => array('#markup' => "<div class='features-comparison'>{$output}</div>"));
  1209. return $output;
  1210. }
  1211. /**
  1212. * Compare the component names. Used to sort alphabetically.
  1213. */
  1214. function features_compare_component_name($a, $b) {
  1215. return strcasecmp($a['name'], $b['name']);
  1216. }
  1217. /**
  1218. * Javascript callback that returns the status of a feature.
  1219. */
  1220. function features_feature_status($feature) {
  1221. module_load_include('inc', 'features', 'features.export');
  1222. return drupal_json_output(array('storage' => features_get_storage($feature->name)));
  1223. }
  1224. /**
  1225. * Make a Drupal options array safe for usage with jQuery DOM selectors.
  1226. * Encodes known bad characters into __[ordinal]__ so that they may be
  1227. * safely referenced by JS behaviors.
  1228. */
  1229. function features_dom_encode_options($options = array(), $keys_only = TRUE) {
  1230. $replacements = features_dom_encode_map();
  1231. $encoded = array();
  1232. foreach ($options as $key => $value) {
  1233. $encoded[strtr($key, $replacements)] = $keys_only ? $value : strtr($value, $replacements);
  1234. }
  1235. return $encoded;
  1236. }
  1237. function features_dom_encode($key) {
  1238. $replacements = features_dom_encode_map();
  1239. return strtr($key, $replacements);
  1240. }
  1241. function features_dom_decode($key) {
  1242. $replacements = array_flip(features_dom_encode_map());
  1243. return strtr($key, $replacements);
  1244. }
  1245. /**
  1246. * Decode an array of option values that have been encoded by
  1247. * features_dom_encode_options().
  1248. */
  1249. function features_dom_decode_options($options, $keys_only = FALSE) {
  1250. $replacements = array_flip(features_dom_encode_map());
  1251. $encoded = array();
  1252. foreach ($options as $key => $value) {
  1253. $encoded[strtr($key, $replacements)] = $keys_only ? $value : strtr($value, $replacements);
  1254. }
  1255. return $encoded;
  1256. }
  1257. /**
  1258. * Returns encoding map for decode and encode options.
  1259. */
  1260. function features_dom_encode_map() {
  1261. return array(
  1262. ':' => '__' . ord(':') . '__',
  1263. '/' => '__' . ord('/') . '__',
  1264. ',' => '__' . ord(',') . '__',
  1265. '.' => '__' . ord('.') . '__',
  1266. '<' => '__' . ord('<') . '__',
  1267. '>' => '__' . ord('>') . '__',
  1268. '%' => '__' . ord('%') . '__',
  1269. ')' => '__' . ord(')') . '__',
  1270. '(' => '__' . ord('(') . '__',
  1271. );
  1272. }
  1273. /**
  1274. * Page callback: Autocomplete field for features package.
  1275. *
  1276. * @param $search_string
  1277. * The char or string that user have written in autocomplete field,
  1278. * this is the string this function uses for filter.
  1279. *
  1280. * @see features_menu()
  1281. */
  1282. function features_autocomplete_packages($search_string) {
  1283. $matched_packages = array();
  1284. //fetch all modules that are features and copy the package name into a new array.
  1285. foreach (features_get_features(NULL, TRUE) as $value) {
  1286. if (preg_match('/' . $search_string . '/i', $value->info['package'])) {
  1287. $matched_packages[$value->info['package']] = $value->info['package'];
  1288. }
  1289. }
  1290. //removes duplicated package, we wont a list of all unique packages.
  1291. $matched_packages = array_unique($matched_packages);
  1292. drupal_json_output($matched_packages);
  1293. }
  1294. /**
  1295. * Return a list of all used components/items not matching a given feature module
  1296. * similar to features_get_conflicts but returns all component items "in use"
  1297. */
  1298. function _features_get_used($module_name = NULL) {
  1299. global $features_ignore_conflicts;
  1300. // make sure we turn off the ignore_conflicts global to get full list of used components
  1301. // hate to use global, but since this is just for an admin screen it's not a real problem
  1302. $old_value = $features_ignore_conflicts;
  1303. $features_ignore_conflicts = FALSE;
  1304. $conflicts = array();
  1305. $component_info = features_get_components();
  1306. $map = features_get_component_map();
  1307. foreach ($map as $type => $components) {
  1308. // Only check conflicts for components we know about.
  1309. if (isset($component_info[$type])) {
  1310. foreach ($components as $component => $modules) {
  1311. foreach ($modules as $module) {
  1312. // only for enabled modules
  1313. if (module_exists($module) && (empty($module_name) || ($module_name != $module))) {
  1314. if (!isset($conflicts[$module])) {
  1315. $conflicts[$module] = array();
  1316. }
  1317. $conflicts[$module][$type][] = $component;
  1318. }
  1319. }
  1320. }
  1321. }
  1322. }
  1323. // restore previous value of global
  1324. $features_ignore_conflicts = $old_value;
  1325. return $conflicts;
  1326. }