<?php

namespace GriffinForms\Admin\Sql;

class Single extends \GriffinForms\Admin\Sql\Format
{
    public function attachFormPage($form_id, $page_id)
    {
        $this->table = $this->config->getTable('form');
        $form = $this->itemValues($form_id, 'form');
        $formpages = $this->maybeDecode($form->formpages);
        
        if (empty($formpages)) {
            $formpages = array($page_id);
        } else {
            $formpages[] = $page_id;
        }
        
        $data = array('formpages' => maybe_serialize($formpages));
        $where = array('id' => $form_id);
        $update = $this->updateData($data, $where);
    }    
    
    public function attachPageRow($page_id, $row_id)
    {
        $this->table = $this->config->getTable('formpage');
        $page = $this->itemValues($page_id, 'formpage');
        $rows = $this->maybeDecode($page->pagerows);
        
        if (empty($rows)) {
            $rows = array($row_id);
        } else {
            $rows[] = $row_id;
        }
        
        $data = array('pagerows' => maybe_serialize($rows));
        $where = array('id' => $page_id);
        $update = $this->updateData($data, $where);
    }    
    
    public function attachRowColumn($row_id, $column_id)
    {
        $this->table = $this->config->getTable('pagerows');
        $row = $this->itemValues($row_id, 'pagerow');
        $columns = $this->maybeDecode($row->rowcolumns);
        
        if (empty($columns)) {
            $columns = array($column_id);
        } else {
            $columns[] = $column_id;
        }
        
        $data = array('rowcolumns' => maybe_serialize($columns));
        $where = array('id' => $row_id);
        $update = $this->updateData($data, $where);
    }
}