/** * Loads a controller for either a page or view */ public function controller($item) { $include = false; if (is_string($item)) { $db = self::db(); if (is_object($db)) { try { $_item = Page::getByPath($item); if ($_item->isError()) { $path = $item; } else { $item = $_item; } } catch (Exception $e) { $path = $item; } } else { $path = $item; } } if ($item instanceof Page) { $c = $item; if ($c->getCollectionTypeID() > 0) { $ctHandle = $c->getCollectionTypeHandle(); $path = self::pageTypeControllerPath($ctHandle, $item->getPackageHandle()); if ($path != false) { require_once $path; $class = Object::camelcase($ctHandle) . 'PageTypeController'; } } else { if ($c->isGeneratedCollection()) { $file = $c->getCollectionFilename(); if ($file != '') { // strip off PHP suffix for the $path variable, which needs it gone if (strpos($file, '/' . FILENAME_COLLECTION_VIEW) !== false) { $path = substr($file, 0, strpos($file, '/' . FILENAME_COLLECTION_VIEW)); } else { $path = substr($file, 0, strpos($file, '.php')); } } } } } else { if ($item instanceof Block || $item instanceof BlockType) { $class = Object::camelcase($item->getBlockTypeHandle()) . 'BlockController'; if ($item instanceof BlockType) { $controller = new $class($item); } if ($item instanceof Block) { $c = $item->getBlockCollectionObject(); } } } $controllerFile = $path . '.php'; if ($path != '') { $env = Environment::get(); $pkgHandle = false; if (is_object($item)) { $pkgHandle = $item->getPackageHandle(); } $f1 = $env->getPath(DIRNAME_CONTROLLERS . $path . '/' . FILENAME_COLLECTION_CONTROLLER, $pkgHandle); $f2 = $env->getPath(DIRNAME_CONTROLLERS . $controllerFile, $pkgHandle); if (file_exists($f2)) { $include = true; require_once $f2; } else { if (file_exists($f1)) { $include = true; require_once $f1; } } if ($include) { $class = Object::camelcase($path) . 'Controller'; } } if (!isset($controller)) { if ($class && class_exists($class)) { // now we get just the filename for this guy, so we can extrapolate // what our controller is named $controller = new $class($item); } else { $controller = new Controller($item); } } if (isset($c) && is_object($c)) { $controller->setCollectionObject($c); } return $controller; }
/** * Loads a controller for either a page or view */ public function controller($item) { if (is_string($item)) { $db = Loader::db(); if (is_object($db)) { try { $_item = Page::getByPath($item); if ($_item->isError()) { $path = $item; } else { $item = $_item; } } catch (Exception $e) { $path = $item; } } else { $path = $item; } } if ($item instanceof Page) { $c = $item; if ($c->getCollectionTypeID() > 0) { $ctHandle = $c->getCollectionTypeHandle(); $path = Loader::pageTypeControllerPath($ctHandle, $item->getPackageHandle()); if ($path != false) { require_once $path; $class = Object::camelcase($ctHandle) . 'PageTypeController'; } } else { if ($c->isGeneratedCollection()) { $file = $c->getCollectionFilename(); if ($file != '') { // strip off PHP suffix for the $path variable, which needs it gone if (strpos($file, FILENAME_COLLECTION_VIEW) !== false) { $path = substr($file, 0, strpos($file, '/' . FILENAME_COLLECTION_VIEW)); } else { $path = substr($file, 0, strpos($file, '.php')); } } } } } else { if ($item instanceof Block || $item instanceof BlockType) { if (file_exists(DIR_FILES_BLOCK_TYPES . '/' . $item->getBlockTypeHandle() . '/' . FILENAME_BLOCK_CONTROLLER)) { require_once DIR_FILES_BLOCK_TYPES . "/" . $item->getBlockTypeHandle() . "/" . FILENAME_BLOCK_CONTROLLER; } else { if ($item->getPackageID() > 0 && file_exists(DIR_PACKAGES . '/' . $item->getPackageHandle() . '/' . DIRNAME_BLOCKS . '/' . $item->getBlockTypeHandle() . '/' . FILENAME_BLOCK_CONTROLLER)) { require_once DIR_PACKAGES . '/' . $item->getPackageHandle() . '/' . DIRNAME_BLOCKS . '/' . $item->getBlockTypeHandle() . '/' . FILENAME_BLOCK_CONTROLLER; } else { if (file_exists(DIR_FILES_BLOCK_TYPES_CORE . '/' . $item->getBlockTypeHandle() . '/' . FILENAME_BLOCK_CONTROLLER)) { require_once DIR_FILES_BLOCK_TYPES_CORE . "/" . $item->getBlockTypeHandle() . "/" . FILENAME_BLOCK_CONTROLLER; } } } $class = Object::camelcase($item->getBlockTypeHandle()) . 'BlockController'; if (class_exists($class) && $item instanceof BlockType) { $controller = new $class($item); } if ($item instanceof Block) { $c = $item->getBlockCollectionObject(); } } } $controllerFile = $path . '.php'; if ($path != '') { if (file_exists(DIR_FILES_CONTROLLERS . $controllerFile)) { include DIR_FILES_CONTROLLERS . $controllerFile; $include = true; } else { if (file_exists(DIR_FILES_CONTROLLERS . $path . '/' . FILENAME_COLLECTION_CONTROLLER)) { include DIR_FILES_CONTROLLERS . $path . '/' . FILENAME_COLLECTION_CONTROLLER; $include = true; } else { if (file_exists(DIR_FILES_CONTROLLERS_REQUIRED . $controllerFile)) { include DIR_FILES_CONTROLLERS_REQUIRED . $controllerFile; $include = true; } else { if (file_exists(DIR_FILES_CONTROLLERS_REQUIRED . $path . '/' . FILENAME_COLLECTION_CONTROLLER)) { include DIR_FILES_CONTROLLERS_REQUIRED . $path . '/' . FILENAME_COLLECTION_CONTROLLER; $include = true; } else { if (is_object($item)) { if ($item->getPackageID() > 0 && file_exists(DIR_FILES_CONTROLLERS . $controllerFile)) { include DIR_FILES_CONTROLLERS . $controllerFile; $include = true; } else { if ($item->getPackageID() > 0 && file_exists(DIR_PACKAGES . '/' . $item->getPackageHandle() . '/' . DIRNAME_CONTROLLERS . $controllerFile)) { include DIR_PACKAGES . '/' . $item->getPackageHandle() . '/' . DIRNAME_CONTROLLERS . $controllerFile; $include = true; } else { if ($item->getPackageID() > 0 && file_exists(DIR_PACKAGES . '/' . $item->getPackageHandle() . '/' . DIRNAME_CONTROLLERS . $path . '/' . FILENAME_COLLECTION_CONTROLLER)) { include DIR_PACKAGES . '/' . $item->getPackageHandle() . '/' . DIRNAME_CONTROLLERS . $path . '/' . FILENAME_COLLECTION_CONTROLLER; $include = true; } else { if ($item->getPackageID() > 0 && file_exists(DIR_PACKAGES_CORE . '/' . $item->getPackageHandle() . '/' . DIRNAME_CONTROLLERS . $controllerFile)) { include DIR_PACKAGES_CORE . '/' . $item->getPackageHandle() . '/' . DIRNAME_CONTROLLERS . $controllerFile; $include = true; } else { if ($item->getPackageID() > 0 && file_exists(DIR_PACKAGES_CORE . '/' . $item->getPackageHandle() . '/' . DIRNAME_CONTROLLERS . $path . '/' . FILENAME_COLLECTION_CONTROLLER)) { include DIR_PACKAGES_CORE . '/' . $item->getPackageHandle() . '/' . DIRNAME_CONTROLLERS . $path . '/' . FILENAME_COLLECTION_CONTROLLER; $include = true; } } } } } } } } } } if ($include) { $class = Object::camelcase($path) . 'Controller'; } } if (!isset($controller)) { if ($class && class_exists($class)) { // now we get just the filename for this guy, so we can extrapolate // what our controller is named $controller = new $class($item); } else { $controller = new Controller($item); } } if (is_object($c)) { $controller->setCollectionObject($c); } $controller->setupRestrictedMethods(); return $controller; }