/**
  * Resets and cascades permissions down to child objects
  * 
  * @access private
  */
 private static function cascade_permissions()
 {
     $db = new argent_database();
     //$db->start_transaction();
     /* Remove old cascade rules */
     $sql = "\r\n                    DELETE FROM\r\n                        `ua_permissions`\r\n                    WHERE\r\n                        `master` != ''\r\n                    ";
     $db->query($sql);
     $sql = "\r\n                    SELECT\r\n                        *\r\n                    FROM\r\n                        `ua_permissions`\r\n                    WHERE\r\n                        `cascade` = 1\r\n                    ";
     $rules = $db->returntable($sql);
     if (count($rules) > 0) {
         foreach ($rules as $rule) {
             $user = $rule['user_id'];
             $meta_guid = $rule['meta_guid'];
             $descendents = argent_meta::descendents($rule['object_id']);
             if (count($descendents) > 0) {
                 foreach ($descendents as $descendent) {
                     $newguid = argent_identifier::meta_guid();
                     $sql = "\r\n                                    INSERT INTO\r\n                                        `ua_permissions`\r\n                                    (\r\n                                        `meta_guid`,\r\n                                        `object_id`,\r\n                                        `user_id`,\r\n                                        `cascade`,\r\n                                        `master`,\r\n                                        `create`,\r\n                                        `read`,\r\n                                        `update`,\r\n                                        `delete`,\r\n                                        `meta_timestamp`\r\n                                    )\r\n                                    VALUES(\r\n                                        '{$newguid}',\r\n                                        '{$descendent}',\r\n                                        '{$user}',\r\n                                        0,\r\n                                        '{$meta_guid}',\r\n                                        {$rule['create']},\r\n                                        {$rule['read']},\r\n                                        {$rule['update']},\r\n                                        {$rule['delete']},\r\n                                        NOW()\r\n                                    )\r\n                                    ";
                     $db->query($sql);
                 }
             }
         }
     }
 }
 public static function find_related($primary_object_id = null, $secondary_object_id = null, $relationship = null, $include_reverse = false)
 {
     $error = new argent_error();
     $db = new argent_database();
     if ($primary_object_id != null && !argent_meta::object_registered($primary_object_id)) {
         $error->add('1038', 'Object does not exist', $primary_object_id, 'argent_uauth');
     }
     if ($secondary_object_id != null && !argent_meta::object_registered($secondary_object_id)) {
         $error->add('1038', 'Object does not exist', $secondary_object_id, 'argent_uauth');
     }
     if (!is_string($relationship) && $relationship != null) {
         $error->add('1050', 'Invalid data type: expecting STRING', $relationship, 'argent_meta');
     }
     if ($error->has_errors()) {
         return $error;
     }
     $sql = "\r\n                    SELECT\r\n                        *\r\n                    FROM\r\n                        `ua_relationships`\r\n                    WHERE\r\n                        ";
     if ($primary_object_id != null || $secondary_object_id != null) {
         $sql .= "\r\n                        (\r\n                        ";
     }
     if ($primary_object_id != null) {
         $sql .= "`primary_object_id` = '{$db->escape_value($primary_object_id)}'";
     }
     if ($primary_object_id != null && $secondary_object_id != null) {
         $sql .= "\r\n                    AND\r\n                    ";
     }
     if ($secondary_object_id != null) {
         $sql .= "\r\n                        `secondary_object_id` = '{$db->escape_value($secondary_object_id)}'\r\n                      ";
     }
     if ($primary_object_id != null || $secondary_object_id != null) {
         $sql .= "\r\n                        )\r\n                    ";
     }
     if ($include_reverse) {
         $sql .= "\r\n                        OR\r\n                          ";
         if ($primary_object_id != null || $secondary_object_id != null) {
             $sql .= "\r\n                            (\r\n                            ";
         }
         if ($secondary_object_id != null) {
             $sql .= "`primary_object_id` = '{$db->escape_value($secondary_object_id)}'";
         }
         if ($primary_object_id != null && $secondary_object_id != null) {
             $sql .= "\r\n                        AND\r\n                        ";
         }
         if ($primary_object_id != null) {
             $sql .= "\r\n                            `secondary_object_id` = '{$db->escape_value($primary_object_id)}'\r\n                          ";
         }
         if ($primary_object_id != null || $secondary_object_id != null) {
             $sql .= "\r\n                            )\r\n                        ";
         }
     }
     if ($relationship != null && ($primary_object_id != null || $secondary_object_id != null)) {
         $sql .= "\r\n                    AND\r\n                    ";
     }
     if ($relationship != null) {
         $sql .= "\r\n                        `relationship` = '{$db->escape_value($relationship)}'\r\n                    ";
     }
     $relationship_data = $db->returntable($sql);
     return $relationship_data;
 }