PCR-360 Wiki

Compose - Code Assistant User Guide: Generating PHP Code, Templates, and CSS for PCR360 Custom Logic and Reports

Compose - Code Assistant User Guide

Overview

The Compose - Code Assistant is an AI-powered tool that generates PHP code, Smarty templates, and CSS for PCR360 Custom Logic and Custom Reports. It uses natural language descriptions to produce ready-to-use code that follows PCR360's coding conventions, whitelisted functions, and security best practices.

The Code Assistant is available on the following forms:

Form

Location

What It Generates

Custom API

Admin > Integration > Custom Logic > API

PHP logic for REST API endpoints

Custom Events

Admin > Integration > Custom Logic > Events

PHP logic for scheduled/triggered events

Custom Validation

Admin > Integration > Custom Logic > Validation

PHP validation rules for save/delete actions

Custom Reports

Admin > Integration > Custom Logic > Reports

PHP logic + Smarty template + CSS

Requirements: The Intelligence Compose license must be active. Without it, the Compose tab displays upgrade information.


Getting Started

Opening the Code Assistant

  1. Navigate to any Custom Logic or Custom Report form

  2. Click Add to create a new record, or Edit an existing one

  3. The Compose - Code Assistant tab appears as the first tab

Generating Code

  1. Click the Compose - Code Assistant tab

  2. Type a natural language description of what you need in the input area

  3. Press Enter or click the Send button

  4. Wait for the AI to generate code (a timer and status message show progress)

  5. The generated code appears in the chat with an explanation

Applying Code

For Custom API, Events, and Validation:

  1. After code is generated, click Apply Code in the toolbar

  2. The code is inserted into the Logic tab's editor

  3. The form's Identifier and Description fields are auto-populated if empty

  4. Switch to the Logic tab and click Validate PHP to verify the code

For Custom Reports:

Report generation is automatic. When you submit a description, the assistant generates all three parts in sequence:

  1. Step 1: Generates PHP Logic and applies it to the Logic tab

  2. Step 2: Generates a Smarty/Bootstrap HTML template and applies it to the Template tab

  3. Step 3: Generates CSS and applies it to the Options tab (CSS field)

A status bar shows which step is currently running.

The Identifier, Name, and Description fields are auto-populated from the AI response.


Features

Conversation History

The Code Assistant maintains a conversation history. You can ask follow-up questions to refine the generated code:

  • "Also add a check for empty email addresses"

  • "Use a LEFT JOIN instead of INNER JOIN"

  • "Add a date range filter"

Each follow-up request is aware of what was previously generated.

Include Current Code

Check the Include current code (or Include current logic for reports) checkbox to send the existing editor content as context. This is useful when you want to:

  • Modify existing code

  • Add a feature to code you've already written

  • Ask the AI to fix or improve existing logic

Start Over

Click Start Over to clear the conversation history and begin fresh. This does not clear any code already applied to the editors.

Report Parameters

When generating Custom Report logic, the AI may identify parameters that your report needs (e.g., date ranges, filters). These are displayed in a Report Parameters table in the chat response, showing:

Column

Description

Name

Parameter name to create in the Parameters tab

Type

Parameter type (text, number, date, picker, etc.)

Description

What the parameter is for

Required

Whether the parameter is mandatory

You must manually create these parameters in the Parameters tab. The table serves as a reference for what to configure.

Tips and Suggestions

Each response includes contextual tips, such as:

  • Which table and action to configure for validations

  • Reminders to use bound parameters for SQL injection prevention

  • Instructions to use the Validate PHP button before saving


Examples by Code Type

Custom API

Prompt: "Look up a contact by email and return their details"

The AI generates PHP code that:

  • Reads the email from $request

  • Queries the CONTACTS table with bound parameters

  • Returns the contact data in $response

Prompt: "Accept a service order number and create a labor entry for 2 hours"

Custom Events

Prompt: "Find service orders overdue by more than 7 days and log them"

The AI generates PHP code that:

  • Queries SERVICE_DESK for overdue orders

  • Iterates results and calls $this->debug() for each

  • Uses bound parameters and date functions

Prompt: "Nightly sync of inactive users — deactivate contacts not logged in for 90 days"

Custom Validation

Prompt: "Prevent duplicate contacts by email address"

The AI generates PHP code that:

  • Queries CONTACTS for existing records with the same email

  • Calls $this->setMessage() with an error if a duplicate is found

  • Returns false to block the save, or true to allow it

Prompt: "Require an owner before completing a service order"

Custom Reports

Prompt: "Monthly service summary by location with a table and chart"

The AI generates three pieces:

  1. PHP Logic: Queries the database for service counts and costs by location, assigns data to the Smarty template

  2. Smarty Template: Bootstrap 5.1 table with service data, plus a chart canvas

  3. CSS: Styling for the table, chart card, and print layout


Tips for Better Results

  1. Be specific — "Prevent duplicate charge catalog entries by name and type on the CHRG_CATALOG table" produces better code than "check for duplicates"

  2. Mention the table name — For validations, include the table: "On the SERVICES table, require a location before saving"

  3. Describe the output — For reports, describe what you want to see: "A table showing location name, service count, and total cost, grouped by department"

  4. Iterate — Start with a basic request, then refine: "Now add pagination" or "Sort by date descending"

  5. Always validate — Click Validate PHP in the Logic tab before saving. The AI uses only whitelisted functions, but validation catches any edge cases

  6. Check parameters — For reports, create the parameters listed in the Parameters table before running the report


Available Functions

The Code Assistant generates code using only PCR360's whitelisted PHP functions. Key functions available:

Function

Description

$this->query($sql, $bind)

Execute SQL with bound parameters

$this->assign($name, $value)

Assign variable to Smarty template (reports)

$this->setMessage($msg)

Set validation failure message

$this->debug($data)

Write debug output

$this->curlRequest($url, $opts)

Make HTTP requests

$this->sendSystemMessage($params)

Send system messages

$this->callCustomEvent($id, $data)

Trigger another custom event

$this->listGetByCode($type, $code)

Look up list values

$this->createFile($path, $content)

Write files (INBOUND/OUTBOUND only)

$this->addError($msg)

Add report error

$this->call($method, $params)

Call API shortcuts

For the complete function whitelist, see:


Scope Variables

Each code type has different variables available:

Code Type

Available Variables

Custom API

$request (POST data), $response (return data)

Custom Event

$data, $user, $params, $backtrace

Custom Validation

$data (record), $params, $user, $backtrace

Custom Report

$params (report parameters), $user


Troubleshooting

"Prohibited function or variable" error on Validate PHP

The AI occasionally generates code using functions not on the whitelist. If this happens:

  • Click back to the Compose tab

  • Ask: "Rewrite this without using [function name], use only whitelisted PCR360 functions"

  • Apply the updated code

Code doesn't match my database schema

The AI uses generic table/column names. Review the generated SQL and update table and column names to match your environment.

Report template doesn't display data

Ensure the variable names in $this->assign() calls match the {$variable} references in the Smarty template. Check the Debug tab for execution output.

Parameters not working

Create the parameters listed in the Report Parameters table in the Parameters tab of the report form. Parameter names must match exactly (case-sensitive).