PageRenderTime 52ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/mailchimp.civix.php

https://github.com/rajeshrhino/uk.co.vedaconsulting.mailchimp
PHP | 278 lines | 156 code | 22 blank | 100 comment | 32 complexity | 99190772271e2da6f9dae3ebcc83445e MD5 | raw file
Possible License(s): AGPL-3.0
  1. <?php
  2. // AUTO-GENERATED FILE -- Civix may overwrite any changes made to this file
  3. /**
  4. * (Delegated) Implementation of hook_civicrm_config
  5. *
  6. * @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_config
  7. */
  8. function _mailchimp_civix_civicrm_config(&$config = NULL) {
  9. static $configured = FALSE;
  10. if ($configured) return;
  11. $configured = TRUE;
  12. $template =& CRM_Core_Smarty::singleton();
  13. $extRoot = dirname( __FILE__ ) . DIRECTORY_SEPARATOR;
  14. $extDir = $extRoot . 'templates';
  15. if ( is_array( $template->template_dir ) ) {
  16. array_unshift( $template->template_dir, $extDir );
  17. } else {
  18. $template->template_dir = array( $extDir, $template->template_dir );
  19. }
  20. $include_path = $extRoot . PATH_SEPARATOR . get_include_path( );
  21. set_include_path( $include_path );
  22. }
  23. /**
  24. * (Delegated) Implementation of hook_civicrm_xmlMenu
  25. *
  26. * @param $files array(string)
  27. * @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_xmlMenu
  28. */
  29. function _mailchimp_civix_civicrm_xmlMenu(&$files) {
  30. foreach (_mailchimp_civix_glob(__DIR__ . '/xml/Menu/*.xml') as $file) {
  31. $files[] = $file;
  32. }
  33. }
  34. /**
  35. * Implementation of hook_civicrm_install
  36. *
  37. * @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_install
  38. */
  39. function _mailchimp_civix_civicrm_install() {
  40. _mailchimp_civix_civicrm_config();
  41. if ($upgrader = _mailchimp_civix_upgrader()) {
  42. return $upgrader->onInstall();
  43. }
  44. }
  45. /**
  46. * Implementation of hook_civicrm_uninstall
  47. *
  48. * @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_uninstall
  49. */
  50. function _mailchimp_civix_civicrm_uninstall() {
  51. _mailchimp_civix_civicrm_config();
  52. if ($upgrader = _mailchimp_civix_upgrader()) {
  53. return $upgrader->onUninstall();
  54. }
  55. }
  56. /**
  57. * (Delegated) Implementation of hook_civicrm_enable
  58. *
  59. * @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_enable
  60. */
  61. function _mailchimp_civix_civicrm_enable() {
  62. _mailchimp_civix_civicrm_config();
  63. if ($upgrader = _mailchimp_civix_upgrader()) {
  64. if (is_callable(array($upgrader, 'onEnable'))) {
  65. return $upgrader->onEnable();
  66. }
  67. }
  68. }
  69. /**
  70. * (Delegated) Implementation of hook_civicrm_disable
  71. *
  72. * @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_disable
  73. */
  74. function _mailchimp_civix_civicrm_disable() {
  75. _mailchimp_civix_civicrm_config();
  76. if ($upgrader = _mailchimp_civix_upgrader()) {
  77. if (is_callable(array($upgrader, 'onDisable'))) {
  78. return $upgrader->onDisable();
  79. }
  80. }
  81. }
  82. /**
  83. * (Delegated) Implementation of hook_civicrm_upgrade
  84. *
  85. * @param $op string, the type of operation being performed; 'check' or 'enqueue'
  86. * @param $queue CRM_Queue_Queue, (for 'enqueue') the modifiable list of pending up upgrade tasks
  87. *
  88. * @return mixed based on op. for 'check', returns array(boolean) (TRUE if upgrades are pending)
  89. * for 'enqueue', returns void
  90. *
  91. * @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_upgrade
  92. */
  93. function _mailchimp_civix_civicrm_upgrade($op, CRM_Queue_Queue $queue = NULL) {
  94. if ($upgrader = _mailchimp_civix_upgrader()) {
  95. return $upgrader->onUpgrade($op, $queue);
  96. }
  97. }
  98. /**
  99. * @return CRM_Mailchimp_Upgrader
  100. */
  101. function _mailchimp_civix_upgrader() {
  102. if (!file_exists(__DIR__.'/CRM/Mailchimp/Upgrader.php')) {
  103. return NULL;
  104. } else {
  105. return CRM_Mailchimp_Upgrader_Base::instance();
  106. }
  107. }
  108. /**
  109. * Search directory tree for files which match a glob pattern
  110. *
  111. * Note: Dot-directories (like "..", ".git", or ".svn") will be ignored.
  112. * Note: In Civi 4.3+, delegate to CRM_Utils_File::findFiles()
  113. *
  114. * @param $dir string, base dir
  115. * @param $pattern string, glob pattern, eg "*.txt"
  116. * @return array(string)
  117. */
  118. function _mailchimp_civix_find_files($dir, $pattern) {
  119. if (is_callable(array('CRM_Utils_File', 'findFiles'))) {
  120. return CRM_Utils_File::findFiles($dir, $pattern);
  121. }
  122. $todos = array($dir);
  123. $result = array();
  124. while (!empty($todos)) {
  125. $subdir = array_shift($todos);
  126. foreach (_mailchimp_civix_glob("$subdir/$pattern") as $match) {
  127. if (!is_dir($match)) {
  128. $result[] = $match;
  129. }
  130. }
  131. if ($dh = opendir($subdir)) {
  132. while (FALSE !== ($entry = readdir($dh))) {
  133. $path = $subdir . DIRECTORY_SEPARATOR . $entry;
  134. if ($entry{0} == '.') {
  135. } elseif (is_dir($path)) {
  136. $todos[] = $path;
  137. }
  138. }
  139. closedir($dh);
  140. }
  141. }
  142. return $result;
  143. }
  144. /**
  145. * (Delegated) Implementation of hook_civicrm_managed
  146. *
  147. * Find any *.mgd.php files, merge their content, and return.
  148. *
  149. * @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_managed
  150. */
  151. function _mailchimp_civix_civicrm_managed(&$entities) {
  152. $mgdFiles = _mailchimp_civix_find_files(__DIR__, '*.mgd.php');
  153. foreach ($mgdFiles as $file) {
  154. $es = include $file;
  155. foreach ($es as $e) {
  156. if (empty($e['module'])) {
  157. $e['module'] = 'uk.co.vedaconsulting.mailchimp';
  158. }
  159. $entities[] = $e;
  160. }
  161. }
  162. }
  163. /**
  164. * (Delegated) Implementation of hook_civicrm_caseTypes
  165. *
  166. * Find any and return any files matching "xml/case/*.xml"
  167. *
  168. * Note: This hook only runs in CiviCRM 4.4+.
  169. *
  170. * @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_caseTypes
  171. */
  172. function _mailchimp_civix_civicrm_caseTypes(&$caseTypes) {
  173. if (!is_dir(__DIR__ . '/xml/case')) {
  174. return;
  175. }
  176. foreach (_mailchimp_civix_glob(__DIR__ . '/xml/case/*.xml') as $file) {
  177. $name = preg_replace('/\.xml$/', '', basename($file));
  178. if ($name != CRM_Case_XMLProcessor::mungeCaseType($name)) {
  179. $errorMessage = sprintf("Case-type file name is malformed (%s vs %s)", $name, CRM_Case_XMLProcessor::mungeCaseType($name));
  180. CRM_Core_Error::fatal($errorMessage);
  181. // throw new CRM_Core_Exception($errorMessage);
  182. }
  183. $caseTypes[$name] = array(
  184. 'module' => 'uk.co.vedaconsulting.mailchimp',
  185. 'name' => $name,
  186. 'file' => $file,
  187. );
  188. }
  189. }
  190. /**
  191. * Glob wrapper which is guaranteed to return an array.
  192. *
  193. * The documentation for glob() says, "On some systems it is impossible to
  194. * distinguish between empty match and an error." Anecdotally, the return
  195. * result for an empty match is sometimes array() and sometimes FALSE.
  196. * This wrapper provides consistency.
  197. *
  198. * @link http://php.net/glob
  199. * @param string $pattern
  200. * @return array, possibly empty
  201. */
  202. function _mailchimp_civix_glob($pattern) {
  203. $result = glob($pattern);
  204. return is_array($result) ? $result : array();
  205. }
  206. /**
  207. * Inserts a navigation menu item at a given place in the hierarchy
  208. *
  209. * $menu - menu hierarchy
  210. * $path - path where insertion should happen (ie. Administer/System Settings)
  211. * $item - menu you need to insert (parent/child attributes will be filled for you)
  212. * $parentId - used internally to recurse in the menu structure
  213. */
  214. function _mailchimp_civix_insert_navigation_menu(&$menu, $path, $item, $parentId = NULL) {
  215. static $navId;
  216. // If we are done going down the path, insert menu
  217. if (empty($path)) {
  218. if (!$navId) $navId = CRM_Core_DAO::singleValueQuery("SELECT max(id) FROM civicrm_navigation");
  219. $navId ++;
  220. $menu[$navId] = array (
  221. 'attributes' => array_merge($item, array(
  222. 'label' => CRM_Utils_Array::value('name', $item),
  223. 'active' => 1,
  224. 'parentID' => $parentId,
  225. 'navID' => $navId,
  226. ))
  227. );
  228. return true;
  229. } else {
  230. // Find an recurse into the next level down
  231. $found = false;
  232. $path = explode('/', $path);
  233. $first = array_shift($path);
  234. foreach ($menu as $key => &$entry) {
  235. if ($entry['attributes']['name'] == $first) {
  236. if (!$entry['child']) $entry['child'] = array();
  237. $found = _mailchimp_civix_insert_navigation_menu($entry['child'], implode('/', $path), $item, $key);
  238. }
  239. }
  240. return $found;
  241. }
  242. }
  243. /**
  244. * (Delegated) Implementation of hook_civicrm_alterSettingsFolders
  245. *
  246. * @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_alterSettingsFolders
  247. */
  248. function _mailchimp_civix_civicrm_alterSettingsFolders(&$metaDataFolders = NULL) {
  249. static $configured = FALSE;
  250. if ($configured) return;
  251. $configured = TRUE;
  252. $settingsDir = __DIR__ . DIRECTORY_SEPARATOR . 'settings';
  253. if(is_dir($settingsDir) && !in_array($settingsDir, $metaDataFolders)) {
  254. $metaDataFolders[] = $settingsDir;
  255. }
  256. }