features.admin.inc 74 KB

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