| 1 |
<?php |
| 2 |
|
| 3 |
// Optimization: maintain a single-query-duration cache of published story counts per group |
| 4 |
$_vojo_group_published_story_counts = array(); |
| 5 |
|
| 6 |
/** |
| 7 |
* Load up helpers |
| 8 |
*/ |
| 9 |
function vojo_init(){ |
| 10 |
// load custom blocks |
| 11 |
module_load_include('inc', 'vojo', 'includes/vojo.blocks'); |
| 12 |
// load costom voip-drupal scripts |
| 13 |
module_load_include('inc', 'vojo', 'includes/vojo.script'); |
| 14 |
if (arg(0) == 'blogs') { |
| 15 |
drupal_set_title('somebodys blog!'); |
| 16 |
} |
| 17 |
} |
| 18 |
|
| 19 |
/** |
| 20 |
* Implementation of hook_menu_alter. |
| 21 |
*/ |
| 22 |
function vojo_menu_alter(&$items) { |
| 23 |
// Unset default blog module's blog path (we want to use views instead). |
| 24 |
unset($items['blog/%user_uid_optional']); |
| 25 |
$items['blog/%user_uid_optional']['page callback'] = FALSE; |
| 26 |
} |
| 27 |
|
| 28 |
/** |
| 29 |
* Implementation of hook_link_alter. |
| 30 |
*/ |
| 31 |
function vojo_link_alter(&$links, $node) { |
| 32 |
// Change user's blog link to so it points to custom view (instead of core blog module's path) |
| 33 |
if ($links['blog_usernames_blog']) { |
| 34 |
$links['blog_usernames_blog']['href'] = 'blogs/'. $node->name; |
| 35 |
} |
| 36 |
} |
| 37 |
/** |
| 38 |
* Implementation of hook_profile_alter. |
| 39 |
*/ |
| 40 |
function vojo_profile_alter(&$account) { |
| 41 |
// Override path to user's blog, /blogs points to a view instead. |
| 42 |
$account->content['summary']['blog']['#value'] = l(t('View recent blog entries'), 'blogs/'. $account->name, array('attributes' => array('title' => 'Read latest blog entries.'))); |
| 43 |
} |
| 44 |
|
| 45 |
/** |
| 46 |
* Helper function to find out how many stories are published within a group. This is written |
| 47 |
* to be called by themes and custom panels/blocks if they need to embed the count somewhere. This |
| 48 |
* manages a qingle-query-duration cache for you. |
| 49 |
*/ |
| 50 |
function vojo_group_published_story_count($group_id) { |
| 51 |
global $_vojo_group_published_story_counts; |
| 52 |
$story_count = 0; |
| 53 |
if(array_key_exists($group_id,$_vojo_group_published_story_counts)){ |
| 54 |
$story_count = $_vojo_group_published_story_counts[$group_id]; |
| 55 |
} else { |
| 56 |
$results = db_query("SELECT count(n.nid) as story_count |
| 57 |
FROM {og_ancestry} as oga, {node} as n |
| 58 |
WHERE oga.nid=n.nid AND oga.group_nid=$group_id AND n.status=1", $group_id); |
| 59 |
while ($object = db_fetch_object($results)) { |
| 60 |
$story_count = $object->story_count; |
| 61 |
} |
| 62 |
$_vojo_group_published_story_counts[$group_id] = $story_count; |
| 63 |
} |
| 64 |
return $story_count; |
| 65 |
} |
| 66 |
|
| 67 |
/** |
| 68 |
* Use this to get the path to an image inside the "img" dir of this module |
| 69 |
*/ |
| 70 |
function _vojo_image_path($imgfile){ |
| 71 |
return theme_image(drupal_get_path('module','vojo')."/img/".$imgfile); |
| 72 |
} |
| 73 |
|
| 74 |
/** |
| 75 |
* Implementation of hook_voipscript_load_script(). |
| 76 |
*/ |
| 77 |
function vojo_voipscript_load_script($script_name, $params = NULL) { |
| 78 |
if ($script_name == 'vojo_group') { |
| 79 |
module_load_include('inc', 'vojo', 'vojo.script'); |
| 80 |
$node = node_load($params['nid']); |
| 81 |
return _vojo_group_script($node); |
| 82 |
} |
| 83 |
elseif ($script_name == 'vojo_record_blog') { |
| 84 |
module_load_include('inc', 'vojo', 'vojo.script'); |
| 85 |
$node = node_load($params['nid']); |
| 86 |
return _vojo_record_blog_script($node); |
| 87 |
} |
| 88 |
elseif ($script_name == 'vojo_save_blog') { |
| 89 |
module_load_include('inc', 'vojo', 'vojo.script'); |
| 90 |
return _vojo_save_blog_script($params['nid'], $params['fid']); |
| 91 |
} |
| 92 |
elseif ($script_name == 'vojo_login') { |
| 93 |
module_load_include('inc', 'vojo', 'vojo.script'); |
| 94 |
return _vojo_login_script($params['number']); |
| 95 |
} |
| 96 |
elseif ($script_name == 'vojo_welcome') { |
| 97 |
module_load_include('inc', 'vojo', 'vojo.script'); |
| 98 |
return _vojo_welcome_script($params['number']); |
| 99 |
} |
| 100 |
elseif ($script_name == 'vojo_redirect_to_group') { |
| 101 |
module_load_include('inc', 'vojo', 'vojo.script'); |
| 102 |
return _vojo_redirect_to_group_script($params['number']); |
| 103 |
} |
| 104 |
} |
| 105 |
|
| 106 |
/** |
| 107 |
* Implementation of hook_voipscript_get_script_names(). |
| 108 |
*/ |
| 109 |
function vojo_voipscript_get_script_names() { |
| 110 |
return array( |
| 111 |
'vojo_group', |
| 112 |
'vojo_record_blog', |
| 113 |
'vojo_save_blog', |
| 114 |
'vojo_login', |
| 115 |
'vojo_welcome', |
| 116 |
'vojo_redirect_to_group', |
| 117 |
); |
| 118 |
} |
| 119 |
|
| 120 |
/** |
| 121 |
* Implementation of hook_ctools_plugin_dierctory() to let the system know |
| 122 |
* we implement task and task_handler plugins. |
| 123 |
*/ |
| 124 |
function vojo_ctools_plugin_directory($module, $plugin) { |
| 125 |
return 'plugins/' . $plugin; |
| 126 |
} |
| 127 |
|
| 128 |
/** --------- OPENLAYERS ------------------------------------------------------ **/ |
| 129 |
|
| 130 |
/** |
| 131 |
* Define custom styles for rendering the markers on a map. |
| 132 |
* Implements hook_openlayers_styles(). |
| 133 |
* http://drupal.org/node/620602 |
| 134 |
*/ |
| 135 |
function vojo_openlayers_styles() { |
| 136 |
$styles = array(); |
| 137 |
|
| 138 |
$style = new stdClass(); |
| 139 |
$style->api_version = 1; |
| 140 |
$style->name = 'vojo_default'; |
| 141 |
$style->title = t('Vojo Default style'); |
| 142 |
$style->description = t('Basic default style for the vojo website.'); |
| 143 |
$style->data = array( |
| 144 |
'pointRadius' => 6, |
| 145 |
'fillColor' => '#ffffff', |
| 146 |
'strokeColor' => '#890053', |
| 147 |
'strokeWidth' => 4, |
| 148 |
'fillOpacity' => 0.9, |
| 149 |
'strokeOpacity' => 1 |
| 150 |
); |
| 151 |
$styles[ $style->name ] = $style; |
| 152 |
|
| 153 |
$style = new stdClass(); |
| 154 |
$style->api_version = 1; |
| 155 |
$style->name = 'vojo_selected'; |
| 156 |
$style->title = t('Vojo Selected style'); |
| 157 |
$style->description = t('Basic selected style for the vojo website.'); |
| 158 |
$style->data = array( |
| 159 |
'pointRadius' => 10, |
| 160 |
'fillColor' => '#ffffff', |
| 161 |
'strokeColor' => '#890053', |
| 162 |
'strokeWidth' => 8, |
| 163 |
'fillOpacity' => 1.0, |
| 164 |
'strokeOpacity' => 1.0 |
| 165 |
); |
| 166 |
$styles[ $style->name ] = $style; |
| 167 |
|
| 168 |
$style = new stdClass(); |
| 169 |
$style->api_version = 1; |
| 170 |
$style->name = 'vojo_temporary'; |
| 171 |
$style->title = t('Vojo Temporary style'); |
| 172 |
$style->description = t('Basic temporary style for the vojo website.'); |
| 173 |
$style->data = array( |
| 174 |
'pointRadius' => 8, |
| 175 |
'fillColor' => '#ffffff', |
| 176 |
'strokeColor' => '#890053', |
| 177 |
'strokeWidth' => 6, |
| 178 |
'fillOpacity' => 0.5, |
| 179 |
'strokeOpacity' => 0.5 |
| 180 |
); |
| 181 |
$styles[ $style->name ] = $style; |
| 182 |
|
| 183 |
return $styles; |
| 184 |
} |
| 185 |
|
| 186 |
/** |
| 187 |
* Tell ctools that we have custom marker styles. |
| 188 |
* Implements hook_ctools_plugin_api(). |
| 189 |
* http://drupal.org/node/620602 |
| 190 |
*/ |
| 191 |
function vojo_ctools_plugin_api($module, $api) { |
| 192 |
if ($module == "openlayers") { |
| 193 |
switch ($api) { |
| 194 |
case 'openlayers_styles': |
| 195 |
return array('version' => 1); |
| 196 |
|
| 197 |
} |
| 198 |
} |
| 199 |
} |
| 200 |
|
| 201 |
/** |
| 202 |
* Get the url to an audio file stored with this model. |
| 203 |
*/ |
| 204 |
function _vojo_get_audio_url($filename){ |
| 205 |
global $base_url; |
| 206 |
return $base_url."/".drupal_get_path('module','vojo')."/audio/".$filename; |
| 207 |
} |
| 208 |
|
| 209 |
/** |
| 210 |
* Increase max length of location field on blog for https://dev.vozmob.net/issues/1072 |
| 211 |
*/ |
| 212 |
function vojo_form_blog_node_form_alter($form, &$form_state) { |
| 213 |
// Hide location field to prevent users from editing it directly. |
| 214 |
$form['field_location_string']['#theme'] = FALSE; |
| 215 |
|
| 216 |
$form['#after_build'][] = 'vojo_form_blog_node_form_after_build'; |
| 217 |
|
| 218 |
// Well need to set value of $form['field_location_string'] in a submit handler. |
| 219 |
$form['#submit'][] = 'vojo_form_blog_node_form_submit'; |
| 220 |
} |
| 221 |
function vojo_form_blog_node_form_after_build($form, &$form_state) { |
| 222 |
$form['locations'][0]['name']['#maxlength'] = "128"; |
| 223 |
return $form; |
| 224 |
} |
| 225 |
function vojo_form_blog_node_form_submit($form, &$form_state) { |
| 226 |
// Grab user entered value from openlayers_geocoder field and save to $form['field_location_string'] |
| 227 |
$form_state['values']['field_location_string'][0]['value'] = $form['field_map']['openlayers_geocoder_query']['#value']; |
| 228 |
} |
| 229 |
/** |
| 230 |
* Find the node matching the specified phone number, checking in all voipnumber fields. |
| 231 |
* Returns false if no match is found. If multiple matches are found, this return the first |
| 232 |
* one it finds. |
| 233 |
*/ |
| 234 |
function _vojo_voipnumber_api_nid_from_number($number) { |
| 235 |
// look in all the voipnumber fields |
| 236 |
foreach( content_fields() as $field){ |
| 237 |
if($field['type']=='voipnumber'){ |
| 238 |
// determine what table to search in |
| 239 |
$table_name= "content_type_".$field['type_name']; |
| 240 |
$col_name = $field['field_name']."_phone"; |
| 241 |
// search for any matches to the phone number |
| 242 |
$sql = 'SELECT nid FROM {'.$table_name.'} WHERE '.$col_name.' = "%s"'; |
| 243 |
$result = db_query($sql, $number); |
| 244 |
while ($noderow = db_fetch_object($result)) { |
| 245 |
return $noderow->nid; |
| 246 |
} |
| 247 |
} |
| 248 |
} |
| 249 |
return false; |
| 250 |
} |
| 251 |
|
| 252 |
|
| 253 |
/** |
| 254 |
* Find the extension number for a specific node id. |
| 255 |
*/ |
| 256 |
function _vojo_voipextension_api_extension_for_node($nid) { |
| 257 |
return db_result(db_query('SELECT eid FROM {voipextension} WHERE module_id = %d', $nid)); |
| 258 |
} |
| 259 |
|
| 260 |
/** |
| 261 |
* Helper function to format phone numbers. |
| 262 |
*/ |
| 263 |
function format_phone_number($number) { |
| 264 |
// If 12 digits format as Mexican number: +xx xx xxx-xxxxx. |
| 265 |
if (is_numeric($number)) { |
| 266 |
if (strlen($number) == 19) { |
| 267 |
$formatted_number = '+'. substr($number, 0, 2) .' '. substr($number, 2, 2) .' '. substr($number, 4, 3) .'-'. substr($number, 7); |
| 268 |
} |
| 269 |
// If 10 digits format as US number: (xxx) xxx-xxxx. |
| 270 |
else if (strlen($number) == 10) { |
| 271 |
$formatted_number = '('. substr($number, 0, 3) .') '. substr($number, 3, 3) .'-'. substr($number, 6); |
| 272 |
} |
| 273 |
} |
| 274 |
else { |
| 275 |
$formatted_number = $number; |
| 276 |
} |
| 277 |
return $formatted_number; |
| 278 |
} |