function testCmp() { $mds1 = Metadata_Structure::getOneFromDb(['metadata_structure_id' => 6001], $this->DB); $mds2 = Metadata_Structure::getOneFromDb(['metadata_structure_id' => 6004], $this->DB); $this->assertEqual(-1, Metadata_Structure::cmp($mds2, $mds1)); $this->assertEqual(1, Metadata_Structure::cmp($mds1, $mds2)); $this->assertEqual(0, Metadata_Structure::cmp($mds2, $mds2)); $mds3 = Metadata_Structure::getOneFromDb(['metadata_structure_id' => 6002], $this->DB); $this->assertEqual(-1, Metadata_Structure::cmp($mds1, $mds3)); $this->assertEqual(1, Metadata_Structure::cmp($mds3, $mds1)); $this->assertEqual(-1, Metadata_Structure::cmp($mds2, $mds3)); $this->assertEqual(1, Metadata_Structure::cmp($mds3, $mds2)); $mds = Metadata_Structure::getAllFromDb(['parent_metadata_structure_id' => 0], $this->DB); usort($mds, 'Metadata_Structure::cmp'); $this->assertEqual('leaf', $mds[0]->name); $this->assertEqual('flower', $mds[1]->name); }
public function canActOnTarget($action, $target) { // system admin -> always yes // owner of target -> always yes, except for verification // all other situatons -> check role action targets // - matching globals -> yes // - specifics // + gets messy // - otherwise -> no if (is_string($action)) { global $ACTIONS; $action = $ACTIONS[$action]; } // system admin -> always yes if ($this->flag_is_system_admin) { return true; } // util_prePrintR($action); // util_prePrintR($target); // owner of target -> always yes, except for verification if ($target->user_id == $this->user_id) { if ($action->name != 'verify') { return true; } } // view & list is controlled by flags on the object rather than explicit permissions / access records if ($action->name == 'view' || $action->name == 'list') { if (array_key_exists('flag_active', $target->fieldValues)) { if ($target->flag_active) { return true; } } elseif (array_key_exists('flag_workflow_published', $target->fieldValues)) { if ($target->flag_workflow_published && $target->flag_workflow_validated) { return true; } } } // all other situatons -> check role action targets $this->cacheRoleActionTargets(); // - matching globals -> yes $target_global_type = Role_Action_Target::getGlobalTargetTypeForObject($target); if (in_array($target_global_type, array_keys($this->cached_role_action_targets_hash_by_target_type_by_id))) { foreach ($this->cached_role_action_targets_hash_by_target_type_by_id[$target_global_type] as $glob_rat) { if ($glob_rat->action_id == $action->action_id) { return true; } } } // util_prePrintR($this->cached_role_action_targets_hash_by_target_type_by_id); // - specifics // + gets messy // if the allowed target types do not contain the specific type of the target in question, then no need to go further // get a list of all the specific ids to check. This gets a bit messy as we have to climb or include a hierarchy depending on what exactly the target is // util_prePrintR($target); $ids_to_check = array(); $target_class = get_class($target); switch ($target_class) { case 'Authoritative_Plant': $ids_to_check = array($target->authoritative_plant_id); break; case 'Authoritative_Plant_Extra': // can act on this if can act on the plant return $this->canActOnTarget($action, $target->getAuthoritativePlant()); break; case 'Metadata_Structure': // can edit this if can edit itself or any parent $ids_to_check = Db_Linked::arrayOfAttrValues($target->getLineage(), 'metadata_structure_id'); break; case 'Metadata_Term_Set': // can edit if can edit any structure that uses this term set $structures = Metadata_Structure::getAllFromDb(['metadata_term_set_id' => $target->metadata_term_set_id], $this->dbConnection); $ids_to_check = array(); foreach ($structures as $s) { $ids_to_check = array_merge($ids_to_check, Db_Linked::arrayOfAttrValues($s->getLineage(), 'metadata_structure_id')); } break; case 'Metadata_Term_Value': // can edit if can edit any structure that uses the term set for which this is a value return $this->canActOnTarget($action, Metadata_Term_Set::getOneFromDb(['metadata_term_set_id' => $target->metadata_term_set_id], $this->dbConnection)); break; case 'Metadata_Reference': // can edit if can edit anything to which this refers return $this->canActOnTarget($action, $target->getReferrent()); break; case 'Notebook': $ids_to_check = array($target->notebook_id); break; case 'Notebook_Page': // can act on if can act on the notebook that contains this page return $this->canActOnTarget($action, $target->getNotebook()); break; case 'Notebook_Page_Field': // can act on if can act on the notebook that contains the notebook page that this page field return $this->canActOnTarget($action, $target->getNotebookPage()->getNotebook()); break; case 'Specimen': $ids_to_check = array($target->specimen_id); break; case 'Specimen_Image': // can act on if can act on the specimen return $this->canActOnTarget($action, $target->getSpecimen()); break; default: break; } // util_prePrintR($ids_to_check); // util_prePrintR($this->cached_role_action_targets_hash_by_target_type_by_id); $target_specific_type = Role_Action_Target::getSpecificTargetTypeForObject($target); if (!in_array($target_specific_type, array_keys($this->cached_role_action_targets_hash_by_target_type_by_id))) { return false; } foreach ($this->cached_role_action_targets_hash_by_target_type_by_id[$target_specific_type] as $spec_rat) { if ($spec_rat->action_id == $action->action_id && in_array($spec_rat->target_id, $ids_to_check)) { if ($action->name == 'view') { $actual_target = $spec_rat->getTargets()[0]; if (array_key_exists('flag_workflow_published', $actual_target->fieldValues)) { return $actual_target->flag_workflow_published && $actual_target->flag_workflow_validated; } } return true; } } return false; }
public function getChildren() { $children = Metadata_Structure::getAllFromDb(['parent_metadata_structure_id' => $this->metadata_structure_id, 'flag_delete' => FALSE], $this->dbConnection); // util_prePrintR($children); usort($children, 'Metadata_Structure::cmp'); return $children; }
public function getTargets() { switch ($this->target_type) { case 'global_notebook': return Notebook::getAllFromDb([], $this->dbConnection); break; case 'global_metadata': return Metadata_Structure::getAllFromDb([], $this->dbConnection); break; case 'global_plant': return Authoritative_Plant::getAllFromDb([], $this->dbConnection); break; case 'global_specimen': return Specimen::getAllFromDb([], $this->dbConnection); break; case 'notebook': return array(Notebook::getOneFromDb(['notebook_id' => $this->target_id], $this->dbConnection)); break; case 'metadata_structure': return array(Metadata_Structure::getOneFromDb(['metadata_structure_id' => $this->target_id], $this->dbConnection)); break; case 'plant': return array(Authoritative_Plant::getOneFromDb(['authoritative_id' => $this->target_id], $this->dbConnection)); break; case 'specimen': return array(Specimen::getOneFromDb(['specimen_id' => $this->target_id], $this->dbConnection)); break; default: return array(); } }
public function loadStructures() { $this->structures = Metadata_Structure::getAllFromDb(['metadata_term_set_id' => $this->metadata_term_set_id, 'flag_delete' => FALSE], $this->dbConnection); usort($this->structures, 'Metadata_Structure::cmp'); }
$req_key = 'new_ordering-item-metadata_structure_' . $ss->metadata_structure_id; // util_prePrintR('handling '.$req_key); if (isset($_REQUEST[$req_key]) && is_numeric($_REQUEST[$req_key])) { $new_ordering = $_REQUEST[$req_key]; if ($new_ordering != $ss->ordering) { $ss->ordering = $new_ordering; // $ss->matchesDb = false; // not needed because ordering field is access via -> rather than the fieldValues array $ss->updateDb(); } } } $action = 'view'; } if ($action == 'list') { echo '<h2>' . util_lang('all_metadata', 'properize') . '</h2>' . "\n"; $all_metadata_structures = Metadata_Structure::getAllFromDb(['parent_metadata_structure_id' => 0], $DB); usort($all_metadata_structures, 'Metadata_Structure::cmp'); $form_started = false; $available_actions = ''; if ($USER->canActOnTarget($ACTIONS['create'], $all_metadata_structures[0])) { $available_actions .= '<a href="' . APP_ROOT_PATH . '/app_code/metadata_structure.php?action=create&parent_metadata_structure_id=0" id="btn-add-metadata_structure-ROOT" class="creation_link btn" title="' . htmlentities(util_lang('add_metadata_structure')) . '">' . htmlentities(util_lang('add_metadata_structure')) . '</a>'; } if ($USER->canActOnTarget($ACTIONS['edit'], $all_metadata_structures[0])) { $available_actions .= '<button id="ordering-submit-control" class="btn btn-success" type="submit" name="ordering-submit-control" value="update_top_level_orderings"><i class="icon-ok-sign icon-white"></i> ' . util_lang('save_ordering') . '</button>' . "\n"; echo '<form action="' . APP_ROOT_PATH . '/app_code/metadata_structure.php">' . "\n"; echo ' <input type="hidden" name="action" value="update_top_level_orderings"/>' . "\n"; $form_started = true; } echo '<ul class="all-metadata-structures">' . "\n"; if ($available_actions) { echo '<li id="actions">' . $available_actions . '</li>' . "\n";