public function testexists()
 {
     //unset and reconnect Db to resolve mysqli fetch exeception
     global $db;
     unset($db->database);
     $db->checkConnection();
     $relationship = new Relationship();
     //test with invalid relationship
     $result = $relationship->exists('test_test', $db);
     $this->assertEquals(false, $result);
     //test with valid relationship
     $result = $relationship->exists('roles_users', $db);
     $this->assertEquals(true, $result);
 }
Beispiel #2
0
 /**
  * Populates the relationship meta for a module.
  *
  * It is called during setup/install. It is used statically to create relationship meta data for many-to-many tables.
  *
  * 	@param string $key name of the object.
  * 	@param object $db database handle.
  *  @param string $tablename table, meta data is being populated for.
  *  @param array dictionary vardef dictionary for the object.     *
  *  @param string module_dir name of subdirectory where module is installed.
  *  @param boolean $iscustom Optional,set to true if module is installed in a custom directory. Default value is false.
  *  @static
  *
  *  Internal function, do not override.
  */
 function createRelationshipMeta($key, $db, $tablename, $dictionary, $module_dir, $iscustom = false)
 {
     //load the module dictionary if not supplied.
     if (empty($dictionary) && !empty($module_dir)) {
         if ($iscustom) {
             $filename = 'custom/modules/' . $module_dir . '/Ext/Vardefs/vardefs.ext.php';
         } else {
             if ($key == 'User') {
                 // a very special case for the Employees module
                 // this must be done because the Employees/vardefs.php does an include_once on
                 // Users/vardefs.php
                 $filename = 'modules/Users/vardefs.php';
             } else {
                 $filename = 'modules/' . $module_dir . '/vardefs.php';
             }
         }
         if (file_exists($filename)) {
             include $filename;
             // cn: bug 7679 - dictionary entries defined as $GLOBALS['name'] not found
             if (empty($dictionary) || !empty($GLOBALS['dictionary'][$key])) {
                 $dictionary = $GLOBALS['dictionary'];
             }
         } else {
             $GLOBALS['log']->debug("createRelationshipMeta: no metadata file found" . $filename);
             return;
         }
     }
     if (!is_array($dictionary) or !array_key_exists($key, $dictionary)) {
         $GLOBALS['log']->fatal("createRelationshipMeta: Metadata for table " . $tablename . " does not exist");
         display_notice("meta data absent for table " . $tablename . " keyed to {$key} ");
     } else {
         if (isset($dictionary[$key]['relationships'])) {
             $RelationshipDefs = $dictionary[$key]['relationships'];
             $delimiter = ',';
             global $beanList;
             $beanList_ucase = array_change_key_case($beanList, CASE_UPPER);
             foreach ($RelationshipDefs as $rel_name => $rel_def) {
                 if (isset($rel_def['lhs_module']) and !isset($beanList_ucase[strtoupper($rel_def['lhs_module'])])) {
                     $GLOBALS['log']->debug('skipping orphaned relationship record ' . $rel_name . ' lhs module is missing ' . $rel_def['lhs_module']);
                     continue;
                 }
                 if (isset($rel_def['rhs_module']) and !isset($beanList_ucase[strtoupper($rel_def['rhs_module'])])) {
                     $GLOBALS['log']->debug('skipping orphaned relationship record ' . $rel_name . ' rhs module is missing ' . $rel_def['rhs_module']);
                     continue;
                 }
                 //check whether relationship exists or not first.
                 if (Relationship::exists($rel_name, $db)) {
                     $GLOBALS['log']->debug('Skipping, reltionship already exists ' . $rel_name);
                 } else {
                     //	add Id to the insert statement.
                     $column_list = 'id';
                     $value_list = "'" . create_guid() . "'";
                     //add relationship name to the insert statement.
                     $column_list .= $delimiter . 'relationship_name';
                     $value_list .= $delimiter . "'" . $rel_name . "'";
                     //todo check whether $rel_def is an array or not.
                     //for now make that assumption.
                     //todo specify defaults if meta not defined.
                     foreach ($rel_def as $def_key => $value) {
                         $column_list .= $delimiter . $def_key;
                         $value_list .= $delimiter . "'" . $value . "'";
                     }
                     //create the record. todo add error check.
                     $insert_string = "INSERT into relationships (" . $column_list . ") values (" . $value_list . ")";
                     $db->query($insert_string, true);
                 }
             }
         } else {
             //todo
             //log informational message stating no relationships meta was set for this bean.
         }
     }
 }
