ctools.module 36 KB

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