root/trunk/owa_coreAPI.php

Revision 558, 22.4 kB (checked in by padams, 3 days ago)

fixed several bugs in serviceUser
renamed 'Administrator' role to 'admin' to maintain backwards compatability
fixed bogus error msg when applying module updates successfully

Line 
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
19require_once(OWA_BASE_DIR.'/owa_base.php');
20require_once(OWA_BASE_DIR.'/owa_lib.php');
21
22/**
23 * OWA Core API
24 *
25 * @author      Peter Adams <peter@openwebanalytics.com>
26 * @copyright   Copyright &copy; 2006 Peter Adams <peter@openwebanalytics.com>
27 * @license     http://www.gnu.org/copyleft/gpl.html GPL v2.0
28 * @category    owa
29 * @package     owa
30 * @version             $Revision$           
31 * @since               owa 1.0.0
32 */
33
34class owa_coreAPI extends owa_base {
35       
36        var $modules;
37       
38        var $admin_panels;
39       
40        var $init;
41       
42        /**
43         * Array of modules whose schemas are out of date
44         *
45         * @var array
46         */
47        var $modules_needing_updates = array();
48       
49        /**
50         * Flag for schema update required
51         *
52         * @var boolean
53         */
54        var $update_required;
55       
56        /**
57         * Container for request params
58         *
59         * @var array
60         */
61        var $params;
62       
63        /**
64         * Container for caller config overrides.
65         *
66         * @var array
67         */
68        var $caller_config_overrides;
69       
70       
71        function owa_coreAPI() {
72               
73                $this->owa_base();
74               
75                return;
76        }
77       
78        function &singleton($params = array()) {
79               
80                static $api;
81               
82                if(!isset($api)):
83                        $api = new owa_coreAPI();
84                endif;
85               
86                if(!empty($params)):
87                        $api->params = $params;
88                endif;
89               
90                return $api;
91        }
92       
93        function setupStorageEngine($type) {
94       
95                if (!class_exists('owa_db')):
96                        require_once(OWA_BASE_CLASSES_DIR.'owa_db.php');
97                endif;
98                       
99                $connection_class = "owa_db_" . $type;
100                $connection_class_path = OWA_PLUGINS_DIR.'/db/' . $connection_class . ".php";
101       
102                if (!require_once($connection_class_path)):
103                        $e->emerg(sprintf('Cannot locate proper db class at %s. Exiting.', $connection_class_path));
104                endif;
105               
106                return;
107
108        }
109       
110        function &dbSingleton() {
111               
112                static $db;
113       
114                if (!isset($db)):
115                       
116                        //$c = &owa_coreAPI::configSingleton();
117                        //$config = $c->fetch('base');
118                        //$e = &owa_error::get_instance();
119                       
120                        if (!class_exists('owa_db')):
121                                require_once(OWA_BASE_CLASSES_DIR.'owa_db.php');
122                        endif;
123                       
124                        $connection_class = "owa_db_" . OWA_DB_TYPE;
125                        $connection_class_path = OWA_PLUGINS_DIR.'/db/' . $connection_class . ".php";
126       
127                        if (!require_once($connection_class_path)):
128                                $e->emerg(sprintf('Cannot locate proper db class at %s. Exiting.',
129                                                                $connection_class_path));
130                                return;
131                        else:   
132                                $db = new $connection_class;
133                               
134                                //$this->e->debug(sprintf('Using db class at %s.',      $connection_class_path));
135                        endif; 
136                       
137                endif;
138               
139                return $db;
140               
141        }
142       
143        function authSingleton() {
144                       
145                static $auth_modules;
146                $auth_mdules = array();
147               
148                if (empty($auth_modules['plugin'])):
149                       
150                        $c = &owa_coreAPI::configSingleton();
151                        $plugin = $c->get('base', 'authentication');
152                       
153                endif;
154               
155                // this needs to not be a singleton
156                $auth_modules[$plugin] = &owa_lib::singleton(OWA_PLUGIN_DIR.'auth'.DIRECTORY_SEPARATOR, 'owa_auth_', $plugin);
157               
158                return $auth_modules[$plugin];
159
160               
161                return;
162        }
163       
164        function &configSingleton($params = array()) {
165               
166                static $config;
167               
168                if(!isset($config)):
169                       
170                        if (!class_exists('owa_settings')):
171                                require_once(OWA_BASE_CLASS_DIR.'settings.php');
172                        endif;
173                       
174                        $config = owa_coreAPI::supportClassFactory('base', 'settings');
175                       
176                endif;
177               
178                return $config;
179        }
180       
181        function &errorSingleton() {
182               
183                static $e;
184               
185                if(!isset($e)):
186                       
187                        if (!class_exists('owa_error')):
188                                require_once(OWA_BASE_CLASS_DIR.'error.php');
189                        endif;
190                       
191                        $e = owa_coreAPI::supportClassFactory('base', 'error');
192                       
193                endif;
194               
195                return $e;
196        }
197       
198        function &getSetting($module, $name) {
199               
200                $s = &owa_coreAPI::configSingleton();
201                return $s->get($module, $name);
202        }
203       
204        function getAllRoles() {
205               
206                $caps = owa_coreAPI::getSetting('base', 'capabilities');
207                return array_keys($caps);
208        }
209       
210        function &getCurrentUser() {
211               
212                $s = &owa_coreAPI::serviceSingleton();
213                return $s->getCurrentUser();
214        }
215       
216        /**
217         * check to see if the current user has a capability
218         * always returns a bool
219         */
220        function isCurrentUserCapable($capability) {
221               
222                $cu = &owa_coreAPI::getCurrentUser();
223                return $cu->isCapable($capability);
224        }
225       
226        function &serviceSingleton() {
227               
228                static $s;
229               
230                if(empty($s)) {
231                       
232                        if (!class_exists('owa_service')) {
233                                require_once(OWA_BASE_CLASS_DIR.'service.php');
234                        }
235                       
236                        $s = owa_coreAPI::supportClassFactory('base', 'service');
237                       
238                }
239               
240                return $s;
241        }
242       
243        function &cacheSingleton($params = array()) {
244               
245                static $cache;
246               
247                if(!isset($cache)):
248                       
249                        if (!class_exists('owa_cache')):
250                                require_once(OWA_BASE_CLASS_DIR.'cache.php');
251                        endif;
252                       
253                        $cache = owa_coreAPI::supportClassFactory('base', 'cacheFacade');
254                       
255                endif;
256               
257                return $cache;
258        }
259       
260        function requestContainerSingleton() {
261       
262                static $request;
263               
264                if(!isset($request)):
265                       
266                        if (!class_exists('owa_requestContainer')):
267                                require_once(OWA_DIR.'owa_requestContainer.php');
268                        endif;
269                       
270                        $request = owa_lib::factory(OWA_DIR, '', 'owa_requestContainer');
271                       
272                endif;
273               
274                return $request;
275       
276        }
277       
278        function setupFramework() {
279               
280                if ($this->init != true):
281                        $this->_loadModules();
282                        $this->_loadEntities();
283                        $this->init = true;
284                endif;
285               
286                return;
287        }
288       
289        function _loadModules() {
290               
291                $am = $this->getActiveModules();
292               
293                foreach ($am as $k => $v) {
294                       
295                        $m = owa_coreAPI::moduleClassFactory($v);
296                       
297                        $this->modules[$m->name] = $m;
298                       
299                        // check for schema updates
300                        $check = $this->modules[$m->name]->isSchemaCurrent();
301                       
302                        if ($check != true):
303                                $this->modules_needing_updates[] = $m->name;
304                        endif;
305                }
306               
307                // set schema update flag
308                if (!empty($this->modules_needing_updates)):
309                        $this->update_required = true;
310                endif;
311               
312                return;
313        }
314       
315               
316        function _loadEntities() {
317               
318                foreach ($this->modules as $k => $module) {
319                       
320                        foreach ($module->entities as $entitiy_k => $entitiy_v) {
321                       
322                                $this->entities[] = $module->name.$entitiy_v;
323                               
324                        }
325                }
326               
327                return;
328        }
329               
330        function moduleRequireOnce($module, $class_dir, $file) {
331               
332                if (!empty($class_dir)):
333               
334                        $class_dir .= DIRECTORY_SEPARATOR;
335                       
336                endif;
337               
338                return require_once(OWA_BASE_DIR.'/modules/'.$module.DIRECTORY_SEPARATOR.$class_dir.$file.'.php');
339        }
340       
341        function moduleFactory($modulefile, $class_suffix = null, $params = '', $class_ns = 'owa_') {
342               
343                list($module, $file) = split("\.", $modulefile);
344                $class = $class_ns.$file.$class_suffix;
345                //print $class;
346                // Require class file if class does not already exist
347                if(!class_exists($class)):     
348                        owa_coreAPI::moduleRequireOnce($module, '', $file);
349                endif;
350                       
351                $obj = owa_lib::factory(OWA_BASE_DIR.'/modules/'.$module, '', $class, $params);
352               
353                //if (isset($obj->module)):
354                        $obj->module = $module;
355                //endif;
356               
357                return $obj;
358        }
359       
360        function moduleGenericFactory($module, $sub_directory, $file, $class_suffix = null, $params = '', $class_ns = 'owa_') {
361               
362                $class = $class_ns.$file.$class_suffix;
363       
364                // Require class file if class does not already exist
365                if(!class_exists($class)):     
366                        owa_coreAPI::moduleRequireOnce($module, $sub_directory, $file);
367                endif;
368                       
369                $obj = owa_lib::factory(OWA_DIR.'modules'.DIRECTORY_SEPARATOR.$module.DIRECTORY_SEPARATOR.$sub_directory, '', $class, $params);
370               
371                return $obj;
372        }
373       
374        /**
375         * Produces Module Classes (module.php)
376         * 
377         * @return Object module class object
378         */
379        function moduleClassFactory($module) {
380               
381                if (!class_exists('owa_module')):
382                        require_once(OWA_BASE_CLASSES_DIR.'owa_module.php');
383                endif;
384                       
385                require_once(OWA_BASE_DIR.'/modules/'.$module.'/module.php');
386                       
387                return owa_lib::factory(OWA_BASE_CLASSES_DIR.$module, 'owa_', $module.'Module');
388               
389        }
390
391       
392        function updateFactory($module, $filename, $class_ns = 'owa_') {
393       
394                require_once(OWA_BASE_CLASS_DIR.'update.php');
395               
396                //$obj = owa_coreAPI::moduleGenericFactory($module, 'updates', $filename, '_update');
397                $class = $class_ns.$module.'_'.$filename.'_update';
398       
399                // Require class file if class does not already exist
400                if(!class_exists($class)):     
401                        owa_coreAPI::moduleRequireOnce($module, 'updates', $filename);
402                endif;
403                       
404                $obj = owa_lib::factory(OWA_DIR.'modules'.DIRECTORY_SEPARATOR.$module.DIRECTORY_SEPARATOR.'updates', '', $class, $params);
405
406                $obj->module_name = $module;
407                return $obj;
408        }
409               
410        function subViewFactory($subview, $params = array()) {
411               
412                list($module, $class) = split("\.", $subview);
413                //print_r($module.' ' . $class);
414                //owa_lib::moduleRequireOnce($module, $class);
415       
416                $subview =  owa_lib::moduleFactory($subview, 'View', $params);
417                $subview->is_subview = true;
418               
419                return $subview;
420        }
421       
422        function &supportClassFactory($module, $class, $params = array(),$class_ns = 'owa_') {
423               
424                $obj = &owa_lib::factory(OWA_BASE_DIR.DIRECTORY_SEPARATOR.'modules'.DIRECTORY_SEPARATOR.$module.DIRECTORY_SEPARATOR.'classes'.DIRECTORY_SEPARATOR, $class_ns, $class, $params);
425                $obj->module = $module;
426               
427                return $obj;
428               
429               
430        }
431       
432        /**
433         * Convienence method for generating entities
434         *
435         * @param unknown_type $entity_name
436         * @return unknown
437         */
438        function entityFactory($entity_name) {
439                       
440                /*if (!class_exists('owa_entity')):
441                        require_once(OWA_BASE_CLASSES_DIR.'owa_entity.php');   
442                endif;
443                       
444                return owa_coreAPI::moduleSpecificFactory($entity_name, 'entities', '', '', false);
445                */
446               
447                return owa_coreAPI::supportClassFactory('base', 'entityManager', $entity_name);
448               
449        }
450       
451        /**
452         * Convienence method for generating entities
453         *
454         * @param unknown_type $entity_name
455         * @return unknown
456         */
457        function rawEntityFactory($entity_name) {
458                       
459                if (!class_exists('owa_entity')):
460                        require_once(OWA_BASE_CLASSES_DIR.'owa_entity.php');   
461                endif;
462                       
463                return owa_coreAPI::moduleSpecificFactory($entity_name, 'entities', '', '', false);
464                               
465        }
466               
467        /**
468         * Factory for generating graphs
469         *
470         * @param unknown_type $entity_name
471         * @return unknown
472         */
473        function graphFactory($graph_name, $params = array()) {
474               
475                return owa_coreAPI::moduleSpecificFactory($graph_name, 'graphs', '', $params, false);
476               
477        }
478       
479        /**
480         * Factory for generating module specific classes
481         *
482         * @param string $modulefile
483         * @param string $class_dir
484         * @param string $class_suffix
485         * @param array $params
486         * @return unknown
487         */
488        function moduleSpecificFactory($modulefile, $class_dir, $class_suffix = null, $params = '', $add_module_name = true, $class_ns = 'owa_') {
489               
490                list($module, $file) = split("\.", $modulefile);
491                $class = $class_ns.$file.$class_suffix;
492               
493                // Require class file if class does not already exist
494                if(!class_exists($class)):     
495                        owa_coreAPI::moduleRequireOnce($module, $class_dir, $file);
496                endif;
497                       
498                $obj = owa_lib::factory(OWA_BASE_DIR.DIRECTORY_SEPARATOR.'modules'.DIRECTORY_SEPARATOR.$class_dir.DIRECTORY_SEPARATOR.$module, '', $class, $params);
499               
500                if ($add_module_name == true):
501                        $obj->module = $module;
502                endif;
503               
504                return $obj;
505               
506               
507        }
508       
509        /**
510         * Convienence method for generating metrics
511         *
512         * @param unknown_type $entity_name
513         * @return unknown
514         */
515        function getMetric($metric_name, $params) {
516               
517                $m = owa_coreAPI::metricFactory($metric_name);
518               
519                if (array_key_exists('constraints', $params)):
520                       
521                        foreach ($params['constraints'] as $k => $v) {
522                               
523                                if(is_array($v)):
524                                        $m->setConstraint($k, $v[1], $v[0]);
525                                else:
526                                        $m->setConstraint($k, $value); 
527                                endif;
528                               
529                        }
530                       
531                        unset($params['constraints']);
532                       
533                endif;
534               
535                $m->applyOverrides($params);
536               
537                return $m->generate();
538        }
539       
540        /**
541         * Convienence method for generating metrics
542         *
543         * @param unknown_type $entity_name
544         * @return unknown
545         */
546        function metricFactory($metric_name) {
547               
548                if (!class_exists('owa_metric')):
549               
550                        require_once(OWA_BASE_CLASSES_DIR.'owa_metric.php');
551                       
552                endif;
553               
554                return owa_coreAPI::moduleSpecificFactory($metric_name, 'metrics', '', $this->params, false);
555               
556        }
557
558
559
560       
561        /**
562         * Returns a consolidated list of admin/options panels from all active modules
563         *
564         * @return array
565         */
566        function getAdminPanels() {
567               
568                $panels = array();
569               
570                foreach ($this->modules as $k => $v) {
571                        $v->registerAdminPanels();
572                        $module_panels = $v->getAdminPanels();
573                       
574                        foreach ($module_panels as $key => $value) {
575                               
576                                $panels[$value['group']][] = $value;
577                        }
578                       
579                }
580               
581                return $panels;
582        }
583       
584        /**
585         * Returns a consolidated list of nav links from all active modules for a particular view
586         * and named navigation element.
587         *
588         * @param string nav_name the name of the navigation element that you want links for
589         * @param string sortby the array value to sort the navigation array by
590         * @return array
591         */
592        function getNavigation($view, $nav_name, $sortby ='order') {
593               
594                $links = array();
595               
596                foreach ($this->modules as $k => $v) {
597                       
598                        // If the module does not have nav links, register them. needed in case this function is called twice on
599                        // same view.
600                        if (empty($v->nav_links)):
601                                $v->registerNavigation();
602                        endif;         
603                       
604                        $module_nav = $v->getNavigationLinks();
605                       
606       
607                        if (!empty($module_nav)):
608                                // assemble the navigation for a specific view's named navigation element'     
609                                foreach ($module_nav as $key => $value) {
610                                       
611                                        $links[$value['view']][$value['nav_name']][] = $value;
612                                }
613                        endif;
614                       
615                }
616               
617                //print_r($links[$view][$nav_name]);
618                if (!empty($links[$view][$nav_name])):
619                        // anonymous sorting function, takes sort by variable.
620                        $code = "return strnatcmp(\$a['$sortby'], \$b['$sortby']);";
621                       
622                        // sort the array
623                        $ret = usort($links[$view][$nav_name], create_function('$a,$b', $code));
624                       
625                        return $links[$view][$nav_name];
626                else: 
627                        return false;
628                endif;
629                 
630        }
631       
632        function getGroupNavigation($group, $sortby ='order') {
633       
634                $links = array();
635               
636                foreach ($this->modules as $k => $v) {
637                       
638                        // If the module does not have nav links, register them. needed in case this function is called twice on
639                        // same view.
640                        if (empty($v->nav_links)):
641                                $v->registerNavigation();
642                        endif;         
643                       
644                        $module_nav = $v->getNavigationLinks();
645                       
646                        if (!empty($module_nav)):
647                                //loop through returned nav array
648                                foreach ($module_nav as $group => $nav_links) {
649                                       
650                                        foreach ($nav_links as $link) { 
651                                                                       
652                                                if (array_key_exists($group, $links)):
653                                                       
654                                                        // check to see if link is already present in the main array
655                                                        if (array_key_exists($link['anchortext'], $links[$group])):
656                                                                // merge various elements?? not now.
657                                                                //check to see if there is an existing subgroup
658                                                               
659                                                                if (array_key_exists('subgroup', $links[$group][$link['anchortext']])):
660                                                                        // if so, merge the subgroups
661                                                                        $links[$group][$link['anchortext']]['subgroup'] = array_merge($links[$group][$link['anchortext']]['subgroup'], $link['subgroup']);
662                                                                endif; 
663                                                        else:
664                                                                // else populate the link
665                                                                $links[$group][$link['anchortext']] = $link;   
666                                                        endif;
667                                                       
668                                                else:
669                                                        $links[$group][$link['anchortext']] = $link;
670                                                endif;
671                                        }                                       
672                                       
673                                }
674                        endif;
675                       
676                }
677               
678                return $links[$group];
679               
680                //print_r($links[$view][$nav_name]);
681                if (!empty($links[$group])):
682                        // anonymous sorting function, takes sort by variable.
683                        $code = "return strnatcmp(\$a['$sortby'], \$b['$sortby']);";
684                       
685                        // sort the array
686                        $ret = usort($links[$group], create_function('$a,$b', $code));
687                       
688                        return $links[$group];
689                else: 
690                        return false;
691                endif;
692       
693