wpdb = $wpdb; } public function getSampleForm($author) { $json = <<author = $author; $form->modified = time(); return $form; } public function createTable() { $table = $this->wpdb->prefix . self::TABLE; $charset_collate = $this->wpdb->get_charset_collate(); $sql = "CREATE TABLE $table (\n" . " id bigint(20) NOT NULL AUTO_INCREMENT, \n" . " title varchar(100) NOT NULL, \n" . " author bigint(20) NOT NULL, \n" . " modified int(11) NOT NULL, \n" . " content mediumtext NOT NULL, \n" . " dataver int(11) DEFAULT 1 NOT NULL, \n" . " PRIMARY KEY (id) \n" . ") ".$charset_collate.";"; require_once( ABSPATH . 'wp-admin/includes/upgrade.php' ); dbDelta($sql); } // Dropping-table moved to aforms.php protected function rowToObject($row) { $version = (int)$row['dataver']; if ($row['author_id']) { $author = new \stdClass(); $author->id = (int)$row['author_id']; $author->name = $row['author_name']; } else { $author = null; } $form = new \stdClass(); $form->id = (int)$row['id']; $form->title = $row['title']; $form->author = $author; $form->modified = (int)$row['modified']; $content = json_decode($row['content'], false); // migiration if (! property_exists($content, 'doConfirm')) { $content->doConfirm = true; } if (! property_exists($content, 'navigator')) { $content->navigator = 'horizontal'; } if (! property_exists($content, 'thanksUrl')) { $content->thanksUrl = ''; } foreach ($content->detailItems as $di) { if (($di->type == 'Selector' || $di->type == 'Auto') && ! property_exists($di, 'quantity')) { // default quantity is 1-fixed. $di->quantity = -1; } if ($di->type == 'Auto' && ! property_exists($di, 'taxRate')) { $di->taxRate = null; } if ($di->type == 'Auto' && ! property_exists($di, 'normalPrice')) { $di->normalPrice = null; } if ($di->type == 'Auto' && ! property_exists($di, 'priceAst')) { $di->priceAst = (float)$di->price; $di->priceVars = array(); $di->price = "" . $di->price; } if ($di->type == 'Quantity') { if (! property_exists($di, 'format')) { $di->format = 'none'; } } if ($di->type == 'Slider') { if (! property_exists($di, 'format')) { $di->format = 'none'; } } if ($di->type == 'AutoQuantity') { if (! property_exists($di, 'format')) { $di->format = 'none'; } if (! property_exists($di, 'depends')) { $di->depends = (object)array(); } if (! property_exists($di, 'suffix')) { $di->suffix = ''; } } if ($di->type == 'Selector') { foreach ($di->options as $option) { if (! property_exists($option, 'ribbons')) { $option->ribbons = (object)array(); } if (! property_exists($option, 'taxRate')) { $option->taxRate = null; } if ($version == 1 && ($option->normalPrice === '0' || $option->normalPrice === 0)) { $option->normalPrice = null; } if (! property_exists($option, 'format')) { $option->format = ($option->price === null || $option->price === "") ? 'none' : 'regular'; } if ($option->price === null || $option->price === "") { $option->price = 0; } if ($version == 1 && property_exists($option->ribbons, 'ribbon1')) { $option->ribbons->SALE = true; } if ($version == 1 && property_exists($option->ribbons, 'ribbon2')) { $option->ribbons->RECOMMENDED = true; } } } } foreach ($content->attrItems as $ai) { if ($ai->type == 'Name' && ! property_exists($ai, 'pattern')) { $ai->pattern = 'none'; } if (($ai->type == 'Radio' || $ai->type == 'Checkbox') && ! property_exists($ai, 'initialValue')) { $ai->initialValue = ''; } if ($ai->type == 'Address' && ! property_exists($ai, 'autoFill')) { $ai->autoFill = 'none'; } } if (! property_exists($content->mail, 'alignReturnPath')) { $content->mail->alignReturnPath = false; } if (! property_exists($content, 'extensions')) { $content->extensions = array(); } $form->navigator = $content->navigator; $form->doConfirm = $content->doConfirm; $form->thanksUrl = $content->thanksUrl; $form->detailItems = $content->detailItems; $form->attrItems = $content->attrItems; $form->mail = $content->mail; $form->extensions = $content->extensions; return $form; } public function findById($id) { $table = $this->wpdb->prefix . self::TABLE; $sql = "SELECT f.*, u.ID AS author_id, u.user_nicename AS author_name " . "FROM $table f LEFT JOIN ".$this->wpdb->users." u ON f.author = u.ID " . "WHERE f.id = %d"; $row = $this->wpdb->get_row($this->wpdb->prepare($sql, $id), ARRAY_A); if (! $row) { return null; } return $this->rowToObject($row); } public function count() { $table = $this->wpdb->prefix . self::TABLE; $sql = "SELECT COUNT(*) FROM $table"; return $this->wpdb->get_var($sql); } public function getList() { $table = $this->wpdb->prefix . self::TABLE; $sql = "SELECT f.*, u.ID AS author_id, u.user_nicename AS author_name " . "FROM $table f LEFT JOIN ".$this->wpdb->users." u ON f.author = u.ID " . "ORDER BY f.id DESC "; $rows = $this->wpdb->get_results($sql, ARRAY_A); $rv = array(); foreach ($rows as $row) { $rv[] = $this->rowToObject($row); } return $rv; } protected function objectToRow($form) { $data = array( 'title' => $form->title, 'author' => ($form->author) ? $form->author->id : null, 'modified' => $form->modified, 'content' => json_encode($form), 'dataver' => 2 ); $format = array('%s', '%d', '%d', '%s', '%d'); return array($data, $format); } public function add($form) { $table = $this->wpdb->prefix . self::TABLE; list($data, $format) = $this->objectToRow($form); $this->wpdb->insert($table, $data, $format); $form->id = $this->wpdb->insert_id; return $form; } public function sync($form) { $table = $this->wpdb->prefix . self::TABLE; list($data, $format) = $this->objectToRow($form); $this->wpdb->update($table, $data, array('id' => $form->id), $format, array('%d')); } public function remove($form) { $table = $this->wpdb->prefix . self::TABLE; $this->wpdb->delete($table, array('id' => $form->id), array('%d')); } }