/** * Purge fake content from the database * TODO: Implement MultiRedirect support * TODO: This is horribly inefficient (rewrite this as an IN() query and manually construct data) * * @param array $values Generator form values */ protected function _purgeContent($values) { foreach ($values['content_types'] as $class) { $fakes = \IPS\faker\Faker::allFake($class); foreach ($fakes as $fake) { try { $obj = $class::load($fake->content_id); $obj->delete(); } catch (\OutOfRangeException $e) { } $fake->delete(); } } }
/** * Load the Comments extension for this Item * * @return mixed The extension if it exists, otherwise NULL */ protected function commentExt() { /* Return the extension if it has already been loaded */ if ($this->_commentExtension) { return $this->_commentExtension; } $extensions = \IPS\faker\Faker::allExtensions(\IPS\faker\Faker::COMMENTS); /* Do we have an explicitly defined app for the Comment extension? */ $app = static::$app; $commentExtension = static::$commentExtension; if (is_array($commentExtension)) { $app = $commentExtension[0]; $commentExtension = $commentExtension[1]; } /* Return the extension if it exists */ if (isset($extensions[$app . '_' . $commentExtension])) { return $this->_commentExtension = $extensions[$app . '_' . $commentExtension]; } return NULL; }
/** * Get request extension data * * @return array Extension object, app name, extension name */ protected function extData() { /* Return pre-generated extension data if we have it */ if ($this->extData) { return $this->extData; } /* Make sure our extension app and extension name have been defined */ if (!($extApp = \IPS\Request::i()->extApp) or !($extension = \IPS\Request::i()->extension)) { \IPS\Output::i()->error('generic_error', '3FAKE108/1', 400); return array(); } /* Try and fetch the requested extension or display a generic 404 error if we can't find it */ try { $extensions = \IPS\faker\Faker::allExtensions(constant('\\IPS\\faker\\Faker::' . mb_strtoupper(static::$controller))); $ext = $extensions[\IPS\Request::i()->extApp . '_' . \IPS\Request::i()->extension]; } catch (\Whoops\Exception\ErrorException $e) { \IPS\Output::i()->error('node_error', '2FAKE108/2', 404); return array(); } return $this->extData = array($ext, $extApp, $extension, static::$controller); }
/** * Dynamic AdminCP menu generator for extensions * * @return array */ public function acpMenu() { $extensions = \IPS\faker\Faker::allExtensions(); $menu = array(); foreach ($extensions as $key => $extension) { $splitKey = explode('_', $key); /* What app is this extension for? */ $app = property_exists($extension, 'app') ? $extension::$app : implode('_', array_slice($splitKey, 0, -1)); /* Make sure the application is enabled */ if (!\IPS\Application::appIsEnabled($app)) { continue; } /* Do we need to create a new category for this app? */ if (!isset($menu[$app])) { $menu[$app] = array(); } $extName = implode('_', array_slice($splitKey, 1)); $menu[$app][$extName] = array('tab' => 'faker', 'controller' => $extension::$_controller, 'do' => "manage&module=generator&extApp={$app}&extension={$extName}", 'restriction' => $extension::$acpRestriction); } /* Append any misc. modules we want to display here */ $origMenu = parent::acpMenu(); $menu['tools'] = $origMenu['tools']; return $menu; }
/** * Return a random fake member account (or guest account if none exist) * * @return \IPS\Member */ public function fakeMember() { if ($fakes = \IPS\faker\Faker::allFake('\\IPS\\Member')) { $fakeIds = array(); foreach ($fakes as $fake) { $fakeIds[$fake->content_id] = $fake; } /* If a fake member account has been deleted, remove the map and try again */ while ($fakeIds) { $fid = array_rand($fakeIds); try { return \IPS\Member::load($fid); } catch (\UnderflowException $e) { $fake = $fakeIds[$fid]; $fake->delete(); unset($fakeIds[$fid]); } } } return $this->guest(); }
/** * Create a map to the fake content item * * @param string $class Content class * @param int $contentId * @return void */ protected function map($class, $contentId) { \IPS\faker\Faker::createMap($class, $contentId); }