id = intval($id); $this->type = $type; $this->area_id = intval($area_id); if ($form_uri) { $this->form_uri = $form_uri; } $this->fetch_data(); } protected function fetch_data() { global $msg; if($this->id){ $query = 'select * from contribution_area_forms where id_form = "'.$this->id.'"'; $result = pmb_mysql_query($query); if(pmb_mysql_num_rows($result)){ $params = pmb_mysql_fetch_object($result); $this->parameters = $params->form_parameters; $this->unserialized_parameters = json_decode($this->parameters); $this->name = $params->form_title; $this->comment = $params->form_comment; $this->type = $params->form_type; } } $onto = contribution_area::get_ontology(); $classes = $onto->get_classes(); $class_uri = ""; foreach($classes as $class){ if($class->pmb_name == $this->type){ $this->uri = $class->uri; $properties = $onto->get_class_properties($this->uri); for($i=0 ; $iget_property($this->uri, $properties[$i]); $this->availableProperties[$property->pmb_name] = $property; } if (is_array($class->sub_class_of)) { foreach($class->sub_class_of as $parent_uri) { $properties = $onto->get_class_properties($parent_uri); for($i=0 ; $iget_property($parent_uri, $properties[$i]); if (!$property->is_undisplayed()) { $this->availableProperties[$property->pmb_name] = $property; } } } } break; } } ksort($this->availableProperties); $classes_array = $onto->get_classes_uri(); $classname = ""; if (!empty($classes_array[$this->uri]) && !empty($classes_array[$this->uri]->label)) { $classname = $classes_array[$this->uri]->label; if (substr($classname,0,4) == "msg:") { if (isset($msg[substr($classname,4)])) { $classname = $msg[substr($classname,4)]; } else { // si on trouve pas le message on met juste le code dans le label $classname = substr($classname,4); } } } $this->classname = $classname; } /** * Renvoie le formulaire de paramétrage d'un formulaire * @param int $area Identifiant de l'espace de provenance, pour pouvoir revenir au paramétrage à la sauvegarde */ public function get_form($area = 0) { global $msg; global $charset; global $ontology_tpl; global $type; global $current_module; /** * TODO : Edition / Creation (msg & cie) */ $hasDraft = self::has_draft_contribution_from_id($this->id); $hasScenario = $this->get_scenario_linked($this->id)["count"]; $html = '

'.$msg["admin_contribution_area_form_type"].' : '.$this->classname.'

'; if($this->id){ $html.='
'; } $html.='
'; if ($hasDraft){ $html.='
'.$msg["contribution_has_draft"].'
'; } if ($hasScenario) { $html.='
'.$msg["contribution_disabled_delete"].'
'; } $html.='
'; //Récupèrer la liste des scénario et des espaces ou le formualire est utilisé $list_scenario_linked = array(); $list_scenario_linked = $this->get_scenario_linked($this->id); if (!empty($list_scenario_linked) && $list_scenario_linked["count"] > 0) { $message = str_replace("%a", count($list_scenario_linked)-1, $msg["contribution_scenario_list"]); $message = str_replace("%s", $list_scenario_linked["count"], $message); $html .= ' '; } $html.='
 
'; $this->get_onto_class(); uasort($this->availableProperties, array($this, 'sort_properties')); foreach($this->availableProperties as $key => $property){ // $onto_item = new onto_common_item($this->onto_class, $property->uri); $restriction = $this->onto_class->get_restriction($property->uri); $min = $restriction->get_min(); $datatype_class_name = onto_common_item::search_datatype_class_name($property, $this->onto_class->pmb_name,$this->onto_class->onto_name); $datatype_ui_class_name = onto_common_item::search_datatype_ui_class_name($datatype_class_name, $this->onto_class->pmb_name, $property, $restriction,$this->onto_class->onto_name); /** * recomposition d'un tableau datas ; contenant les instances de datatypes */ $property_data = $this->recompose_data($property->pmb_name,$property->uri, $datatype_class_name); // On a besoin des valeurs disponibles dans les liste pour la génération du champ $property->pmb_extended['list_values'] = $this->get_saved_property($property->pmb_name, 'list_values'); $html.=' '; if($min > 0){ $html.=''; $html.=' '; } $html = str_replace('!!onto_form_name!!', 'contribution_area_form', $html); $html.= '
'.$msg['admin_contribution_area_th_enabled'].' '.$msg['admin_contribution_area_form_fields'].' '.$msg['admin_contribution_area_form_displayed_label'].' '.$msg['admin_contribution_area_form_default_value'].' '.$msg['admin_contribution_area_form_needed_field'].' '.$msg['admin_contribution_area_form_hidden_field'].' '.$msg['admin_contribution_area_form_readonly'].' '.$msg['admin_contribution_area_form_advanced'].'
'; $html.= ''; $td_mandatory =''; }else{ $html.=''; $html.= 'get_saved_property($property->pmb_name) ? 'checked="checked"' : '') .' id="switch_'.$property->pmb_name.'" name="'.$property->pmb_name .'[switch]" class="switch" ' .(($this->get_saved_property($property->pmb_name) && $hasDraft) ? 'onclick="return false;"' : '') .'/>'; $td_mandatory ='get_saved_property($property->pmb_name,'mandatory') || $min>0 ? 'checked="checked"' : '').' value="1">'; } $html.=''; $html.=' ('.$property->get_pmb_datatype_label($property->pmb_datatype).')
'.$this->get_equations_selector($this->get_equations_from_type($property->pmb_datatype, $property->range),$property->pmb_name,$this->get_saved_property($property->pmb_name,'equation')).'
'.$datatype_ui_class_name::get_form('',$property,$this->onto_class->get_restriction($property->uri),$property_data, $this->onto_class->pmb_name,'').'
'.$td_mandatory.' get_saved_property($property->pmb_name,'hidden') ? 'checked="checked"' : '').' value="1"> get_saved_property($property->pmb_name,'readonly') ? 'checked="checked"' : '').' value="1"> '.$msg['admin_contribution_area_form_advanced'].'
 
'; $html.= ''; if ($this->id) { $html.= ' '; } $html.= '
'; if ($this->id) { $html.= '
'; $html .= confirmation_delete("./modelling.php?module=modelling&categ=contribution_area&sub=form&action=delete&type=".$type."&form_id="); } $html.= '
'; return $html; } public function set_from_form() { global $inputs_name; global $charset; global $form_name; global $form_comment; $properties_list = array(); for($i = 0 ; $i < count($inputs_name); $i++){ $property_name = $inputs_name[$i]; global ${$property_name}; $property = ${$property_name}; if (is_array($property) && isset($property['switch']) && ($property['switch'] == 'on')) { $properties_list[$property_name] = stripslashes_array($property); // On gère les traitements particuliers if (!empty($property['default_value'])) { $var_name = $property['default_value']; global ${$var_name}; $default_value = stripslashes_array(${$var_name}); if (!isset($default_value[0])) { // Ce n'est pas un tableau numériques, on ne sait pas quoi en faire... $default_value = array(array('value' => '')); } if ($default_value[0]['type'] != 'http://www.w3.org/2000/01/rdf-schema#Literal' && empty($default_value[0]['value'])) { // Si on n'a pas d'entité choisie, on ne met pas de label // Risque d'avoir un mauvais type d'entité $default_value[0]['display_label'] = ""; } // On récupère l'uri de la propriété $range = array(); $onto = $this->get_onto_class(); $properties = $onto->get_properties(); foreach ($properties as $property_uri) { if (preg_match("/#$property_name$/", $property_uri)) { $range = $onto->get_property_range($property_uri); break; } } // Si on n'a pas le type on le définit en Literal par défaut if (empty($default_value[0]['type'])) { // Ce n'est pas un tableau numériques, on ne sait pas quoi en faire... $default_value[0]['type'] = 'http://www.w3.org/2000/01/rdf-schema#Literal'; } // Le type ne correspond pas aux données dans le store ... if (!empty($range) && !empty($range[0]) && ($range[0] != $default_value[0]['type'])) { // On récupère le type qui correspond à la propriété $default_value[0]['type'] = $range[0]; } if (!empty($default_value[0]["assertions"]["author_qualification"]["elements"])){ $default_value[0]["assertions"]["author_qualification"] = stripslashes( json_encode( htmlspecialchars_array( $default_value[0]["assertions"]["author_qualification"]))); } else { $default_value[0]["assertions"]["author_qualification"] = ''; } for($j = 0; $j < count($default_value); $j++) { if ($default_value[$j]['type'] == "merge_properties") { global ${$default_value[$j]['value']}; $sub_properties = array(); foreach(${$default_value[$j]['value']} as $key => $value) { global ${$value}; if (isset(${$value}) && ${$value}) { $sub_properties[$key] = ${$value}; } } $default_value[$j]['value'] = $sub_properties; } } $properties_list[$property_name]['default_value'] = $default_value; } } } $this->parameters = encoding_normalize::json_encode($properties_list); $this->name = stripslashes($form_name); $this->comment = stripslashes($form_comment); } public function save($ajax_mode = false) { global $thesaurus_ontology_filemtime, $pmb_authors_qualification; $query = 'insert into '; $where = ''; if (!empty($this->id)) { $query = 'update '; $where = ' where id_form = "'.$this->id.'"'; } $query .= 'contribution_area_forms set '; $query .= 'form_title="'.addslashes($this->name).'",'; $query .= 'form_comment="'.addslashes($this->comment).'",'; $query .= 'form_parameters="'.addslashes($this->parameters).'",'; $query .= 'form_type="'.addslashes($this->type).'"'; pmb_mysql_query($query.$where); if (empty($this->id)) { $this->id = pmb_mysql_insert_id(); } $tab_file_rdf = unserialize($thesaurus_ontology_filemtime); $tab_file_rdf['onto_contribution_form_' . $this->id] = 0; $query = 'UPDATE parametres SET valeur_param="'.addslashes(serialize($tab_file_rdf)).'" WHERE type_param="thesaurus" AND sstype_param="ontology_filemtime"'; pmb_mysql_query($query); if (empty($ajax_mode)) { return true; } $form = array( 'type' => "form", 'form_id' => $this->id, 'parent_type' => $this->type, 'name' => $this->name, 'comment' => $this->comment, ); return $form; } public static function save_parameters($id_contribution, $parameters) { pmb_mysql_query("UPDATE contribution_area_forms SET form_parameters = '" . addslashes(encoding_normalize::json_encode($parameters)) . "' WHERE id_form = '$id_contribution'"); } protected function get_saved_property($property,$sub_property = ''){ if ($sub_property) { if(isset($this->unserialized_parameters->$property->$sub_property)){ return $this->unserialized_parameters->$property->$sub_property; } } else { if(isset($this->unserialized_parameters->$property)){ return $this->unserialized_parameters->$property; } } return ''; } public function delete($ajax_mode = false){ global $thesaurus_ontology_filemtime; /** * TODO: Vérification de l'utilisation dans les scénarios. */ $success = false; $query = 'delete from contribution_area_forms where id_form = "'.$this->id.'"'; $result = pmb_mysql_query($query); $tab_file_rdf = unserialize($thesaurus_ontology_filemtime); unset($tab_file_rdf['onto_contribution_form_' . $this->id]); $query='UPDATE parametres SET valeur_param="'.addslashes(serialize($tab_file_rdf)).'" WHERE type_param="thesaurus" AND sstype_param="ontology_filemtime"'; pmb_mysql_query($query); if($result){ $success = true; } if($ajax_mode){ return (array('form_id'=> $this->id, 'success'=>$success)); } return $success; } /** * Fonction permettant d'émuler une partie du framework * @param array $params tableau des paramètres sérialisés du formulaire * @param string $property_uri uri de la propriété que l'on veux valoriser * @return array Soit un array contenant des instance de datatype, soit un array vide */ protected function recompose_data($property_name, $property_uri, $datatype_class_name){ $values = array(); if(isset($this->unserialized_parameters->$property_name)) { foreach($this->unserialized_parameters->$property_name as $key => $value){ if($key == "default_value" && is_array($value)){ $value_properties = array(); foreach($value as $val){ if (!empty($val->lang)) { $value_properties["lang"] = $val->lang; } if (!empty($val->display_label)) { $value_properties["display_label"] = stripslashes(encoding_normalize::utf8_decode($val->display_label)); } if (empty($val->type)) { $val->type = "http://www.w3.org/2000/01/rdf-schema#Literal"; } if (!empty($val->assertions)) { $value_properties["assertions"] = array(); foreach($val->assertions as $prop => $value) { $value_properties["assertions"][] = new onto_assertion($val->value, $prop, $value); } } if ($datatype_class_name == 'onto_contribution_datatype_merge_properties') { $onto = contribution_area::get_ontology(); $merge_properties = $onto->get_property($this->uri, $property_uri); foreach($onto->get_class_properties($merge_properties->range[0]) as $uri_sub_property){ $property = $onto->get_property($merge_properties->range[0], $uri_sub_property); $sub_datatype_class_name = onto_common_item::search_datatype_class_name($property, $merge_properties->pmb_name,$merge_properties->onto_name); if (is_object($val->value)) { $sub_property = $val->value->{$property->pmb_name}[0]; $sub_value_properties = array(); if ($sub_property->display_label) { $sub_value_properties["display_label"] = stripslashes(encoding_normalize::utf8_decode($sub_property->display_label)); } $val->value->{$property->pmb_name} = array(); $val->value->{$property->pmb_name}[0] = new $sub_datatype_class_name ($sub_property->value, $sub_property->type, $sub_value_properties,''); } } } $values[] = new $datatype_class_name((isset($val->value) ? $val->value : null), $val->type, $value_properties,''); } } } } return $values; } /** * Méthode originaire de la classe opac permettant de constituer un tableau des propriétés actives. * @return multitype: */ public function get_active_properties() { if (isset($this->active_properties)) { return $this->active_properties; } $this->active_properties = array(); if ($this->unserialized_parameters) { foreach($this->unserialized_parameters as $key => $param){ $uri = $this->availableProperties[$key]->uri; $this->active_properties[$uri] = new stdClass(); $this->active_properties[$uri] = $this->unserialized_parameters->$key; $tab_default_value = $this->unserialized_parameters->$key->default_value; //on uniformise toutes les valeurs sous forme de tableau for ($j = 0; $j < count($tab_default_value); $j++) { if (!is_array($tab_default_value[$j]->value)) { $tab_default_value[$j]->value = array($tab_default_value[$j]->value); } } $this->active_properties[$uri]->default_value = $tab_default_value; } } return $this->active_properties; } public function get_uri(){ return $this->uri; } public function get_equations_from_type($datatype, $range) { $equations = array(); switch($datatype) { case "http://www.pmbservices.fr/ontology#resource_selector": case "http://www.pmbservices.fr/ontology#responsability_selector": if($range[0]) { $type = explode('#',$range[0])[1]; switch ($type) { case "linked_record": case "record": $type = "record"; break; case "responsability": $type = "author"; break; } $equations = contribution_area_equation::get_list_by_type($type); } } return $equations; } public function get_equations_selector ($equations, $name, $selected = 0) { global $msg; if (!count($equations)) { return ""; } $selector = "
".$msg['contribution_area_autocompletion_equation']."
"; $selector .= ""; return $selector; } public function render(){ global $base_path, $nb_per_page_gestion, $class_path; $onto_store_config = array( /* db */ 'db_name' => DATA_BASE, 'db_user' => USER_NAME, 'db_pwd' => USER_PASS, 'db_host' => SQL_SERVER, /* store */ 'store_name' => 'onto_contribution_form_' . $this->id, /* stop after 100 errors */ 'max_errors' => 100, 'store_strip_mb_comp_str' => 0, 'params' => $this->get_active_properties() ); $data_store_config = array( /* db */ 'db_name' => DATA_BASE, 'db_user' => USER_NAME, 'db_pwd' => USER_PASS, 'db_host' => SQL_SERVER, /* store */ 'store_name' => 'contribution_area_datastore', /* stop after 100 errors */ 'max_errors' => 100, 'store_strip_mb_comp_str' => 0 ); $tab_namespaces = array( "skos" => "http://www.w3.org/2004/02/skos/core#", "dc" => "http://purl.org/dc/elements/1.1", "dct" => "http://purl.org/dc/terms/", "owl" => "http://www.w3.org/2002/07/owl#", "rdf" => "http://www.w3.org/1999/02/22-rdf-syntax-ns#", "rdfs" => "http://www.w3.org/2000/01/rdf-schema#", "xsd" => "http://www.w3.org/2001/XMLSchema#", "pmb" => "http://www.pmbservices.fr/ontology#" ); $params = new onto_param(array( 'base_resource' => 'modelling.php', 'lvl' => 'contribution_area', 'sub' => 'form', 'type' => $this->type, 'action' => 'edit', 'page' => '1', 'nb_per_page' => $nb_per_page_gestion, 'id' => '0', 'area_id' => (isset($this->area_id) ? $this->area_id : 0), 'parent_id' => '', 'form_id' => (isset($this->id) ? $this->id : 0), 'form_uri' => (isset($this->form_uri) ? $this->form_uri : ''), 'item_uri' => '' )); $onto_store = new onto_store_arc2_extended($onto_store_config); $onto_store->set_namespaces($tab_namespaces); //chargement de l'ontologie dans son store $reset = $onto_store->load($class_path."/rdf/ontologies_pmb_entities.rdf", onto_parametres_perso::is_modified()); onto_parametres_perso::load_in_store($onto_store, $reset); $onto_ui = new onto_ui("", $onto_store, array(), "arc2", $data_store_config,$tab_namespaces,'http://www.w3.org/2000/01/rdf-schema#label',$params); return $onto_ui->proceed(); } public function get_name() { return $this->name; } public function get_comment() { return $this->comment; } /** * Retourne le script de redirection post sauvegarde ou suppression * @param number $area Identifiant de l'espace vers lequel faire une redirection */ public function get_redirection($area = 0) { global $base_path; $location = $base_path.'/modelling.php?categ=contribution_area&sub=form'; if ($area*1) { $location = $base_path.'/modelling.php?categ=contribution_area&sub=area&action=define&id='.$area; } return ' '; } public static function get_contribution_area_form($type, $id=0, $area_id = 0, $form_uri = '') { if (!isset(self::$contribution_area_form[$type])) { self::$contribution_area_form[$type] = array(); } $key = ''; if (intval($id)) { $key = $id; $key.= ($area_id ? '_'.$area_id : ''); } if (!$key) { return new contribution_area_form($type, $id, $area_id, $form_uri); } if (!isset(self::$contribution_area_form[$type][$key])) { self::$contribution_area_form[$type][$key] = new contribution_area_form($type, $id, $area_id, $form_uri); } return self::$contribution_area_form[$type][$key]; } public function sort_properties($a, $b){ $asavedprop = $this->get_saved_property($a->pmb_name); $bsavedprop = $this->get_saved_property($b->pmb_name); $restriction = $this->onto_class->get_restriction($a->uri); $amin = $restriction->get_min(); $restriction = $this->onto_class->get_restriction($b->uri); $bmin = $restriction->get_min(); if(($asavedprop || ($amin > 0)) && !($bsavedprop || ($bmin > 0))){ return -1; } if(!($asavedprop || ($amin > 0)) && ($bsavedprop || ($bmin > 0))){ return 1; } if(strtolower(convert_diacrit($a->label)) < strtolower(convert_diacrit($b->label))) { return -1; } return 1; } public function get_onto_class() { if (isset($this->onto_class)) { return $this->onto_class; } $ontology = contribution_area::get_ontology(); $this->onto_class = $ontology->get_class($this->uri); return $this->onto_class; } public function get_linked_forms () { if (isset($this->linked_forms)) { return $this->linked_forms; } $contribution_area_store = new contribution_area_store(); $complete_form_uri = $contribution_area_store->get_uri_from_id($this->form_uri); $graph_store_datas = $contribution_area_store->get_attachment_detail($complete_form_uri, 'http://www.pmbservices.fr/ca/Area#'.$this->area_id,'','',1); $this->linked_forms = array(); for ($i = 0 ; $i < count($graph_store_datas); $i++) { if ($graph_store_datas[$i]['type'] == "form") { $graph_store_datas[$i]['area_id'] = $this->area_id; $this->linked_forms[] = $graph_store_datas[$i]; } else { $data_form = $contribution_area_store->get_attachment_detail($graph_store_datas[$i]['uri'], 'http://www.pmbservices.fr/ca/Area#'.$this->area_id,'','',1); for ($j = 0 ; $j < count($data_form); $j++) { if ($data_form[$j]['type'] == "form") { $data_form[$j]['area_id'] = $this->area_id; $data_form[$j]['propertyPmbName'] = $graph_store_datas[$i]['propertyPmbName']; if ($graph_store_datas[$i]['type'] == "scenario") { $data_form[$j]['scenarioUri'] = $graph_store_datas[$i]['uri']; } $this->linked_forms[] = $data_form[$j]; } } } } return $this->linked_forms; } /** * Renvoie le formulaire de duplication * @param int $area * @return string */ public function get_duplication_form($area = 0) { // On vide l'id, le nom et on affiche le formulaire. $this->id = 0; $this->name = ''; $this->comment = ''; return $this->get_form($area); } /** * Créer le formulaire de duplication automatiquement * @param int $area * @return string */ public function generate_duplication_form($ajax_mode = false) { global $msg; $this->id = 0; $this->comment = ''; if (strpos($this->name, $msg['contribution_area_form_duplicated']) === false) { $new_name = ""; $new_name = $msg['contribution_area_form_duplicated'] . " " . $this->name ; $this->name = $new_name; } $matches = array(); if (preg_match('/\([1-9]+\)/i', $this->name, $matches)) { $name = explode(" (", $this->name); $query = 'SELECT count(*) FROM contribution_area_forms WHERE form_title like "%'.$name[0].'%"'; $result = pmb_mysql_query($query); $max = pmb_mysql_result($result,0,0); $this->name = $name[0]." ($max)"; }else{ $query = 'SELECT count(*) FROM contribution_area_forms WHERE form_title like "%'.$this->name.'%"'; $result = pmb_mysql_query($query); $max = pmb_mysql_result($result,0,0); if (!empty($max)) { $this->name .= " ($max)"; } } return encoding_normalize::json_encode($this->save($ajax_mode)); } /** * Renvoie l'identifiant * @return int */ public function get_id() { return $this->id; } public function get_scenario_linked() { $graphstore = contribution_area::get_graphstore(); $scenario_list = array(); $scenario_list["count"] = 0; $count = 0; $succes = $graphstore->query('select ?uri where { ?uri ca:eltId "'.$this->id.'" . }'); if ($succes) { $forms = $graphstore->get_result(); $contribution_area_list = contribution_area::get_list_ajax(); foreach ($forms as $form) { $succes = $graphstore->query('select ?uri ?name ?area where { ?attachment rdf:type ca:Attachment . ?attachment ca:attachmentSource ?uri . ?attachment ca:inArea ?area . ?attachment ca:attachmentDest <'.$form->uri.'> . ?uri rdf:label ?name . }'); if ($succes) { $scenarios = $graphstore->get_result(); foreach ($scenarios as $scenario) { $matches = array(); preg_match('/(\d+)$/', $scenario->area, $matches); if (!empty($contribution_area_list[$matches[0]])) { $scenario_list[$matches[0]][] = $scenario->name; $count ++; } } } else { var_dump("Errors : ".self::$graphstore->get_errors()); break; } } $scenario_list["count"] = $count; } else { var_dump("Errors : ".self::$graphstore->get_errors()); } return $scenario_list; } public static function has_draft_contribution_from_id($id){ $store = new contribution_area_store(); $dataStore = $store->get_datastore(); $success = $dataStore->query(" select * where { ?uri pmb:form_id '$id' . optional { ?uri pmb:identifier ?identifier . } } "); $results = array(); $has_draft = false; if ($success){ $results = $dataStore->get_result(); foreach ($results as $key => $result) { if (!empty($result->identifier)) { array_splice($results, $key, 1); } } } if (count($results) > 1) { $has_draft = true; } return $has_draft; } public static function has_draft_contribution_from_uri($uri){ $store = new contribution_area_store(); $dataStore = $store->get_datastore(); $success = $dataStore->query(" select ?uri where { ?uri pmb:form_uri '$uri' . optional { ?uri pmb:identifier ?identifier . } } "); $results = array(); $has_draft = false; if ($success){ $results = $dataStore->get_result(); foreach ($results as $key => $result) { if (!empty($result->identifier)) { array_splice($results, $key, 1); } } } if (count($results) > 1) { $has_draft = true; } return $has_draft; } public function get_type() { return $this->type; } }