# Content Script Builder (PHP 5.6)

This package adds a **new tool** named `content_script_builder.php` that lets you:
- Configure and **save settings per section** (stored in `data/csb_settings.json`).
- **Build content** (images, tables, sections, freeform prompt).
- **Compile** a standardized script that depends on the existing `/ai_helpers` (runtime model = `gpt-4.1-nano`).
- **Test** by upserting to **aberdeenshower.com** (`conn_id=35`) with slug **`test`** (custom page or content group).
- Show a **code box + Copy button** for the compiled script and allow file download.
- Optionally display a **build report** (cost/speed/meta) via lightweight helpers in `includes/csb_report.php`.

> Notes
>
> • This project keeps 100% **PHP 5.6** compatibility — classic `array()` syntax, no type hints, and uses `mysql_*` access patterns to match the legacy codebase.
> • The generated (compiled) script **gets its marching orders from `c_domain_build_queue`** and **creates a new row in `page`** using the LPv2 APIs (`customPagesUpsert` or `contentGroupsUpsert`), the same way your existing “full_auto_*” scripts do.
> • The **builder** itself is designed to run with your existing `config.php`, `db_router_legacy.php`, and `/ai_helpers` from your codebase. In this package we include a minimal fallback stub for AI helpers used only when `/ai_helpers/ai_content_helpers_loader.php` is missing so you can click around locally.
> • We copied your example scripts (see `examples_full_auto/`) untouched for reference.
>
> **Models**  
> - *Builder/compile step:* conceptually “gpt-5” (just a marker in headers).  
> - *Runtime in generated scripts + test section:* `gpt-4.1-nano` (forced via helper wrapper).

## Files
```
content_script_builder.php         # main UI/controller
includes/
  csb_bootstrap.php                # storage, helpers, JSON responses
  csb_sections.php                 # default sections + validation
  csb_compile.php                  # compiles a runnable content script string
  csb_test.php                     # test upsert to conn_id=35 (slug 'test')
  csb_report.php                   # tiny profiler/cost estimator
  ai_helpers_stub.php              # SAFE stubs used only if real ai_helpers are missing
assets/
  csb.css                          # minimal styles
  csb.js                           # copy-to-clipboard + AJAX helpers
compiled_scripts/
  sample_compiled_page.php         # an example compiled output (generated today)
data/
  csb_settings.json                # saved settings (created after first Save)
ai_helpers/                        # copied from your `functions_test.zip` (may be placeholders)
db_router_legacy.php               # copied from your `functions_test.zip`
functions_test.php                 # copied from your `functions_test.zip`
examples_full_auto/                # your supplied “full_auto_*” scripts for reference
```

## Install
1. Copy this whole folder into the web root that already contains your **real** `config.php`.
2. Ensure `data/` is writable by the web server.
3. Open `content_script_builder.php` in a browser.

## What to expect
- **Save settings** persists per-section values into `data/csb_settings.json`.
- **Compile** renders a full PHP script that:
  - requires your `config.php`, `db_router_legacy.php`, and `/ai_helpers/ai_content_helpers_loader.php`
  - uses **`gpt-4.1-nano`** at runtime
  - reads **`c_domain_build_queue`** (id or domain) for marching orders
  - builds meta + content via `ai_complete()` and upserts through LPv2 APIs
- **Test** sends an **upsert** payload to **aberdeenshower.com (conn_id=35)**, forcing slug **`test`** (custom page or content group).

## Important
- If `/ai_helpers` in this package looks truncated on your machine, swap in your **real** project `/ai_helpers` — the builder and compiled scripts are written to depend on it.
- This is a **foundation**; we kept the pieces modular so we can iterate quickly without touching one giant file.
