array($lang['property-openai_gpt_prompt'],'',2,0), 'openai_gpt_input_field' => array($lang['property-openai_gpt_input_field'],'',0,0), ]; return array_merge($fieldcolumns,$addcolumns); } return false; } /** * Alter rendering of the new columns on the metadata field editing page * * @param int $ref Ref of the metadata field being edited * @param string $column Name of table column for which input is being rendered * @param array $column_detail Array of metadata field rendering data from the edit page * @param array $fielddata Array of metadata field information from get_resource_type_field() * * @return bool Is standard display rendering being overridden? * */ function HookOpenai_gptAdmin_resource_type_field_editAdmin_field_replace_question($ref,$column,$column_detail,$fielddata) { global $lang; if(!in_array($column,["openai_gpt_input_field","openai_gpt_prompt"])) { return false; } $currentvalue = $fielddata[$column]; if($column=="openai_gpt_input_field") { $fields = get_resource_type_fields(); ?>
0) { $source_values = $newvalues; } elseif (count($newnodes) > 0) { get_nodes_by_refs($newnodes); $source_values = array_column($newnodes,"name"); } else { $source_values[] = $value; } // Use this field's value to update the dependent field if (in_array($targetfield["type"],$valid_ai_field_types) && count($source_values) > 0) { openai_gpt_update_field($resource, $targetfield, $source_values); } } return false; } /** * Hook into save_resource_data() and save_resource_data_multi() to process value changes * * @param int|array $r Resource ID or array of resource IDs * @param mixed $all_nodes_to_add Passed from hook, unused * @param mixed $all_nodes_to_remove Passed from hook, unused * @param mixed $autosave_field Passed from hook, unused * @param mixed $fields Array of edited field data * @param mixed $updated_resources Array of resources & fields that have been updated * with resources as the top level key and field IDs as subkeys * * @return bool * */ function HookOpenai_gptAllAftersaveresourcedata($r, $all_nodes_to_add, $all_nodes_to_remove,$autosave_field, $fields,$updated_resources) { if(!(is_int_loose($r) || is_array($r))) { return false; } $refs = (is_array($r) ? $r : [$r]); debug("openai_gpt Aftersaveresourcedata - resources to update: " . implode(",",$refs)); $success=false; // Check if any configured fields have been edited foreach($fields as $field) { $targetfields = openai_gpt_get_dependent_fields($field["ref"]); foreach($targetfields as $targetfield) { debug("openai_gpt aftersaveresourcedata - processing field #" . $targetfield["ref"] . " (" . $targetfield["title"] . ")"); foreach($refs as $ref) { // Has the value been updated? if(isset($updated_resources[$ref][$field["ref"]]) && count($updated_resources[$ref][$field["ref"]]) > 0) { if(count($updated_resources[$ref][$field["ref"]]) == 1 && trim($updated_resources[$ref][$field["ref"]][0]) == "") { // Empty value - clear the target field debug("openai_gpt - no value set for resource # " . $ref . ", field #" . $field["ref"] . " " . $field["name"] . ", clearing target field #" . $targetfield["ref"]); $updated = update_field($ref,$targetfield["ref"],""); } else { $updated = openai_gpt_update_field($ref,$targetfield,$updated_resources[$ref][$field["ref"]]); } if($updated) { $success=true; } } } } } return $success; } /** * Hook into image upload to process the image as GPT input * * * @return bool Success if field is updated * */ function HookOpenai_gptAllAfterpreviewcreation($ref, $alternative, $generateall = false): bool { debug("openai_gpt after preview creation - resource: " . $ref . ", alternative: ", $alternative); if ($alternative>0) { return false; } $fields = get_resource_field_data($ref, false); $fields_with_values = array_filter($fields, function ($f) { return isset($f['value']) && $f['value'] != '';}); $fields_with_values = array_column($fields_with_values, 'ref'); // Do any fields use image as input? $ai_gpt_image_fields = ps_query("SELECT " . columns_in("resource_type_field") . " FROM resource_type_field WHERE openai_gpt_input_field = -1"); foreach($ai_gpt_image_fields as $ai_gpt_image_field) { // Don't update if not a valid field type if(!in_array($ai_gpt_image_field["type"],$GLOBALS["valid_ai_field_types"])) { continue; } // Get the preview file $file=get_resource_path($ref,true,"pre"); if (!file_exists($file)) { return false; } // Only update this field if it is empty. if (in_array($ai_gpt_image_field['ref'], $fields_with_values)) { continue; } $success = openai_gpt_update_field($ref,$ai_gpt_image_field, [],$file); } return $success[$ref] ?? false; }