Beispiel #3
0
 /**
  * Populates the relationship meta for a module.
  *
  * It is called during setup/install. It is used statically to create relationship meta data for many-to-many tables.
  *
  * 	@param string $key name of the object.
  * 	@param object $db database handle.
  *  @param string $tablename table, meta data is being populated for.
  *  @param array dictionary vardef dictionary for the object.     *
  *  @param string module_dir name of subdirectory where module is installed.
  *  @param boolean $iscustom Optional,set to true if module is installed in a custom directory. Default value is false.
  *  @static
  *
  *  Internal function, do not override.
  */
 function createRelationshipMeta($key, $db, $tablename, $dictionary, $module_dir, $iscustom = false)
 {
     global $beanList;
     //load the module dictionary if not supplied.
     if (empty($dictionary) && !empty($module_dir)) {
         if ($iscustom) {
             $filename = 'custom/modules/' . $module_dir . '/Ext/Vardefs/vardefs.ext.php';
         } else {
             if ($key == 'User') {
                 // a very special case for the Employees module
                 // this must be done because the Employees/vardefs.php does an include_once on
                 // Users/vardefs.php
                 $filename = 'modules/Users/vardefs.php';
             } else {
                 $filename = 'modules/' . $module_dir . '/vardefs.php';
             }
         }
         if (file_exists($filename)) {
             include $filename;
             // cn: bug 7679 - dictionary entries defined as $GLOBALS['name'] not found
             if (empty($dictionary) || !empty($GLOBALS['dictionary'][$key])) {
                 $dictionary = $GLOBALS['dictionary'];
             }
         } else {
             $GLOBALS['log']->debug("createRelationshipMeta: no metadata file found" . $filename);
             return;
         }
     }
     if (!is_array($dictionary) or !array_key_exists($key, $dictionary)) {
         $GLOBALS['log']->fatal("createRelationshipMeta: Metadata for table " . $tablename . " does not exist");
         display_notice("meta data absent for table " . $tablename . " keyed to {$key} ");
     } else {
         if (isset($dictionary[$key]['relationships'])) {
             $RelationshipDefs = $dictionary[$key]['relationships'];
             $beanList_ucase = array_change_key_case($beanList, CASE_UPPER);
             $seed = BeanFactory::getBean("Relationships");
             $keys = array_keys($seed->field_defs);
             foreach ($RelationshipDefs as $rel_name => $rel_def) {
                 if (isset($rel_def['lhs_module']) and !isset($beanList_ucase[strtoupper($rel_def['lhs_module'])])) {
                     $GLOBALS['log']->debug('skipping orphaned relationship record ' . $rel_name . ' lhs module is missing ' . $rel_def['lhs_module']);
                     continue;
                 }
                 if (isset($rel_def['rhs_module']) and !isset($beanList_ucase[strtoupper($rel_def['rhs_module'])])) {
                     $GLOBALS['log']->debug('skipping orphaned relationship record ' . $rel_name . ' rhs module is missing ' . $rel_def['rhs_module']);
                     continue;
                 }
                 //check whether relationship exists or not first.
                 if (Relationship::exists($rel_name, $db)) {
                     $GLOBALS['log']->debug('Skipping, relationship already exists ' . $rel_name);
                 } else {
                     $toInsert = array();
                     foreach ($keys as $key) {
                         if ($key == "id") {
                             $toInsert[$key] = create_guid();
                         } else {
                             if ($key == 'relationship_role_columns') {
                                 if (!empty($rel_def['relationship_role_columns'])) {
                                     $toInsert[$key] = json_encode($rel_def['relationship_role_columns']);
                                 } else {
                                     $toInsert[$key] = '';
                                 }
                             } else {
                                 if ($key == "relationship_name") {
                                     $toInsert[$key] = $rel_name;
                                 } else {
                                     if (isset($rel_def[$key])) {
                                         $toInsert[$key] = $rel_def[$key];
                                     }
                                 }
                             }
                         }
                         //todo specify defaults if meta not defined.
                     }
                     DBManagerFactory::getInstance()->insertParams('relationships', $seed->field_defs, $toInsert);
                     Relationship::$relCacheInternal[$rel_name] = true;
                 }
             }
         } else {
             $GLOBALS['log']->debug("createRelationshipMeta: No relationship metadata set for {$module_dir}");
         }
     }
 }