features.admin.inc 65 KB

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