| 1 | <?php |
|---|
| 2 | |
|---|
| 3 | // |
|---|
| 4 | // Open Web Analytics - An Open Source Web Analytics Framework |
|---|
| 5 | // |
|---|
| 6 | // Copyright 2006 Peter Adams. All rights reserved. |
|---|
| 7 | // |
|---|
| 8 | // Licensed under GPL v2.0 http://www.gnu.org/copyleft/gpl.html |
|---|
| 9 | // |
|---|
| 10 | // Unless required by applicable law or agreed to in writing, software |
|---|
| 11 | // distributed under the License is distributed on an "AS IS" BASIS, |
|---|
| 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|---|
| 13 | // See the License for the specific language governing permissions and |
|---|
| 14 | // limitations under the License. |
|---|
| 15 | // |
|---|
| 16 | // $Id$ |
|---|
| 17 | // |
|---|
| 18 | |
|---|
| 19 | /** |
|---|
| 20 | * Abstract Module Class |
|---|
| 21 | * |
|---|
| 22 | * @author Peter Adams <peter@openwebanalytics.com> |
|---|
| 23 | * @copyright Copyright © 2006 Peter Adams <peter@openwebanalytics.com> |
|---|
| 24 | * @license http://www.gnu.org/copyleft/gpl.html GPL v2.0 |
|---|
| 25 | * @category owa |
|---|
| 26 | * @package owa |
|---|
| 27 | * @version $Revision$ |
|---|
| 28 | * @since owa 1.0.0 |
|---|
| 29 | */ |
|---|
| 30 | |
|---|
| 31 | class owa_module extends owa_base { |
|---|
| 32 | |
|---|
| 33 | /** |
|---|
| 34 | * Name of module |
|---|
| 35 | * |
|---|
| 36 | * @var string |
|---|
| 37 | */ |
|---|
| 38 | var $name; |
|---|
| 39 | |
|---|
| 40 | /** |
|---|
| 41 | * Description of Module |
|---|
| 42 | * |
|---|
| 43 | * @var string |
|---|
| 44 | */ |
|---|
| 45 | var $description; |
|---|
| 46 | |
|---|
| 47 | /** |
|---|
| 48 | * Version of Module |
|---|
| 49 | * |
|---|
| 50 | * @var string |
|---|
| 51 | */ |
|---|
| 52 | var $version; |
|---|
| 53 | |
|---|
| 54 | /** |
|---|
| 55 | * Schema Version of Module |
|---|
| 56 | * |
|---|
| 57 | * @var string |
|---|
| 58 | */ |
|---|
| 59 | var $schema_version = 1100; |
|---|
| 60 | |
|---|
| 61 | /** |
|---|
| 62 | * Name of author of module |
|---|
| 63 | * |
|---|
| 64 | * @var string |
|---|
| 65 | */ |
|---|
| 66 | var $author; |
|---|
| 67 | |
|---|
| 68 | /** |
|---|
| 69 | * URL for author of module |
|---|
| 70 | * |
|---|
| 71 | * @var unknown_type |
|---|
| 72 | */ |
|---|
| 73 | var $author_url; |
|---|
| 74 | |
|---|
| 75 | /** |
|---|
| 76 | * Wiki Page title. Used to generate link to OWA wiki for this module. |
|---|
| 77 | * |
|---|
| 78 | * Must be unique or else it will could clobber another wiki page. |
|---|
| 79 | * |
|---|
| 80 | * @var string |
|---|
| 81 | */ |
|---|
| 82 | var $wiki_title; |
|---|
| 83 | |
|---|
| 84 | /** |
|---|
| 85 | * name used in display situations |
|---|
| 86 | * |
|---|
| 87 | * @var unknown_type |
|---|
| 88 | */ |
|---|
| 89 | var $display_name; |
|---|
| 90 | |
|---|
| 91 | /** |
|---|
| 92 | * Array of event names that this module has handlers for |
|---|
| 93 | * |
|---|
| 94 | * @var array |
|---|
| 95 | */ |
|---|
| 96 | var $subscribed_events; |
|---|
| 97 | |
|---|
| 98 | /** |
|---|
| 99 | * Array of link information for admin panels that this module implements. |
|---|
| 100 | * |
|---|
| 101 | * @var array |
|---|
| 102 | */ |
|---|
| 103 | var $admin_panels; |
|---|
| 104 | |
|---|
| 105 | /** |
|---|
| 106 | * Array of navigation links that this module implements |
|---|
| 107 | * |
|---|
| 108 | * @var unknown_type |
|---|
| 109 | */ |
|---|
| 110 | var $nav_links; |
|---|
| 111 | |
|---|
| 112 | /** |
|---|
| 113 | * Array of metric names that this module implements |
|---|
| 114 | * |
|---|
| 115 | * @var unknown_type |
|---|
| 116 | */ |
|---|
| 117 | var $metrics; |
|---|
| 118 | |
|---|
| 119 | /** |
|---|
| 120 | * Array of graphs that are implemented by this module |
|---|
| 121 | * |
|---|
| 122 | * @var array |
|---|
| 123 | */ |
|---|
| 124 | var $graphs; |
|---|
| 125 | |
|---|
| 126 | /** |
|---|
| 127 | * The Module Group that the module belongs to. |
|---|
| 128 | * |
|---|
| 129 | * This is used often to group a module's features or functions together in the UI |
|---|
| 130 | * |
|---|
| 131 | * @var string |
|---|
| 132 | */ |
|---|
| 133 | var $group; |
|---|
| 134 | |
|---|
| 135 | /** |
|---|
| 136 | * Array of Entities that are implmented by the module |
|---|
| 137 | * |
|---|
| 138 | * @var array |
|---|
| 139 | */ |
|---|
| 140 | var $entities = array(); |
|---|
| 141 | |
|---|
| 142 | /** |
|---|
| 143 | * Required Schema Version |
|---|
| 144 | * |
|---|
| 145 | * @var array |
|---|
| 146 | */ |
|---|
| 147 | var $required_schema_version; |
|---|
| 148 | |
|---|
| 149 | /** |
|---|
| 150 | * Available Updates |
|---|
| 151 | * |
|---|
| 152 | * @var array |
|---|
| 153 | */ |
|---|
| 154 | var $updates = array(); |
|---|
| 155 | |
|---|
| 156 | /** |
|---|
| 157 | * Constructor |
|---|
| 158 | * |
|---|
| 159 | * @return owa_module |
|---|
| 160 | */ |
|---|
| 161 | function owa_module() { |
|---|
| 162 | |
|---|
| 163 | $this->owa_base(); |
|---|
| 164 | |
|---|
| 165 | // register event handlers unless OWA is operating in async handling mode |
|---|
| 166 | if ($this->config['async_db'] == false): |
|---|
| 167 | $this->_registerEventHandlers(); |
|---|
| 168 | endif; |
|---|
| 169 | |
|---|
| 170 | $this->_registerEntities(); |
|---|
| 171 | |
|---|
| 172 | return; |
|---|
| 173 | |
|---|
| 174 | } |
|---|
| 175 | |
|---|
| 176 | /** |
|---|
| 177 | * Returns array of admin Links for this module to be used in navigation |
|---|
| 178 | * |
|---|
| 179 | * @access public |
|---|
| 180 | * @return array |
|---|
| 181 | */ |
|---|
| 182 | function getAdminPanels() { |
|---|
| 183 | |
|---|
| 184 | return $this->admin_panels; |
|---|
| 185 | } |
|---|
| 186 | |
|---|
| 187 | /** |
|---|
| 188 | * Returns array of report links for this module that will be |
|---|
| 189 | * used in report navigation |
|---|
| 190 | * |
|---|
| 191 | * @access public |
|---|
| 192 | * @return array |
|---|
| 193 | */ |
|---|
| 194 | function getNavigationLinks() { |
|---|
| 195 | |
|---|
| 196 | return $this->nav_links; |
|---|
| 197 | } |
|---|
| 198 | |
|---|
| 199 | /** |
|---|
| 200 | * Abstract method for registering event handlers |
|---|
| 201 | * |
|---|
| 202 | * @access public |
|---|
| 203 | * @return array |
|---|
| 204 | */ |
|---|
| 205 | function _registerEventHandlers() { |
|---|
| 206 | |
|---|
| 207 | return; |
|---|
| 208 | } |
|---|
| 209 | |
|---|
| 210 | /** |
|---|
| 211 | * Abstract method for registering administration panels |
|---|
| 212 | * |
|---|
| 213 | * @access public |
|---|
| 214 | * @return array |
|---|
| 215 | */ |
|---|
| 216 | function _registerAdminPanels() { |
|---|
| 217 | |
|---|
| 218 | return; |
|---|
| 219 | } |
|---|
| 220 | |
|---|
| 221 | /** |
|---|
| 222 | * Attaches an event handler to the event queue |
|---|
| 223 | * |
|---|
| 224 | * @param array $event_name |
|---|
| 225 | * @param string $handler_name |
|---|
| 226 | * @return boolean |
|---|
| 227 | */ |
|---|
| 228 | function _addHandler($event_name, $handler_name) { |
|---|
| 229 | |
|---|
| 230 | $handler_dir = OWA_BASE_DIR.DIRECTORY_SEPARATOR.'modules'.DIRECTORY_SEPARATOR.$this->name.DIRECTORY_SEPARATOR.'handlers'; |
|---|
| 231 | |
|---|
| 232 | $class = 'owa_'.$handler_name; |
|---|
| 233 | |
|---|
| 234 | // Require class file if class does not already exist |
|---|
| 235 | if(!class_exists('owa_'.$handler_name)): |
|---|
| 236 | require_once($handler_dir.DIRECTORY_SEPARATOR.$handler_name.'.php'); |
|---|
| 237 | endif; |
|---|
| 238 | |
|---|
| 239 | $handler = &owa_lib::factory($handler_dir,'owa_', $handler_name); |
|---|
| 240 | $handler->_priority = PEAR_LOG_INFO; |
|---|
| 241 | |
|---|
| 242 | $eq = &eventQueue::get_instance(); |
|---|
| 243 | |
|---|
| 244 | // Register event names for this handler |
|---|
| 245 | if(is_array($event_name)): |
|---|
| 246 | |
|---|
| 247 | foreach ($event_name as $k => $name) { |
|---|
| 248 | $handler->_event_type[] = $name; |
|---|
| 249 | } |
|---|
| 250 | |
|---|
| 251 | else: |
|---|
| 252 | $handler->_event_type[] = $event_name; |
|---|
| 253 | |
|---|
| 254 | endif; |
|---|
| 255 | |
|---|
| 256 | $eq->attach($handler); |
|---|
| 257 | |
|---|
| 258 | return ; |
|---|
| 259 | |
|---|
| 260 | } |
|---|
| 261 | |
|---|
| 262 | /** |
|---|
| 263 | * Registers an admin panel with this module |
|---|
| 264 | * |
|---|
| 265 | */ |
|---|
| 266 | function addAdminPanel($panel) { |
|---|
| 267 | |
|---|
| 268 | $this->admin_panels[] = $panel; |
|---|
| 269 | |
|---|
| 270 | return true; |
|---|
| 271 | } |
|---|
| 272 | |
|---|
| 273 | /** |
|---|
| 274 | * Registers Navigation Link with a particular View |
|---|
| 275 | * |
|---|
| 276 | */ |
|---|
| 277 | /* |
|---|
| 278 | function addNavigationLink($link) { |
|---|
| 279 | |
|---|
| 280 | $this->nav_links[] = $link; |
|---|
| 281 | |
|---|
| 282 | return; |
|---|
| 283 | } |
|---|
| 284 | */ |
|---|
| 285 | |
|---|
| 286 | /** |
|---|
| 287 | * Registers Group Link with a particular View |
|---|
| 288 | * |
|---|
| 289 | */ |
|---|
| 290 | function addNavigationLink($group, $subgroup = '', $ref, $anchortext, $order = 0, $priviledge = 'viewer') { |
|---|
| 291 | |
|---|
| 292 | $link = array('ref' => $ref, |
|---|
| 293 | 'anchortext' => $anchortext, |
|---|
| 294 | 'order' => $order, |
|---|
| 295 | 'priviledge' => $priviledge); |
|---|
| 296 | |
|---|
| 297 | if (!empty($subgroup)): |
|---|
| 298 | $this->nav_links[$group][$subgroup]['subgroup'][] = $link; |
|---|
| 299 | else: |
|---|
| 300 | $this->nav_links[$group][$anchortext] = $link; |
|---|
| 301 | endif; |
|---|
| 302 | |
|---|
| 303 | return; |
|---|
| 304 | } |
|---|
| 305 | |
|---|
| 306 | |
|---|
| 307 | /** |
|---|
| 308 | * Registers Entity |
|---|
| 309 | * |
|---|
| 310 | */ |
|---|
| 311 | function _addEntity($entity_name) { |
|---|
| 312 | |
|---|
| 313 | if (is_array($entity_name)): |
|---|
| 314 | $this->entities = array_merge($this->entities, $entity_name); |
|---|
| 315 | else: |
|---|
| 316 | $this->entities[] = $entity_name; |
|---|
| 317 | endif; |
|---|
| 318 | |
|---|
| 319 | return; |
|---|
| 320 | } |
|---|
| 321 | |
|---|
| 322 | function getEntities() { |
|---|
| 323 | |
|---|
| 324 | return $this->entities; |
|---|
| 325 | } |
|---|
| 326 | |
|---|
| 327 | /** |
|---|
| 328 | * Installation method |
|---|
| 329 | * |
|---|
| 330 | * Creates database tables and sets schema version |
|---|
| 331 | * |
|---|
| 332 | */ |
|---|
| 333 | function install() { |
|---|
| 334 | |
|---|
| 335 | $this->e->notice('Starting installation of module: '.$this->name); |
|---|
| 336 | |
|---|
| 337 | $errors = ''; |
|---|
| 338 | |
|---|
| 339 | // Install schema |
|---|
| 340 | if (!empty($this->entities)): |
|---|
| 341 | |
|---|
| 342 | foreach ($this->entities as $k => $v) { |
|---|
| 343 | |
|---|
| 344 | $entity = owa_coreAPI::entityFactory($this->name.'.'.$v); |
|---|
| 345 | //$this->e->debug("about to execute createtable"); |
|---|
| 346 | $status = $entity->createTable(); |
|---|
| 347 | |
|---|
| 348 | if ($status != true): |
|---|
| 349 | $this->e->notice("Entity Installation Failed."); |
|---|
| 350 | $errors = true; |
|---|
| 351 | //return false; |
|---|
| 352 | endif; |
|---|
| 353 | |
|---|
| 354 | } |
|---|
| 355 | |
|---|
| 356 | endif; |
|---|
| 357 | |
|---|
| 358 | // activate module and persist configuration changes |
|---|
| 359 | if ($errors != true): |
|---|
| 360 | |
|---|
| 361 | // run post install hook |
|---|
| 362 | $ret = $this->postInstall(); |
|---|
| 363 | |
|---|
| 364 | if ($ret == true): |
|---|
| 365 | // save schema version to configuration |
|---|
| 366 | $this->c->setSetting($this->name, 'schema_version', $this->schema_version); |
|---|
| 367 | //activate the module and save the configuration |
|---|
| 368 | $this->activate(); |
|---|
| 369 | $this->e->notice("Installation complete."); |
|---|
| 370 | return true; |
|---|
| 371 | else: |
|---|
| 372 | $this->e->notice("Post install proceadure failed."); |
|---|
| 373 | return true; |
|---|
| 374 | endif; |
|---|
| 375 | else: |
|---|
| 376 | $this->e->notice("Installation failed."); |
|---|
| 377 | return false; |
|---|
| 378 | endif; |
|---|
| 379 | |
|---|
| 380 | } |
|---|
| 381 | |
|---|
| 382 | /** |
|---|
| 383 | * Post installation hook |
|---|
| 384 | * |
|---|
| 385 | */ |
|---|
| 386 | function postInstall() { |
|---|
| 387 | |
|---|
| 388 | return false; |
|---|
| 389 | } |
|---|
| 390 | |
|---|
| 391 | /** |
|---|
| 392 | * Checks for and applies schema upgrades for the module |
|---|
| 393 | * |
|---|
| 394 | */ |
|---|
| 395 | function update() { |
|---|
| 396 | |
|---|
| 397 | // list files in a directory |
|---|
| 398 | $files = owa_lib::listDir(OWA_DIR.'modules'.DIRECTORY_SEPARATOR.$this->name.DIRECTORY_SEPARATOR.'updates', false); |
|---|
| 399 | //print_r($files); |
|---|
| 400 | |
|---|
| 401 | $current_schema_version = $this->c->get($this->name, 'schema_version'); |
|---|
| 402 | |
|---|
| 403 | // extract sequence |
|---|
| 404 | foreach ($files as $k => $v) { |
|---|
| 405 | // the use of %d casts the sequence number as an int which is critical for maintaining the |
|---|
| 406 | // order of the keys in the array that we are going ot create that holds the update objs |
|---|
| 407 | //$n = sscanf($v['name'], '%d_%s', $seq, $classname); |
|---|
| 408 | $seq = substr($v['name'], 0, -4); |
|---|
| 409 | |
|---|
| 410 | settype($seq, "integer"); |
|---|
| 411 | |
|---|
| 412 | if ($seq > $current_schema_version): |
|---|
| 413 | |
|---|
| 414 | if ($seq <= $this->required_schema_version): |
|---|
| 415 | $this->updates[$seq] = owa_coreAPI::updateFactory($this->name, substr($v['name'], 0, -4)); |
|---|
| 416 | // set schema version from sequence number in file name. This ensures that only one update |
|---|
| 417 | // class can ever be in use for a particular schema version |
|---|
| 418 | $this->updates[$seq]->schema_version = $seq; |
|---|
| 419 | endif; |
|---|
| 420 | endif; |
|---|
| 421 | |
|---|
| 422 | } |
|---|
| 423 | |
|---|
| 424 | // sort the array |
|---|
| 425 | ksort($this->updates, SORT_NUMERIC); |
|---|
| 426 | |
|---|
| 427 | //print_r(array_keys($this->updates)); |
|---|
| 428 | |
|---|
| 429 | foreach ($this->updates as $k => $obj) { |
|---|
| 430 | |
|---|
| 431 | $this->e->notice(sprintf("Applying Update %d (%s)", $k, get_class($obj))); |
|---|
| 432 | |
|---|
| 433 | $ret = $obj->apply(); |
|---|
| 434 | |
|---|
| 435 | if ($ret == true): |
|---|
| 436 | $this->e->notice("Update Suceeded"); |
|---|
| 437 | else: |
|---|
| 438 | $this->e->notice("Update Failed"); |
|---|
| 439 | return false; |
|---|
| 440 | endif; |
|---|
| 441 | } |
|---|
| 442 | |
|---|
| 443 | return true; |
|---|
| 444 | } |
|---|
| 445 | |
|---|
| 446 | /** |
|---|
| 447 | * Deactivates and removes schema for the module |
|---|
| 448 | * |
|---|
| 449 | */ |
|---|
| 450 | function uninstall() { |
|---|
| 451 | |
|---|
| 452 | return; |
|---|
| 453 | } |
|---|
| 454 | |
|---|
| 455 | /** |
|---|
| 456 | * Places the Module into the active module list in the global configuration |
|---|
| 457 | * |
|---|
| 458 | */ |
|---|
| 459 | function activate() { |
|---|
| 460 | |
|---|
| 461 | if ($this->name != 'base'): |
|---|
| 462 | |
|---|
| 463 | $this->c->setSetting($this->name, 'is_active', true); |
|---|
| 464 | $this->c->save(); |
|---|
| 465 | |
|---|
| 466 | endif; |
|---|
| 467 | |
|---|
| 468 | return; |
|---|
| 469 | } |
|---|
| 470 | |
|---|
| 471 | /** |
|---|
| 472 | * Deactivates the module by removing it from |
|---|
| 473 | * the active module list in the global configuration |
|---|
| 474 | * |
|---|
| 475 | */ |
|---|
| 476 | function deactivate() { |
|---|
| 477 | |
|---|
| 478 | if ($this->name != 'base'): |
|---|
| 479 | |
|---|
| 480 | $this->c->setSetting($this->name, 'is_active', false); |
|---|
| 481 | $this->c->save(); |
|---|
| 482 | |
|---|
| 483 | endif; |
|---|
| 484 | |
|---|
| 485 | return; |
|---|
| 486 | } |
|---|
| 487 | |
|---|
| 488 | /** |
|---|
| 489 | * Registers a set of entities for the module |
|---|
| 490 | * |
|---|
| 491 | */ |
|---|
| 492 | function _registerEntities() { |
|---|
| 493 | |
|---|
| 494 | return false; |
|---|
| 495 | } |
|---|
| 496 | |
|---|
| 497 | /** |
|---|
| 498 | * Checks to se if the schema is up to date |
|---|
| 499 | * |
|---|
| 500 | */ |
|---|
| 501 | function isSchemaCurrent() { |
|---|
| 502 | |
|---|
| 503 | $current_schema = $this->c->get($this->name, 'schema_version'); |
|---|
| 504 | |
|---|
| 505 | if ($current_schema >= $this->required_schema_version): |
|---|
| 506 | return true; |
|---|
| 507 | else: |
|---|
| 508 | return false; |
|---|
| 509 | endif; |
|---|
| 510 | } |
|---|
| 511 | |
|---|
| 512 | /** |
|---|
| 513 | * Registers updates |
|---|
| 514 | * |
|---|
| 515 | */ |
|---|
| 516 | function _registerUpdates() { |
|---|
| 517 | |
|---|
| 518 | |
|---|
| 519 | |
|---|
| 520 | return; |
|---|
| 521 | |
|---|
| 522 | } |
|---|
| 523 | |
|---|
| 524 | /** |
|---|
| 525 | * Adds an update class into the update array. |
|---|
| 526 | * This should be used to within the _registerUpdates method or else |
|---|
| 527 | * it will not get called. |
|---|
| 528 | * |
|---|
| 529 | */ |
|---|
| 530 | function _addUpdate($sequence, $class) { |
|---|
| 531 | |
|---|
| 532 | $this->updates[$sequence] = $class; |
|---|
| 533 | |
|---|
| 534 | return true; |
|---|
| 535 | } |
|---|
| 536 | |
|---|
| 537 | } |
|---|
| 538 | |
|---|
| 539 | ?> |
|---|