features.admin.inc 59 KB

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