📄 resource_functions.php
字号:
<?# Resource functions# Functions to create, edit and index resourcesfunction create_resource($resource_type,$archive=-1,$user=-1) { # Create a new resource. if ($archive==-1) { # Work out an appropriate default state $archive=0; if (!checkperm("e0")) {$archive=2;} # Can't create a resource in normal state? create in archive. } if ($archive==-2) { # Work out user ref - note: only for content in status -2 (user submitted). global $userref; $user=$userref; } else {$user=-1;} sql_query("insert into resource(resource_type,creation_date,archive,created_by) values ('$resource_type',now(),'$archive','$user')"); $insert=sql_insert_id(); # Log this daily_stat("Create resource",$insert); resource_log($insert,'c',0); return $insert; } function save_resource_data($ref,$multi) { # Save all submitted data for resource $ref. # Also re-index all keywords from indexable fields. # Loop through the field data and save (if necessary) $errors=array(); $resource_sql=""; $fields=get_resource_field_data($ref,$multi); for ($n=0;$n<count($fields);$n++) { if ($fields[$n]["type"]==2) { # construct the value from the ticked boxes $val=","; # Note: it seems wrong to start with a comma, but this ensures it is treated as a comma separated list by split_keywords(), so if just one item is selected it still does individual word adding, so 'South Asia' is split to 'South Asia','South','Asia'. $options=trim_array(explode(",",$fields[$n]["options"]));sort($options); for ($m=0;$m<count($options);$m++) { $name=$fields[$n]["ref"] . "_" . $m; if (getval($name,"")=="yes") { if ($val!=",") {$val.=",";} $val.=$options[$m]; } } } elseif ($fields[$n]["type"]==4 || $fields[$n]["type"]==6) { # date type, construct the value from the date dropdowns $val=getvalescaped("field_" . $fields[$n]["ref"] . "-y",""); $val.="-" . getvalescaped("field_" . $fields[$n]["ref"] . "-m",""); $val.="-" . getvalescaped("field_" . $fields[$n]["ref"] . "-d",""); if (strlen($val)!=10) {$val="";} } else { $val=getvalescaped("field_" . $fields[$n]["ref"],""); } if ($fields[$n]["value"]!=$val) { # This value is different from the value we have on record. # Write this edit to the log. resource_log($ref,'e',$fields[$n]["ref"]); # If 'resource_column' is set, then we need to add this to a query to back-update # the related columns on the resource table if (strlen($fields[$n]["resource_column"])>0) { if ($resource_sql!="") {$resource_sql.=",";} $resource_sql.=$fields[$n]["resource_column"] . "='" . $val . "'"; } # Purge existing data and keyword mappings, decrease keyword hitcounts. sql_query("delete from resource_data where resource='$ref' and resource_type_field='" . $fields[$n]["ref"] . "'"); # Insert new data and keyword mappings, increase keyword hitcounts. sql_query("insert into resource_data(resource,resource_type_field,value) values('$ref','" . $fields[$n]["ref"] . "','" . str_replace((!(strpos($val,"\'")===false)?"\'":"'"),"''",$val) ."')"); $oldval=$fields[$n]["value"]; if ($fields[$n]["type"]==3) { # Prepend a comma when indexing dropdowns $val="," . getvalescaped("field_" . $fields[$n]["ref"],""); $oldval="," . $oldval; } if ($fields[$n]["keywords_index"]==1) { remove_keyword_mappings($ref,i18n_get_indexable($oldval),$fields[$n]["ref"]); add_keyword_mappings($ref,i18n_get_indexable($val),$fields[$n]["ref"]); } # update resources table if necessary if ($resource_sql!="") sql_query("update resource set $resource_sql where ref='$ref'"); } # Check required fields have been entered. if ($fields[$n]["required"]==1 && ($val=="" || $val==",")) { global $lang; $errors[$fields[$n]["ref"]]=$lang["requiredfield"]; } } # Also save related resources field sql_query("delete from resource_related where resource='$ref'"); # remove existing related items $related=explode(",",getvalescaped("related","")); # Make sure all submitted values are numeric $ok=array();for ($n=0;$n<count($related);$n++) {if (is_numeric(trim($related[$n]))) {$ok[]=trim($related[$n]);}} if (count($ok)>0) {sql_query("insert into resource_related(resource,related) values ($ref," . join("),(" . $ref . ",",$ok) . ")");} # Also update archive status and access level sql_query("update resource set archive='" . getvalescaped("archive",0) . "',access='" . getvalescaped("access",0) . "' where ref='$ref'"); # For access level 3 (custom) - also save custom permissions if (getvalescaped("access",0)==3) {save_resource_custom_access($ref);} if (count($errors)==0) {return true;} else {return $errors;} } function save_resource_data_multi($collection) { # Save all submitted data for collection $collection, this is for the 'edit multiple resources' feature # Loop through the field data and save (if necessary) $list=get_collection_resources($collection); $ref=$list[0]; $fields=get_resource_field_data($ref); for ($n=0;$n<count($fields);$n++) { if (getval("editthis_field_" . $fields[$n]["ref"],"")!="") { if ($fields[$n]["type"]==2) { # construct the value from the ticked boxes $val=","; # Note: it seems wrong to start with a comma, but this ensures it is treated as a comma separated list by split_keywords(), so if just one item is selected it still does individual word adding, so 'South Asia' is split to 'South Asia','South','Asia'. $options=trim_array(explode(",",$fields[$n]["options"]));sort($options); for ($m=0;$m<count($options);$m++) { $name=$fields[$n]["ref"] . "_" . $m; if (getval($name,"")=="yes") { if ($val!=",") {$val.=",";} $val.=$options[$m]; } } } elseif ($fields[$n]["type"]==4) { # date type, construct the value from the date dropdowns $val=getvalescaped("field_" . $fields[$n]["ref"] . "-y",""); $val.="-" . getvalescaped("field_" . $fields[$n]["ref"] . "-m",""); $val.="-" . getvalescaped("field_" . $fields[$n]["ref"] . "-d",""); if (strlen($val)!=10) {$val="";} } else { $val=getvalescaped("field_" . $fields[$n]["ref"],""); } $origval=$val; # Loop through all the resources and save. for ($m=0;$m<count($list);$m++) { $ref=$list[$m]; $resource_sql=""; # Work out existing field value. $existing=escape_check(sql_value("select value from resource_data where resource='$ref' and resource_type_field='" . $fields[$n]["ref"] . "'","")); # Find and replace mode? Perform the find and replace. if (getval("modeselect_" . $fields[$n]["ref"],"")=="FR") { $val=str_replace ( getvalescaped("find_" . $fields[$n]["ref"],""), getvalescaped("replace_" . $fields[$n]["ref"],""), $existing ); } # Append text mode? if (getval("modeselect_" . $fields[$n]["ref"],"")=="AP") { $val=$existing . " " . $origval; } #echo "<li>existing=$existing, new=$val"; if ($existing!=$val) { # This value is different from the value we have on record. # Write this edit to the log. resource_log($ref,'m',$fields[$n]["ref"]); # If 'resource_column' is set, then we need to add this to a query to back-update # the related columns on the resource table if (strlen($fields[$n]["resource_column"])>0) { sql_query("update resource set " . $fields[$n]["resource_column"] . "='$val' where ref='$ref'"); } # Purge existing data and keyword mappings, decrease keyword hitcounts. sql_query("delete from resource_data where resource='$ref' and resource_type_field='" . $fields[$n]["ref"] . "'"); # Insert new data and keyword mappings, increase keyword hitcounts. sql_query("insert into resource_data(resource,resource_type_field,value) values('$ref','" . $fields[$n]["ref"] . "','$val')"); $oldval=$existing; $newval=$val; if ($fields[$n]["type"]==3) { # Prepend a comma when indexing dropdowns $newval="," . $val; $oldval="," . $oldval; } if ($fields[$n]["keywords_index"]==1) { remove_keyword_mappings($ref,i18n_get_indexable($oldval),$fields[$n]["ref"]); add_keyword_mappings($ref,i18n_get_indexable($newval),$fields[$n]["ref"]); } } } } } # Also save related resources field if (getval("editthis_related","")!="") { $related=explode(",",getvalescaped("related","")); # Make sure all submitted values are numeric $ok=array();for ($n=0;$n<count($related);$n++) {if (is_numeric(trim($related[$n]))) {$ok[]=trim($related[$n]);}} for ($m=0;$m<count($list);$m++) { $ref=$list[$m]; sql_query("delete from resource_related where resource='$ref'"); # remove existing related items if (count($ok)>0) {sql_query("insert into resource_related(resource,related) values ($ref," . join("),(" . $ref . ",",$ok) . ")");} } } # Also update archive status if (getval("editthis_status","")!="") { for ($m=0;$m<count($list);$m++) { $ref=$list[$m]; sql_query("update resource set archive='" . getvalescaped("archive",0) . "' where ref='$ref'"); } } # Also update access level if (getval("editthis_access","")!="") { for ($m=0;$m<count($list);$m++) { $ref=$list[$m]; $access=getvalescaped("access",0); sql_query("update resource set access='$access' where ref='$ref'"); # For access level 3 (custom) - also save custom permissions if ($access==3) {save_resource_custom_access($ref);} } } }function remove_keyword_mappings($ref,$string,$resource_type_field) { # Removes one instance of each keyword->resource mapping for each occurrence of that # keyword in $string. # This is used to remove keyword mappings when a field has changed. # We also decrease the hit count for each keyword. if (trim($string)=="") {return false;} $keywords=split_keywords($string,true); for ($n=0;$n<count($keywords);$n++) { #echo "<li>removing " . $keywords[$n]; sql_query("delete from resource_keyword where resource='$ref' and keyword=(select ref from keyword where keyword='" . escape_check($keywords[$n]) . "') and resource_type_field='$resource_type_field' limit 1"); sql_query("update keyword set hit_count=hit_count-1 where keyword='" . escape_check($keywords[$n]) . "' limit 1"); } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -