/**
  * Adds default content to a page or DataObject
  *
  * @todo Migrate this to a decorator. DataObjects should know how to seed themeselves.
  * @param DataObject The object to modify
  * @param integer The level to which to seed content (see "silversmith help")
  * @param array Limit the seeding to certain fields
  */
 public static function add_default_content(&$object, $level, $onlyFields = array())
 {
     if ($level < 2) {
         return;
     }
     $fields = array();
     if (!empty($onlyFields)) {
         foreach ($onlyFields as $f) {
             $fields[] = trim($f);
         }
     }
     $site_tree = singleton('SiteTree');
     $data_object = singleton('DataObject');
     $is_sitetree = $object->class == "SiteTree" || is_subclass_of($object, "SiteTree");
     foreach ($object->db() as $field => $type) {
         if ($data_object->db($field) || $is_sitetree && $site_tree->db($field) || !empty($fields) && !in_array($field, $fields)) {
             continue;
         }
         if (!$object->{$field}) {
             if (strstr($field, "Email")) {
                 $object->{$field} = preg_replace('/[^a-z@\\.]/', '', strtolower(self::get_lipsum_words(1) . "@" . self::get_lipsum_words(1) . ".com"));
             } elseif (strstr($field, "Phone")) {
                 $object->{$field} = rand(100, 999) . "-" . rand(100, 999) . "-" . rand(100, 999);
             } else {
                 $object->{$field} = self::get_default_content($object->obj($field));
             }
         }
     }
     foreach ($object->has_one() as $relation => $class) {
         if ($data_object->has_one($relation) || $site_tree->has_one($relation) || !empty($fields) && !in_array($relation, $fields)) {
             continue;
         }
         $filter = $class == "File" ? "ClassName = 'File'" : null;
         $o = DataList::create($class)->where($filter)->sort("RAND()")->first();
         if ($o) {
             $key = $relation . "ID";
             $object->{$key} = $o->ID;
         }
     }
     if ($level > 2) {
         foreach ($object->has_many() as $relation => $class) {
             if ($data_object->has_many($relation) || $site_tree->has_many($relation) || !SilverSmithProject::get_node($class) || !empty($fields) && !in_array($relation, $fields)) {
                 continue;
             }
             if ($name = array_search($object->class, singleton($class)->has_one())) {
                 $key = $name . "ID";
                 $count = rand(1, 5);
                 for ($i = 0; $i <= $count; $i++) {
                     if ($candidate = DataList::create($class)->where("{$key} = 0 OR {$key} IS NULL")->first()) {
                         $candidate->{$key} = $object->ID;
                         $candidate->write();
                     } elseif (!is_subclass_of($class, "SiteTree") && !is_subclass_of($class, "File")) {
                         $related = new $class();
                         $related->{$key} = $object->ID;
                         $related->write();
                         self::add_default_content($related, $level);
                         $related->write();
                     } else {
                         // We don't create a site tree object for the has many, because it will mess up the hierarchy.
                     }
                 }
             }
         }
         foreach ((array) $object->stat('many_many') as $relation => $class) {
             if ($class == $object->class || is_subclass_of($class, $object->class)) {
                 continue;
             }
             if ($data_object->many_many($relation) || $site_tree->many_many($relation) || !SilverSmithProject::get_node($class) || !empty($fields) && !in_array($relation, $fields)) {
                 continue;
             }
             $table = $object->class . "_" . $relation;
             $parentKey = $object->class . "ID";
             $childKey = $class . "ID";
             $set = DataList::create($class)->sort("RAND()")->limit(5);
             if (!$set) {
                 $set = new DataList();
             }
             // never create sitetree or file objects.
             if (!is_subclass_of($class, "SiteTree") && !is_subclass_of($class, "File") && $class != $object->class) {
                 $count = $set->Count();
                 while ($count < 5) {
                     $related = new $class();
                     $related->write();
                     self::add_default_content($related, $level);
                     $related->write();
                     $count++;
                 }
             }
             $set = DataList::create($class)->sort("RAND()")->limit(rand(1, 5));
             if ($set) {
                 $object->{$relation}()->setByIDList($set->column('ID'));
             }
         }
     }
 }