-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #196 from compucorp/staging
Sync master with staging
- Loading branch information
Showing
24 changed files
with
3,521 additions
and
1,036 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
|
||
The module provides an api function to easily load civicrm resources from an extention into drupal. It invokes a custom drupal cache bin to store path information of extensions, so as not to bootstrap civicrm on every api call. | ||
|
||
This module is sponsored by www.compucorp.co.uk | ||
|
||
Installation | ||
------------ | ||
|
||
Place the Civi civicrm_resources in the modules directory of your site and | ||
enable it on the `admin/modules` page. | ||
|
||
|
||
Usage | ||
----- | ||
|
||
The module invokes a custom drupal cache bin to store path information of extensions, so as not to call civicrm_initialize() on every api call. | ||
To load a js/css from "Job Contract (CiviHR)", call the api function as in following examples. | ||
|
||
- civicrm_resources_load('org.civicrm.hrjobcontract', array('gulpfile.js', 'hrjc.css', 'contact.js')); | ||
This will load all css/js files listed in array located anywhere in extension "Job Contract (CiviHR)". | ||
|
||
- civicrm_resources_load('org.civicrm.hrjobcontract', array('gulpfile.js', 'contact.js', '*.css')); | ||
This will load all css files and 2 js files listed in array located anywhere in extension "Job Contract (CiviHR)". | ||
|
||
- civicrm_resources_load('org.civicrm.hrjobcontract'); | ||
This will load all css files and all js files located anywhere in extension "Job Contract (CiviHR)". |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
name = CiviCRM Resources | ||
description = Loading Civicrm js and css files in drupal. | ||
package = "CiviCRM" | ||
core = "7.x" | ||
dependencies[] = civicrm | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?php | ||
|
||
/** | ||
* Implementation of hook_schema(). | ||
*/ | ||
function civicrm_resources_schema() { | ||
$schema = array(); | ||
$schema['cache_civicrm_resources'] = drupal_get_schema_unprocessed('system', 'cache'); | ||
return $schema; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,168 @@ | ||
<?php | ||
|
||
/** | ||
* @file | ||
* Module to load css and js resources from a directory | ||
*/ | ||
|
||
/** | ||
* Internal function to handle the logic of files to be filtered and included. | ||
* | ||
* @param string $extention_path | ||
* Machine path of civicrm extention. | ||
* @param array $req_files | ||
* An array of file list to be included | ||
* | ||
* @return NULL | ||
*/ | ||
function civicrm_resources_add_resources($extention_path, $req_files = []) { | ||
global $base_url; | ||
|
||
$files = civicrm_resources_file_scan_directory($extention_path, '/\.(css|js)$/i'); | ||
|
||
$js_list = []; | ||
$css_list = []; | ||
|
||
foreach ($files as $key => $file) { | ||
if (!empty($req_files)) { | ||
if (in_array('*.js', $req_files) && preg_match('/\.js$/', $file->filename)) { | ||
$js_list[] = $file->uri; | ||
} | ||
else { | ||
if (in_array($file->filename, $req_files) && preg_match('/\.js$/', $file->filename)) { | ||
$js_list[] = $file->uri; | ||
} | ||
} | ||
if (in_array('*.css', $req_files) && preg_match('/\.css$/', $file->filename)) { | ||
$css_list[] = $file->uri; | ||
} | ||
else { | ||
if (in_array($file->filename, $req_files) && preg_match('/\.css$/', $file->filename)) { | ||
$css_list[] = $file->uri; | ||
} | ||
} | ||
} | ||
else { | ||
if (preg_match('/\.css$/', $file->filename)) { | ||
$css_list[] = $file->uri; | ||
} | ||
elseif (preg_match('/\.js$/', $file->filename)) { | ||
$js_list[] = $file->uri; | ||
} | ||
} | ||
} | ||
foreach ($css_list as $key => $css_file) { | ||
drupal_add_css(ltrim(str_replace(DRUPAL_ROOT, '', $css_file), '/')); | ||
} | ||
foreach ($js_list as $key => $js_file) { | ||
drupal_add_js(ltrim(str_replace(DRUPAL_ROOT, '', $js_file), '/')); | ||
} | ||
} | ||
|
||
/** | ||
* Finds all files that match a given mask in a given directory. | ||
* | ||
* Directories and files beginning with a period are excluded; this | ||
* prevents hidden files and directories (such as SVN working directories) | ||
* from being scanned. | ||
* | ||
* @param $dir | ||
* The base directory or URI to scan, without trailing slash. | ||
* @param $mask | ||
* The preg_match() regular expression of the files to find. | ||
* @param $options | ||
* An associative array of additional options, with the following elements: | ||
* - 'nomask': The preg_match() regular expression of the files to ignore. | ||
* Defaults to '/(\.\.?|CVS)$/'. | ||
* - 'callback': The callback function to call for each match. There is no | ||
* default callback. | ||
* - 'recurse': When TRUE, the directory scan will recurse the entire tree | ||
* starting at the provided directory. Defaults to TRUE. | ||
* - 'key': The key to be used for the returned associative array of files. | ||
* Possible values are 'uri', for the file's URI; 'filename', for the | ||
* basename of the file; and 'name' for the name of the file without the | ||
* extension. Defaults to 'uri'. | ||
* - 'min_depth': Minimum depth of directories to return files from. Defaults | ||
* to 0. | ||
* @param $depth | ||
* Current depth of recursion. This parameter is only used internally and | ||
* should not be passed in. | ||
* | ||
* @return | ||
* An associative array (keyed on the chosen key) of objects with 'uri', | ||
* 'filename', and 'name' members corresponding to the matching files. | ||
*/ | ||
function civicrm_resources_file_scan_directory($dir, $mask, $options = [], $depth = 0) { | ||
// Merge in defaults. | ||
$options += [ | ||
'nomask' => '/(\.\.?|CVS)$/', | ||
'callback' => 0, | ||
'recurse' => TRUE, | ||
'key' => 'uri', | ||
'min_depth' => 0, | ||
]; | ||
|
||
$options['key'] = in_array($options['key'], [ | ||
'uri', | ||
'filename', | ||
'name', | ||
]) ? $options['key'] : 'uri'; | ||
$files = []; | ||
if (is_dir($dir) && $handle = opendir($dir)) { | ||
while (FALSE !== ($filename = readdir($handle))) { | ||
if (!preg_match($options['nomask'], $filename) && $filename[0] != '.') { | ||
$uri = "$dir/$filename"; | ||
$uri = file_stream_wrapper_uri_normalize($uri); | ||
if (is_dir($uri) && $options['recurse']) { | ||
$files = array_merge(civicrm_resources_file_scan_directory($uri, $mask, $options, $depth + 1), $files); | ||
} | ||
elseif ($depth >= $options['min_depth'] && preg_match($mask, $filename)) { | ||
$file = new stdClass(); | ||
$file->uri = $uri; | ||
$file->filename = $filename; | ||
$file->name = pathinfo($filename, PATHINFO_FILENAME); | ||
$key = $options['key']; | ||
$files[$file->$key] = $file; | ||
if ($options['callback']) { | ||
$options['callback']($uri); | ||
} | ||
} | ||
} | ||
} | ||
closedir($handle); | ||
} | ||
return $files; | ||
} | ||
|
||
/** | ||
* API function to be used across drupal site to load resources. | ||
* | ||
* @param string $extention_full_name | ||
* Machine name of civicrm extention. | ||
* @param array $files | ||
* An array of file list to be included | ||
* | ||
* @return NULL | ||
*/ | ||
function civicrm_resources_load($extention_full_name, $files = []) { | ||
if (cache_get($extention_full_name, 'cache_civicrm_resources')) { | ||
$ext_path_cache = cache_get($extention_full_name, 'cache_civicrm_resources'); | ||
$ext_path = $ext_path_cache->data; | ||
} | ||
else { | ||
civicrm_initialize(); | ||
$ext_path = CRM_Extension_System::singleton() | ||
->getMapper() | ||
->keyToBasePath($extention_full_name); | ||
cache_set($extention_full_name, $ext_path, 'cache_civicrm_resources'); | ||
} | ||
//civicrm_resources_add_resources($ext_path, array('gulpfile.js', 'hrjc.css', 'contact.js')); | ||
civicrm_resources_add_resources($ext_path, $files); | ||
} | ||
|
||
/** | ||
* Implements hook_flush_caches(). | ||
*/ | ||
function civicrm_resources_flush_caches() { | ||
return ['cache_civicrm_resources']; | ||
} |
Oops, something went wrong.