ctools.module 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202
  1. <?php
  2. /**
  3. * @file
  4. * CTools primary module file.
  5. *
  6. * Most of the CTools tools are in their own .inc files. This contains
  7. * nothing more than a few convenience functions and some hooks that
  8. * must be implemented in the module file.
  9. */
  10. define('CTOOLS_API_VERSION', '2.0.9');
  11. /**
  12. * The current working ctools version.
  13. *
  14. * In a release, it should be 7.x-1.x, which should match what drush make will
  15. * create. In a dev format, it should be 7.x-1.(x+1)-dev, which will allow
  16. * modules depending on new features in ctools to depend on ctools > 7.x-1.x.
  17. *
  18. * To define a specific version of CTools as a dependency for another module,
  19. * simply include a dependency line in that module's info file, e.g.:
  20. * ; Requires CTools v7.x-1.4 or newer.
  21. * dependencies[] = ctools (>=1.4)
  22. */
  23. define('CTOOLS_MODULE_VERSION', '7.x-1.13');
  24. /**
  25. * Test the CTools API version.
  26. *
  27. * This function can always be used to safely test if CTools has the minimum
  28. * API version that your module can use. It can also try to protect you from
  29. * running if the CTools API version is too new, but if you do that you need
  30. * to be very quick about watching CTools API releases and release new versions
  31. * of your software as soon as the new release is made, or people might end up
  32. * updating CTools and having your module shut down without any recourse.
  33. *
  34. * It is recommended that every hook of your module that might use CTools or
  35. * might lead to a use of CTools be guarded like this:
  36. *
  37. * @code
  38. * if (!module_invoke('ctools', 'api_version', '1.0')) {
  39. * return;
  40. * }
  41. * @endcode
  42. *
  43. * Note that some hooks such as _menu() or _theme() must return an array().
  44. *
  45. * You can use it in your hook_requirements to report this error condition
  46. * like this:
  47. *
  48. * @code
  49. * define('MODULENAME_MINIMUM_CTOOLS_API_VERSION', '1.0');
  50. * define('MODULENAME_MAXIMUM_CTOOLS_API_VERSION', '1.1');
  51. *
  52. * function MODULENAME_requirements($phase) {
  53. * $requirements = array();
  54. * if (!module_invoke('ctools', 'api_version', MODULENAME_MINIMUM_CTOOLS_API_VERSION, MODULENAME_MAXIMUM_CTOOLS_API_VERSION)) {
  55. * $requirements['MODULENAME_ctools'] = array(
  56. * 'title' => $t('MODULENAME required Chaos Tool Suite (CTools) API Version'),
  57. * 'value' => t('Between @a and @b', array('@a' => MODULENAME_MINIMUM_CTOOLS_API_VERSION, '@b' => MODULENAME_MAXIMUM_CTOOLS_API_VERSION)),
  58. * 'severity' => REQUIREMENT_ERROR,
  59. * );
  60. * }
  61. * return $requirements;
  62. * }
  63. * @endcode
  64. *
  65. * Please note that the version is a string, not an floating point number.
  66. * This will matter once CTools reaches version 1.10.
  67. *
  68. * A CTools API changes history will be kept in API.txt. Not every new
  69. * version of CTools will necessarily update the API version.
  70. * @param $minimum
  71. * The minimum version of CTools necessary for your software to run with it.
  72. * @param $maximum
  73. * The maximum version of CTools allowed for your software to run with it.
  74. *
  75. * @return bool
  76. * TRUE if the running ctools is usable, FALSE otherwise.
  77. */
  78. function ctools_api_version($minimum, $maximum = NULL) {
  79. if (version_compare(CTOOLS_API_VERSION, $minimum, '<')) {
  80. return FALSE;
  81. }
  82. if (isset($maximum) && version_compare(CTOOLS_API_VERSION, $maximum, '>')) {
  83. return FALSE;
  84. }
  85. return TRUE;
  86. }
  87. // -----------------------------------------------------------------------
  88. // General utility functions.
  89. /**
  90. * Include .inc files as necessary.
  91. *
  92. * This fuction is helpful for including .inc files for your module. The
  93. * general case is including ctools funcitonality like this:
  94. *
  95. * @code
  96. * ctools_include('plugins');
  97. * @endcode
  98. *
  99. * Similar funcitonality can be used for other modules by providing the $module
  100. * and $dir arguments like this:
  101. *
  102. * @code
  103. * // include mymodule/includes/import.inc
  104. * ctools_include('import', 'mymodule');
  105. * // include mymodule/plugins/foobar.inc
  106. * ctools_include('foobar', 'mymodule', 'plugins');
  107. * @endcode
  108. *
  109. * @param $file
  110. * The base file name to be included.
  111. * @param $module
  112. * Optional module containing the include.
  113. * @param $dir
  114. * Optional subdirectory containing the include file.
  115. */
  116. function ctools_include($file, $module = 'ctools', $dir = 'includes') {
  117. static $used = array();
  118. $dir = '/' . ($dir ? $dir . '/' : '');
  119. if (!isset($used[$module][$dir][$file])) {
  120. require_once DRUPAL_ROOT . '/' . drupal_get_path('module', $module) . "$dir$file.inc";
  121. $used[$module][$dir][$file] = TRUE;
  122. }
  123. }
  124. /**
  125. * Include .inc files in a form context.
  126. *
  127. * This is a variant of ctools_include that will save information in the
  128. * the form_state so that cached forms will properly include things.
  129. */
  130. function ctools_form_include(&$form_state, $file, $module = 'ctools', $dir = 'includes') {
  131. if (!isset($form_state['build_info']['args'])) {
  132. $form_state['build_info']['args'] = array();
  133. }
  134. $dir = '/' . ($dir ? $dir . '/' : '');
  135. form_load_include($form_state, 'inc', $module, $dir . $file);
  136. }
  137. /**
  138. * Add an arbitrary path to the $form_state so it can work with form cache.
  139. *
  140. * The module_load_include() function uses an unfortunately annoying syntax to
  141. * work, making it difficult to translate the more simple $path + $file syntax.
  142. */
  143. function ctools_form_include_file(&$form_state, $filename) {
  144. if (!isset($form_state['build_info']['args'])) {
  145. $form_state['build_info']['args'] = array();
  146. }
  147. // Now add this to the build info files so that AJAX requests will know to load it.
  148. $form_state['build_info']['files']["$filename"] = $filename;
  149. require_once DRUPAL_ROOT . '/' . $filename;
  150. }
  151. /**
  152. * Provide the proper path to an image as necessary.
  153. *
  154. * This helper function is used by ctools but can also be used in other
  155. * modules in the same way as explained in the comments of ctools_include.
  156. *
  157. * @param $image
  158. * The base file name (with extension) of the image to be included.
  159. * @param $module
  160. * Optional module containing the include.
  161. * @param $dir
  162. * Optional subdirectory containing the include file.
  163. *
  164. * @return string
  165. * A string containing the appropriate path from drupal root.
  166. */
  167. function ctools_image_path($image, $module = 'ctools', $dir = 'images') {
  168. return drupal_get_path('module', $module) . "/$dir/" . $image;
  169. }
  170. /**
  171. * Include css files as necessary.
  172. *
  173. * This helper function is used by ctools but can also be used in other
  174. * modules in the same way as explained in the comments of ctools_include.
  175. *
  176. * @param $file
  177. * The base file name to be included.
  178. * @param $module
  179. * Optional module containing the include.
  180. * @param $dir
  181. * Optional subdirectory containing the include file.
  182. */
  183. function ctools_add_css($file, $module = 'ctools', $dir = 'css') {
  184. drupal_add_css(drupal_get_path('module', $module) . "/$dir/$file.css");
  185. }
  186. /**
  187. * Format a css file name for use with $form['#attached']['css'].
  188. *
  189. * This helper function is used by ctools but can also be used in other
  190. * modules in the same way as explained in the comments of ctools_include.
  191. *
  192. * @code
  193. * $form['#attached']['css'] = array(ctools_attach_css('collapsible-div'));
  194. * $form['#attached']['css'][ctools_attach_css('collapsible-div')] = array('preprocess' => FALSE);
  195. * @endcode
  196. *
  197. * @param $file
  198. * The base file name to be included.
  199. * @param $module
  200. * Optional module containing the include.
  201. * @param $dir
  202. * Optional subdirectory containing the include file.
  203. *
  204. * @return string
  205. * A string containing the appropriate path from drupal root.
  206. */
  207. function ctools_attach_css($file, $module = 'ctools', $dir = 'css') {
  208. return drupal_get_path('module', $module) . "/$dir/$file.css";
  209. }
  210. /**
  211. * Include js files as necessary.
  212. *
  213. * This helper function is used by ctools but can also be used in other
  214. * modules in the same way as explained in the comments of ctools_include.
  215. *
  216. * @param $file
  217. * The base file name to be included.
  218. * @param $module
  219. * Optional module containing the include.
  220. * @param $dir
  221. * Optional subdirectory containing the include file.
  222. */
  223. function ctools_add_js($file, $module = 'ctools', $dir = 'js') {
  224. drupal_add_js(drupal_get_path('module', $module) . "/$dir/$file.js");
  225. }
  226. /**
  227. * Format a javascript file name for use with $form['#attached']['js'].
  228. *
  229. * This helper function is used by ctools but can also be used in other
  230. * modules in the same way as explained in the comments of ctools_include.
  231. *
  232. * @code
  233. * $form['#attached']['js'] = array(ctools_attach_js('auto-submit'));
  234. * @endcode
  235. *
  236. * @param $file
  237. * The base file name to be included.
  238. * @param $module
  239. * Optional module containing the include.
  240. * @param $dir
  241. * Optional subdirectory containing the include file.
  242. *
  243. * @return string
  244. * A string containing the appropriate path from drupal root.
  245. */
  246. function ctools_attach_js($file, $module = 'ctools', $dir = 'js') {
  247. return drupal_get_path('module', $module) . "/$dir/$file.js";
  248. }
  249. /**
  250. * Get a list of roles in the system.
  251. *
  252. * @return
  253. * An array of role names keyed by role ID.
  254. *
  255. * @deprecated
  256. * user_roles() should be used instead.
  257. */
  258. function ctools_get_roles() {
  259. return user_roles();
  260. }
  261. /**
  262. * Parse integer sequences of the form "x,y,z" or "x+y+z" into separate values.
  263. *
  264. * A string with integers separated by comma (,) is reported as an 'and' set;
  265. * separation by a plus sign (+) or a space ( ) is an 'or' set. The meaning
  266. * of this is up to the caller. Negative or fractional numbers are not
  267. * recognised.
  268. *
  269. * Additional space characters within or around the sequence are not allowed.
  270. *
  271. * @param $str
  272. * The string to parse.
  273. *
  274. * @return object
  275. * An object containing the properties:
  276. *
  277. * - operator: Either 'and' or 'or' when there are multiple matched values.
  278. * Absent when invalid_input is TRUE or there is only one value.
  279. * - value: An array of integers (never strings) from $str. An empty array is
  280. * returned if the input is empty. A single integer input is returned
  281. * as a single value, but no 'operator' is defined.
  282. * - invalid_input: TRUE if input could not be parsed and the values array
  283. * will contain just -1. This property is otherwise absent.
  284. */
  285. function ctools_break_phrase($str) {
  286. $object = new stdClass();
  287. if (preg_match('/^([0-9]+[+ ])+[0-9]+$/', $str)) {
  288. // The '+' character in a query string may be parsed as ' '.
  289. $object->operator = 'or';
  290. $object->value = preg_split('/[+ ]/', $str);
  291. }
  292. elseif (preg_match('/^([0-9]+,)*[0-9]+$/', $str)) {
  293. $object->operator = 'and';
  294. $object->value = explode(',', $str);
  295. }
  296. // Keep an 'error' value if invalid strings were given.
  297. if (!empty($str) && (empty($object->value) || !is_array($object->value))) {
  298. $object->value = array(-1);
  299. $object->invalid_input = TRUE;
  300. return $object;
  301. }
  302. if (empty($object->value)) {
  303. $object->value = array();
  304. }
  305. // Doubly ensure that all values are numeric only.
  306. foreach ($object->value as $id => $value) {
  307. $object->value[$id] = (int) $value;
  308. }
  309. return $object;
  310. }
  311. /**
  312. * Set a token/value pair to be replaced later in the request, specifically in
  313. * ctools_page_token_processing().
  314. *
  315. * @param string $token
  316. * The token to be replaced later, during page rendering. This should
  317. * ideally be a string inside of an HTML comment, so that if there is
  318. * no replacement, the token will not render on the page.
  319. * If $token is NULL, the token set is not changed, but is still
  320. * returned.
  321. * @param string $type
  322. * The type of the token. Can be either 'variable', which will pull data
  323. * directly from the page variables, or 'callback', which causes a function
  324. * to be called to calculate the value. No other values are supported.
  325. * @param string|array $argument
  326. * For $type of:
  327. * - 'variable': argument should be the key to fetch from the $variables.
  328. * - 'callback': then it should either be the callback function name as a
  329. * string, or an array that will be sent to call_user_func_array(). Argument
  330. * arrays must not use array keys (i.e. $a[0] is the first and $a[1] the
  331. * second element, etc.)
  332. *
  333. * @return array
  334. * A array of token/variable names to be replaced.
  335. */
  336. function ctools_set_page_token($token = NULL, $type = NULL, $argument = NULL) {
  337. $tokens = &drupal_static('ctools_set_page_token', array());
  338. if (isset($token)) {
  339. $tokens[$token] = array($type, $argument);
  340. }
  341. return $tokens;
  342. }
  343. /**
  344. * Reset the defined page tokens within this request.
  345. *
  346. * Introduced for simpletest purposes. Normally not needed.
  347. */
  348. function ctools_reset_page_tokens() {
  349. drupal_static_reset('ctools_set_page_token');
  350. }
  351. /**
  352. * Set a replacement token from the containing element's children during #post_render.
  353. *
  354. * This function can be used like this:
  355. * $token = ctools_set_variable_token('tabs');
  356. *
  357. * The token "<!-- ctools-page-tabs -->" would then be replaced by the value of
  358. * this element's (sibling) render array key 'tabs' during post-render (or be
  359. * deleted if there was no such key by that point).
  360. *
  361. * @param string $token
  362. * The token string for the page callback, e.g. 'title'.
  363. *
  364. * @return string
  365. * The constructed token.
  366. *
  367. * @see ctools_set_callback_token()
  368. * @see ctools_page_token_processing()
  369. */
  370. function ctools_set_variable_token($token) {
  371. $string = '<!-- ctools-page-' . $token . ' -->';
  372. ctools_set_page_token($string, 'variable', $token);
  373. return $string;
  374. }
  375. /**
  376. * Set a replacement token from the value of a function during #post_render.
  377. *
  378. * This function can be used like this:
  379. * $token = ctools_set_callback_token('id', 'mymodule_myfunction');
  380. *
  381. * Or this (from its use in ctools_page_title_content_type_render):
  382. * $token = ctools_set_callback_token('title', array(
  383. * 'ctools_page_title_content_type_token', $conf['markup'], $conf['id'], $conf['class']
  384. * )
  385. * );
  386. *
  387. * The token (e.g: "<!-- ctools-page-id-1b7f84d1c8851290cc342631ac663053 -->")
  388. * would then be replaced during post-render by the return value of:
  389. *
  390. * ctools_page_title_content_type_token($value_markup, $value_id, $value_class);
  391. *
  392. * @param string $token
  393. * The token string for the page callback, e.g. 'title'.
  394. *
  395. * @param string|array $callback
  396. * For callback functions that require no args, the name of the function as a
  397. * string; otherwise an array of two or more elements: the function name
  398. * followed by one or more function arguments.
  399. *
  400. * NB: the value of $callback must be a procedural (non-class) function that
  401. * passes the php function_exists() check.
  402. *
  403. * The callback function itself will be called with args dependent
  404. * on $callback. If:
  405. * - $callback is a string, the function is called with a reference to the
  406. * render array;
  407. * - $callback is an array, the function is called with $callback merged
  408. * with an array containing a reference to the render array.
  409. *
  410. * @return string
  411. * The constructed token.
  412. *
  413. * @see ctools_set_variable_token()
  414. * @see ctools_page_token_processing()
  415. */
  416. function ctools_set_callback_token($token, $callback) {
  417. // If the callback uses arguments they are considered in the token.
  418. if (is_array($callback)) {
  419. $token .= '-' . md5(serialize($callback));
  420. }
  421. $string = '<!-- ctools-page-' . $token . ' -->';
  422. ctools_set_page_token($string, 'callback', $callback);
  423. return $string;
  424. }
  425. /**
  426. * Tell CTools that sidebar blocks should not be rendered.
  427. *
  428. * It is often desirable to not display sidebars when rendering a page,
  429. * particularly when using Panels. This informs CTools to alter out any
  430. * sidebar regions during block render.
  431. */
  432. function ctools_set_no_blocks($blocks = FALSE) {
  433. $status = &drupal_static(__FUNCTION__, TRUE);
  434. $status = $blocks;
  435. }
  436. /**
  437. * Wrapper function to create UUIDs via ctools, falls back on UUID module
  438. * if it is enabled. This code is a copy of uuid.inc from the uuid module.
  439. *
  440. * @see http://php.net/uniqid#65879
  441. */
  442. function ctools_uuid_generate() {
  443. if (!module_exists('uuid')) {
  444. ctools_include('uuid');
  445. $callback = drupal_static(__FUNCTION__);
  446. if (empty($callback)) {
  447. if (function_exists('uuid_create') && !function_exists('uuid_make')) {
  448. $callback = '_ctools_uuid_generate_pecl';
  449. }
  450. elseif (function_exists('com_create_guid')) {
  451. $callback = '_ctools_uuid_generate_com';
  452. }
  453. else {
  454. $callback = '_ctools_uuid_generate_php';
  455. }
  456. }
  457. return $callback();
  458. }
  459. else {
  460. return uuid_generate();
  461. }
  462. }
  463. /**
  464. * Check that a string appears to be in the format of a UUID.
  465. *
  466. * @see http://drupal.org/project/uuid
  467. *
  468. * @param $uuid
  469. * The string to test.
  470. *
  471. * @return
  472. * TRUE if the string is well formed.
  473. */
  474. function ctools_uuid_is_valid($uuid = '') {
  475. if (empty($uuid)) {
  476. return FALSE;
  477. }
  478. if (function_exists('uuid_is_valid') || module_exists('uuid')) {
  479. return uuid_is_valid($uuid);
  480. }
  481. else {
  482. ctools_include('uuid');
  483. return uuid_is_valid($uuid);
  484. }
  485. }
  486. /**
  487. * Add an array of classes to the body.
  488. *
  489. * @param mixed $classes
  490. * A string or an array of class strings to add.
  491. * @param string $hook
  492. * The theme hook to add the class to. The default is 'html' which will
  493. * affect the body tag.
  494. */
  495. function ctools_class_add($classes, $hook = 'html') {
  496. if (!is_array($classes)) {
  497. $classes = array($classes);
  498. }
  499. $static = &drupal_static('ctools_process_classes', array());
  500. if (!isset($static[$hook]['add'])) {
  501. $static[$hook]['add'] = array();
  502. }
  503. foreach ($classes as $class) {
  504. $static[$hook]['add'][] = $class;
  505. }
  506. }
  507. /**
  508. * Remove an array of classes from the body.
  509. *
  510. * @param mixed $classes
  511. * A string or an array of class strings to remove.
  512. * @param string $hook
  513. * The theme hook to remove the class from. The default is 'html' which will
  514. * affect the body tag.
  515. */
  516. function ctools_class_remove($classes, $hook = 'html') {
  517. if (!is_array($classes)) {
  518. // @todo Consider using explode(' ', $classes);
  519. // @todo Consider checking that $classes is a string before adding.
  520. $classes = array($classes);
  521. }
  522. $static = &drupal_static('ctools_process_classes', array());
  523. if (!isset($static[$hook]['remove'])) {
  524. $static[$hook]['remove'] = array();
  525. }
  526. foreach ($classes as $class) {
  527. $static[$hook]['remove'][] = $class;
  528. }
  529. }
  530. /**
  531. * Reset the storage used for ctools_class_add and ctools_class_remove.
  532. *
  533. * @see ctools_class_add()
  534. * @see ctools_class_remove()
  535. */
  536. function ctools_class_reset() {
  537. drupal_static_reset('ctools_process_classes');
  538. }
  539. /**
  540. * Return the classes for the body (added by ctools_class_add).
  541. *
  542. * @return array
  543. * A copy of the array of classes to add to the body tag. If none have been
  544. * added, this will be an empty array.
  545. *
  546. * @see ctools_class_add()
  547. */
  548. function ctools_get_classes() {
  549. return drupal_static('ctools_process_classes', array());
  550. }
  551. // -----------------------------------------------------------------------
  552. // Drupal core hooks.
  553. /**
  554. * Implement hook_init to keep our global CSS at the ready.
  555. */
  556. function ctools_init() {
  557. ctools_add_css('ctools');
  558. // If we are sure that CTools' AJAX is in use, change the error handling.
  559. if (!empty($_REQUEST['ctools_ajax'])) {
  560. ini_set('display_errors', 0);
  561. register_shutdown_function('ctools_shutdown_handler');
  562. }
  563. // Clear plugin cache on the module page submit.
  564. if ($_GET['q'] == 'admin/modules/list/confirm' && !empty($_POST)) {
  565. cache_clear_all('ctools_plugin_files:', 'cache', TRUE);
  566. }
  567. }
  568. /**
  569. * Shutdown handler used during ajax operations to help catch fatal errors.
  570. */
  571. function ctools_shutdown_handler() {
  572. if (function_exists('error_get_last') && ($error = error_get_last())) {
  573. switch ($error['type']) {
  574. case E_ERROR:
  575. case E_CORE_ERROR:
  576. case E_COMPILE_ERROR:
  577. case E_USER_ERROR:
  578. // Do this manually because including files here is dangerous.
  579. $commands = array(
  580. array(
  581. 'command' => 'alert',
  582. 'title' => t('Error'),
  583. 'text' => t('Unable to complete operation. Fatal error in @file on line @line: @message', array(
  584. '@file' => $error['file'],
  585. '@line' => $error['line'],
  586. '@message' => $error['message'],
  587. )),
  588. ),
  589. );
  590. // Change the status code so that the client will read the AJAX returned.
  591. header('HTTP/1.1 200 OK');
  592. drupal_json($commands);
  593. }
  594. }
  595. }
  596. /**
  597. * Implements hook_theme().
  598. */
  599. function ctools_theme() {
  600. ctools_include('utility');
  601. $items = array();
  602. ctools_passthrough('ctools', 'theme', $items);
  603. return $items;
  604. }
  605. /**
  606. * Implements hook_menu().
  607. */
  608. function ctools_menu() {
  609. ctools_include('utility');
  610. $items = array();
  611. ctools_passthrough('ctools', 'menu', $items);
  612. return $items;
  613. }
  614. /**
  615. * Implements hook_permission().
  616. */
  617. function ctools_permission() {
  618. return array(
  619. 'use ctools import' => array(
  620. 'title' => t('Use CTools importer'),
  621. 'description' => t('The import functionality allows users to execute arbitrary PHP code, so extreme caution must be taken.'),
  622. 'restrict access' => TRUE,
  623. ),
  624. );
  625. }
  626. /**
  627. * Implementation of hook_cron. Clean up old caches.
  628. */
  629. function ctools_cron() {
  630. ctools_include('utility');
  631. $items = array();
  632. ctools_passthrough('ctools', 'cron', $items);
  633. }
  634. /**
  635. * Implements hook_flush_caches().
  636. */
  637. function ctools_flush_caches() {
  638. // Only return the CSS cache bin if it has been activated, to avoid
  639. // drupal_flush_all_caches() from trying to truncate a non-existing table.
  640. return variable_get('cache_class_cache_ctools_css', FALSE) ? array('cache_ctools_css') : array();
  641. }
  642. /**
  643. * Implements hook_element_info_alter().
  644. */
  645. function ctools_element_info_alter(&$type) {
  646. ctools_include('dependent');
  647. ctools_dependent_element_info_alter($type);
  648. }
  649. /**
  650. * Implementation of hook_file_download()
  651. *
  652. * When using the private file system, we have to let Drupal know it's ok to
  653. * download CSS and image files from our temporary directory.
  654. */
  655. function ctools_file_download($filepath) {
  656. if (strpos($filepath, 'ctools') === 0) {
  657. $mime = file_get_mimetype($filepath);
  658. // For safety's sake, we allow only text and images.
  659. if (strpos($mime, 'text') === 0 || strpos($mime, 'image') === 0) {
  660. return array('Content-type:' . $mime);
  661. }
  662. }
  663. }
  664. /**
  665. * Implements hook_registry_files_alter().
  666. *
  667. * Alter the registry of files to automagically include all classes in
  668. * class-based plugins.
  669. */
  670. function ctools_registry_files_alter(&$files, $indexed_modules) {
  671. ctools_include('registry');
  672. return _ctools_registry_files_alter($files, $indexed_modules);
  673. }
  674. // -----------------------------------------------------------------------
  675. // FAPI hooks that must be in the .module file.
  676. /**
  677. * Alter the comment form to get a little more control over it.
  678. */
  679. function ctools_form_comment_form_alter(&$form, &$form_state) {
  680. if (!empty($form_state['ctools comment alter'])) {
  681. // Force the form to post back to wherever we are.
  682. $form['#action'] = url($_GET['q'], array('fragment' => 'comment-form'));
  683. if (empty($form['#submit'])) {
  684. $form['#submit'] = array('comment_form_submit');
  685. }
  686. $form['#submit'][] = 'ctools_node_comment_form_submit';
  687. }
  688. }
  689. function ctools_node_comment_form_submit(&$form, &$form_state) {
  690. $form_state['redirect'][0] = $_GET['q'];
  691. }
  692. // -----------------------------------------------------------------------
  693. // CTools hook implementations.
  694. /**
  695. * Implementation of hook_ctools_plugin_directory() to let the system know
  696. * where all our own plugins are.
  697. */
  698. function ctools_ctools_plugin_directory($owner, $plugin_type) {
  699. if ($owner == 'ctools') {
  700. return 'plugins/' . $plugin_type;
  701. }
  702. }
  703. /**
  704. * Implements hook_ctools_plugin_type().
  705. */
  706. function ctools_ctools_plugin_type() {
  707. ctools_include('utility');
  708. $items = array();
  709. // Add all the plugins that have their own declaration space elsewhere.
  710. ctools_passthrough('ctools', 'plugin-type', $items);
  711. return $items;
  712. }
  713. // -----------------------------------------------------------------------
  714. // Drupal theme preprocess hooks that must be in the .module file.
  715. /**
  716. * A theme preprocess function to automatically allow panels-based node
  717. * templates based upon input when the panel was configured.
  718. */
  719. function ctools_preprocess_node(&$vars) {
  720. // The 'ctools_template_identifier' attribute of the node is added when the pane is
  721. // rendered.
  722. if (!empty($vars['node']->ctools_template_identifier)) {
  723. $vars['ctools_template_identifier'] = check_plain($vars['node']->ctools_template_identifier);
  724. $vars['theme_hook_suggestions'][] = 'node__panel__' . check_plain($vars['node']->ctools_template_identifier);
  725. }
  726. }
  727. /**
  728. * Implements hook_page_alter().
  729. *
  730. * Last ditch attempt to remove sidebar regions if the "no blocks"
  731. * functionality has been activated.
  732. *
  733. * @see ctools_block_list_alter()
  734. */
  735. function ctools_page_alter(&$page) {
  736. $check = drupal_static('ctools_set_no_blocks', TRUE);
  737. if (!$check) {
  738. foreach ($page as $region_id => $region) {
  739. // @todo -- possibly we can set configuration for this so that users can
  740. // specify which blocks will not get rendered.
  741. if (strpos($region_id, 'sidebar') !== FALSE) {
  742. unset($page[$region_id]);
  743. }
  744. }
  745. }
  746. $page['#post_render'][] = 'ctools_page_token_processing';
  747. }
  748. /**
  749. * A theme post_render callback to allow content type plugins to use page
  750. * template variables which are not yet available when the content type is
  751. * rendered.
  752. */
  753. function ctools_page_token_processing($children, $elements) {
  754. $tokens = ctools_set_page_token();
  755. if (!empty($tokens)) {
  756. foreach ($tokens as $token => $key) {
  757. list($type, $argument) = $key;
  758. switch ($type) {
  759. case 'variable':
  760. $tokens[$token] = isset($elements[$argument]) ? $elements[$argument] : '';
  761. break;
  762. case 'callback':
  763. if (is_string($argument) && function_exists($argument)) {
  764. $tokens[$token] = $argument($elements);
  765. }
  766. if (is_array($argument) && function_exists($argument[0])) {
  767. $function = array_shift($argument);
  768. $argument = array_merge(array(&$elements), $argument);
  769. $tokens[$token] = call_user_func_array($function, $argument);
  770. }
  771. break;
  772. }
  773. }
  774. $children = strtr($children, $tokens);
  775. }
  776. return $children;
  777. }
  778. /**
  779. * Implements hook_process().
  780. *
  781. * Add and remove CSS classes from the variables array. We use process so that
  782. * we alter anything added in the preprocess hooks.
  783. */
  784. function ctools_process(&$variables, $hook) {
  785. if (!isset($variables['classes'])) {
  786. return;
  787. }
  788. $classes = ctools_get_classes();
  789. // Process the classses to add.
  790. if (!empty($classes[$hook]['add'])) {
  791. $add_classes = array_map('drupal_clean_css_identifier', $classes[$hook]['add']);
  792. $variables['classes_array'] = array_unique(array_merge($variables['classes_array'], $add_classes));
  793. }
  794. // Process the classes to remove.
  795. if (!empty($classes[$hook]['remove'])) {
  796. $remove_classes = array_map('drupal_clean_css_identifier', $classes[$hook]['remove']);
  797. $variables['classes_array'] = array_diff($variables['classes_array'], $remove_classes);
  798. }
  799. // Since this runs after template_process(), we need to re-implode the
  800. // classes array.
  801. $variables['classes'] = implode(' ', $variables['classes_array']);
  802. }
  803. // -----------------------------------------------------------------------
  804. // Menu callbacks that must be in the .module file.
  805. /**
  806. * Determine if the current user has access via a plugin.
  807. *
  808. * This function is meant to be embedded in the Drupal menu system, and
  809. * therefore is in the .module file since sub files can't be loaded, and
  810. * takes arguments a little bit more haphazardly than ctools_access().
  811. *
  812. * @param $access
  813. * An access control array which contains the following information:
  814. * - 'logic': and or or. Whether all tests must pass or one must pass.
  815. * - 'plugins': An array of access plugins. Each contains:
  816. * - - 'name': The name of the plugin
  817. * - - 'settings': The settings from the plugin UI.
  818. * - - 'context': Which context to use.
  819. * @param ...
  820. * zero or more context arguments generated from argument plugins. These
  821. * contexts must have an 'id' attached to them so that they can be
  822. * properly associated. The argument plugin system should set this, but
  823. * if the context is coming from elsewhere it will need to be set manually.
  824. *
  825. * @return
  826. * TRUE if access is granted, false if otherwise.
  827. */
  828. function ctools_access_menu($access) {
  829. // Short circuit everything if there are no access tests.
  830. if (empty($access['plugins'])) {
  831. return TRUE;
  832. }
  833. $contexts = array();
  834. foreach (func_get_args() as $arg) {
  835. if (is_object($arg) && get_class($arg) == 'ctools_context') {
  836. $contexts[$arg->id] = $arg;
  837. }
  838. }
  839. ctools_include('context');
  840. return ctools_access($access, $contexts);
  841. }
  842. /**
  843. * Determine if the current user has access via checks to multiple different
  844. * permissions.
  845. *
  846. * This function is a thin wrapper around user_access that allows multiple
  847. * permissions to be easily designated for use on, for example, a menu callback.
  848. *
  849. * @param ...
  850. * An indexed array of zero or more permission strings to be checked by
  851. * user_access().
  852. *
  853. * @return bool
  854. * Iff all checks pass will this function return TRUE. If an invalid argument
  855. * is passed (e.g., not a string), this function errs on the safe said and
  856. * returns FALSE.
  857. */
  858. function ctools_access_multiperm() {
  859. foreach (func_get_args() as $arg) {
  860. if (!is_string($arg) || !user_access($arg)) {
  861. return FALSE;
  862. }
  863. }
  864. return TRUE;
  865. }
  866. /**
  867. * Check to see if the incoming menu item is js capable or not.
  868. *
  869. * This can be used as %ctools_js as part of a path in hook menu. CTools
  870. * ajax functions will automatically change the phrase 'nojs' to 'ajax'
  871. * when it attaches ajax to a link. This can be used to autodetect if
  872. * that happened.
  873. */
  874. function ctools_js_load($js) {
  875. if ($js == 'ajax') {
  876. return TRUE;
  877. }
  878. return 0;
  879. }
  880. /**
  881. * Provides the default value for %ctools_js.
  882. *
  883. * This allows drupal_valid_path() to work with %ctools_js.
  884. */
  885. function ctools_js_to_arg($arg) {
  886. return empty($arg) || $arg == '%' ? 'nojs' : $arg;
  887. }
  888. /**
  889. * Menu _load hook.
  890. *
  891. * This function will be called to load an object as a replacement for
  892. * %ctools_export_ui in menu paths.
  893. */
  894. function ctools_export_ui_load($item_name, $plugin_name) {
  895. $return = &drupal_static(__FUNCTION__, FALSE);
  896. if (!$return) {
  897. ctools_include('export-ui');
  898. $plugin = ctools_get_export_ui($plugin_name);
  899. $handler = ctools_export_ui_get_handler($plugin);
  900. if ($handler) {
  901. return $handler->load_item($item_name);
  902. }
  903. }
  904. return $return;
  905. }
  906. // -----------------------------------------------------------------------
  907. // Caching callbacks on behalf of export-ui.
  908. /**
  909. * Menu access callback for various tasks of export-ui.
  910. */
  911. function ctools_export_ui_task_access($plugin_name, $op, $item = NULL) {
  912. ctools_include('export-ui');
  913. $plugin = ctools_get_export_ui($plugin_name);
  914. $handler = ctools_export_ui_get_handler($plugin);
  915. if ($handler) {
  916. return $handler->access($op, $item);
  917. }
  918. // Deny access if the handler cannot be found.
  919. return FALSE;
  920. }
  921. /**
  922. * Callback for access control ajax form on behalf of export ui.
  923. *
  924. * Returns the cached access config and contexts used.
  925. * Note that this is assuming that access will be in $item->access -- if it
  926. * is not, an export UI plugin will have to make its own callbacks.
  927. */
  928. function ctools_export_ui_ctools_access_get($argument) {
  929. ctools_include('export-ui');
  930. list($plugin_name, $key) = explode(':', $argument, 2);
  931. $plugin = ctools_get_export_ui($plugin_name);
  932. $handler = ctools_export_ui_get_handler($plugin);
  933. if ($handler) {
  934. ctools_include('context');
  935. $item = $handler->edit_cache_get($key);
  936. if (!$item) {
  937. $item = ctools_export_crud_load($handler->plugin['schema'], $key);
  938. }
  939. $contexts = ctools_context_load_contexts($item);
  940. return array($item->access, $contexts);
  941. }
  942. }
  943. /**
  944. * Callback for access control ajax form on behalf of export ui.
  945. *
  946. * Returns the cached access config and contexts used.
  947. * Note that this is assuming that access will be in $item->access -- if it
  948. * is not, an export UI plugin will have to make its own callbacks.
  949. */
  950. function ctools_export_ui_ctools_access_set($argument, $access) {
  951. ctools_include('export-ui');
  952. list($plugin_name, $key) = explode(':', $argument, 2);
  953. $plugin = ctools_get_export_ui($plugin_name);
  954. $handler = ctools_export_ui_get_handler($plugin);
  955. if ($handler) {
  956. ctools_include('context');
  957. $item = $handler->edit_cache_get($key);
  958. if (!$item) {
  959. $item = ctools_export_crud_load($handler->plugin['schema'], $key);
  960. }
  961. $item->access = $access;
  962. return $handler->edit_cache_set_key($item, $key);
  963. }
  964. }
  965. /**
  966. * Implements hook_menu_local_tasks_alter().
  967. */
  968. function ctools_menu_local_tasks_alter(&$data, $router_item, $root_path) {
  969. ctools_include('menu');
  970. _ctools_menu_add_dynamic_items($data, $router_item, $root_path);
  971. }
  972. /**
  973. * Implements hook_block_list_alter().
  974. *
  975. * Used to potentially remove blocks.
  976. * This exists in order to replicate Drupal 6's "no blocks" functionality.
  977. */
  978. function ctools_block_list_alter(&$blocks) {
  979. $check = drupal_static('ctools_set_no_blocks', TRUE);
  980. if (!$check) {
  981. foreach ($blocks as $block_id => $block) {
  982. // @todo -- possibly we can set configuration for this so that users can
  983. // specify which blocks will not get rendered.
  984. if (strpos($block->region, 'sidebar') !== FALSE) {
  985. unset($blocks[$block_id]);
  986. }
  987. }
  988. }
  989. }
  990. /**
  991. * Implements hook_modules_enabled().
  992. *
  993. * Clear caches for detecting new plugins.
  994. */
  995. function ctools_modules_enabled($modules) {
  996. ctools_include('plugins');
  997. ctools_get_plugins_reset();
  998. cache_clear_all('ctools_plugin_files:', 'cache', TRUE);
  999. }
  1000. /**
  1001. * Implements hook_modules_disabled().
  1002. *
  1003. * Clear caches for removing disabled plugins.
  1004. */
  1005. function ctools_modules_disabled($modules) {
  1006. ctools_include('plugins');
  1007. ctools_get_plugins_reset();
  1008. cache_clear_all('ctools_plugin_files:', 'cache', TRUE);
  1009. }
  1010. /**
  1011. * Menu theme callback.
  1012. *
  1013. * This simply ensures that Panels ajax calls are rendered in the same
  1014. * theme as the original page to prevent .css file confusion.
  1015. *
  1016. * To use this, set this as the theme callback on AJAX related menu
  1017. * items. Since the ajax page state won't be sent during ajax requests,
  1018. * it should be safe to use even if ajax isn't invoked.
  1019. */
  1020. function ctools_ajax_theme_callback() {
  1021. if (!empty($_POST['ajax_page_state']['theme'])) {
  1022. return $_POST['ajax_page_state']['theme'];
  1023. }
  1024. }
  1025. /**
  1026. * Implements hook_ctools_entity_context_alter().
  1027. */
  1028. function ctools_ctools_entity_context_alter(&$plugin, &$entity, $plugin_id) {
  1029. ctools_include('context');
  1030. switch ($plugin_id) {
  1031. case 'entity_id:taxonomy_term':
  1032. $plugin['no ui'] = TRUE;
  1033. break;
  1034. case 'entity:user':
  1035. $plugin = ctools_get_context('user');
  1036. unset($plugin['no ui']);
  1037. unset($plugin['no required context ui']);
  1038. break;
  1039. }
  1040. // Apply restrictions on taxonomy term reverse relationships whose
  1041. // restrictions are in the settings on the field.
  1042. if (!empty($plugin['parent']) &&
  1043. $plugin['parent'] == 'entity_from_field' &&
  1044. !empty($plugin['reverse']) &&
  1045. $plugin['to entity'] == 'taxonomy_term') {
  1046. $field = field_info_field($plugin['field name']);
  1047. if (isset($field['settings']['allowed_values'][0]['vocabulary'])) {
  1048. $plugin['required context']->restrictions = array('vocabulary' => array($field['settings']['allowed_values'][0]['vocabulary']));
  1049. }
  1050. }
  1051. }
  1052. /**
  1053. * Implements hook_field_create_field().
  1054. */
  1055. function ctools_field_create_field($field) {
  1056. ctools_flush_field_caches();
  1057. }
  1058. /**
  1059. * Implements hook_field_create_instance().
  1060. */
  1061. function ctools_field_create_instance($instance) {
  1062. ctools_flush_field_caches();
  1063. }
  1064. /**
  1065. * Implements hook_field_delete_field().
  1066. */
  1067. function ctools_field_delete_field($field) {
  1068. ctools_flush_field_caches();
  1069. }
  1070. /**
  1071. * Implements hook_field_delete_instance().
  1072. */
  1073. function ctools_field_delete_instance($instance) {
  1074. ctools_flush_field_caches();
  1075. }
  1076. /**
  1077. * Implements hook_field_update_field().
  1078. */
  1079. function ctools_field_update_field($field, $prior_field, $has_data) {
  1080. ctools_flush_field_caches();
  1081. }
  1082. /**
  1083. * Implements hook_field_update_instance().
  1084. */
  1085. function ctools_field_update_instance($instance, $prior_instance) {
  1086. ctools_flush_field_caches();
  1087. }
  1088. /**
  1089. * Clear field related caches.
  1090. */
  1091. function ctools_flush_field_caches() {
  1092. // Clear caches of 'Entity field' content type plugin.
  1093. cache_clear_all('ctools_entity_field_content_type_content_types', 'cache');
  1094. }