public function testField()
 {
     $f = new Field('test', 1);
     $this->assertEquals('test', $f->getName());
     $this->assertEquals(1, $f->getTable());
     $f = new AllFields();
     $this->assertEquals(0, $f->getTable());
     $f = new AllFields(2);
     $this->assertEquals(2, $f->getTable());
     try {
         $f = new Field(array('1'));
         $this->fail('first parameter should be string (array is not allowed)');
     } catch (InvalidArgumentException $e) {
     }
     try {
         $f = new Field('foo', 'bar');
         $this->fail('second parameter has to be numeric');
     } catch (InvalidArgumentException $e) {
     }
 }
Example #2
0
 /**
  * Check that the given Field's table exists and create it if it doesn't
  *
  * @param Field $field
  *
  */
 protected function checkFieldTable(Field $field)
 {
     // if(!$this->wire('config')->debug) return;
     $database = $this->wire('database');
     $table = $database->escapeTable($field->getTable());
     if (empty($table)) {
         return;
     }
     $exists = $database->query("SHOW TABLES LIKE '{$table}'")->rowCount() > 0;
     if ($exists) {
         return;
     }
     try {
         if ($field->type && count($field->type->getDatabaseSchema($field))) {
             if ($field->type->createField($field)) {
                 $this->message("Created table '{$table}'");
             }
         }
     } catch (Exception $e) {
         $this->error($e->getMessage());
     }
 }
 /**
  * Delete the given option IDs
  *
  * @param Field $field
  * @param array $ids
  * @return int Number of options deleted
  *
  */
 public function deleteOptionsByID(Field $field, array $ids)
 {
     $database = $this->wire('database');
     $table = $database->escapeTable($field->getTable());
     $cleanIDs = array();
     foreach ($ids as $key => $id) {
         $cleanIDs[] = (int) $id;
     }
     // convert to SQL ready string
     $cleanIDs = implode(',', $cleanIDs);
     // delete from field_[fieldName] table
     $sql = "DELETE FROM `{$table}` WHERE data IN({$cleanIDs})";
     $query = $database->prepare($sql);
     $query->execute();
     $cnt = $query->rowCount();
     $this->message("Deleted {$cnt} rows from table {$table}", Notice::debug);
     // delete from fieldtype_options table
     $table = self::optionsTable;
     $sql = "DELETE FROM `{$table}` WHERE fields_id=:fields_id AND option_id IN({$cleanIDs})";
     $query = $database->prepare($sql);
     $query->bindValue(':fields_id', $field->id);
     $query->execute();
     $cnt = $query->rowCount();
     $this->message("Deleted {$cnt} rows from table {$table}", Notice::debug);
     $this->message(sprintf($this->_n('Deleted %d option', 'Deleted %d options', $cnt), $cnt));
     return $cnt;
 }
 /**
  * Return a count of database rows populated the given field
  *
  * @param Field $field
  * @param array $options Optionally specify any of the following options:
  * 	template: Specify a Template object, ID or name to isolate returned rows specific to pages using that template. 
  * 	page: Specify a Page object, ID or path to isolate returned rows specific to that page. 
  * 	countPages: Specify boolean true to make it return a page count rather than a row count (default=false). 
  * 		There will only potential difference between rows and pages counts with multi-value fields. 
  * 	getPageIDs: Specify boolean true to make it return an array of matching Page IDs rather than a count (overrides countPages).
  * @return int|array Returns array only if getPageIDs option set. 
  * @throws WireException If given option for page or template doesn't resolve to actual page/template.
  *
  */
 public function getNumRows(Field $field, array $options = array())
 {
     $defaults = array('template' => 0, 'page' => 0, 'countPages' => false, 'getPageIDs' => false);
     $options = array_merge($defaults, $options);
     $database = $this->wire('database');
     $table = $database->escapeTable($field->getTable());
     $useRowCount = false;
     $schema = $field->type->getDatabaseSchema($field);
     if (empty($schema)) {
         // field has no schema or table (example: FieldtypeConcat)
         if ($options['getPageIDs']) {
             return array();
         }
         return 0;
     }
     if ($options['template']) {
         // count by pages using specific template
         if ($options['template'] instanceof Template) {
             $template = $options['template'];
         } else {
             $template = $this->wire('templates')->get($options['template']);
         }
         if (!$template) {
             throw new WireException("Unknown template: {$options['template']}");
         }
         if ($options['getPageIDs']) {
             $sql = "SELECT DISTINCT {$table}.pages_id FROM {$table} " . "JOIN pages ON pages.templates_id=:templateID AND pages.id=pages_id ";
         } else {
             if ($options['countPages']) {
                 $sql = "SELECT COUNT(DISTINCT pages_id) FROM {$table} " . "JOIN pages ON pages.templates_id=:templateID AND pages.id=pages_id ";
             } else {
                 $sql = "SELECT COUNT(*) FROM pages " . "JOIN {$table} ON {$table}.pages_id=pages.id " . "WHERE pages.templates_id=:templateID ";
             }
         }
         $query = $database->prepare($sql);
         $query->bindValue(':templateID', $template->id, PDO::PARAM_INT);
     } else {
         if ($options['page']) {
             // count by specific page
             if (is_int($options['page'])) {
                 $pageID = $options['page'];
             } else {
                 $page = $this->wire('pages')->get($options['page']);
                 $pageID = $page->id;
             }
             if (!$pageID) {
                 throw new WireException("Unknown page: {$options['page']}");
             }
             if ($options['countPages']) {
                 // is there any the point to  this?
                 $sql = "SELECT COUNT(DISTINCT pages_id) FROM {$table} WHERE pages_id=:pageID ";
             } else {
                 $sql = "SELECT COUNT(*) FROM {$table} WHERE pages_id=:pageID ";
             }
             $query = $database->prepare($sql);
             $query->bindValue(':pageID', $pageID, PDO::PARAM_INT);
         } else {
             // overall total count
             if ($options['getPageIDs']) {
                 $sql = "SELECT DISTINCT {$table}.pages_id FROM {$table}";
             } else {
                 if ($options['countPages']) {
                     $sql = "SELECT COUNT(DISTINCT pages_id) FROM {$table}";
                 } else {
                     $sql = "SELECT COUNT(*) FROM {$table}";
                 }
             }
             $query = $database->prepare($sql);
         }
     }
     $return = $options['getPageIDs'] ? array() : 0;
     try {
         $query->execute();
         if ($options['getPageIDs']) {
             while ($row = $query->fetch(PDO::FETCH_NUM)) {
                 $return[] = (int) $row['id'];
             }
         } else {
             if ($useRowCount) {
                 $return = (int) $query->rowCount();
             } else {
                 list($return) = $query->fetch(PDO::FETCH_NUM);
                 $return = (int) $return;
             }
         }
     } catch (Exception $e) {
         $this->error($e->getMessage() . " (getNumRows)");
     }
     return $return;
 }