features.admin.inc 66 KB

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