features.admin.inc 63 KB

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