/** * returns array of ArticleCategory objects identified by numeric id or alias * * @param array $ids contains mixed category ids or alias * @throws ArticleCategoryException * @return array */ public static function getInstances(array $ids) { $db = Application::getInstance()->getDb(); $toRetrieveById = array(); $toRetrieveByAlias = array(); foreach ($ids as $id) { if (is_numeric($id)) { $id = (int) $id; if (!isset(self::$instancesById[$id])) { $toRetrieveById[] = $id; } } else { if (!isset(self::$instancesByAlias[$id])) { $toRetrieveByAlias[] = $id; } } $where = array(); if (count($toRetrieveById)) { $where[] = 'c.articlecategoriesID IN (' . implode(',', array_fill(0, count($toRetrieveById), '?')) . ')'; } if (count($toRetrieveByAlias)) { $where[] = 'c.alias IN (' . implode(',', array_fill(0, count($toRetrieveByAlias), '?')) . ')'; } if (count($where)) { $rows = $db->doPreparedQuery(' SELECT c.*, p.articlecategoriesID AS parentID FROM articlecategories c LEFT JOIN articlecategories p ON p.l < c.l AND p.r > c.r AND p.level = c.level - 1 WHERE ' . implode(' OR ', $where), array_merge($toRetrieveById, $toRetrieveByAlias)); foreach ($rows as $row) { if (!empty($row['level'])) { if (empty($row['parentID'])) { throw new ArticleCategoryException("Category '{$row['Title']}' not properly nested.", ArticleCategoryException::ARTICLECATEGORY_NOT_NESTED); } else { $cat = new self($row['Title'], ArticleCategory::getInstance($row['parentID'])); } } else { $cat = new self($row['Title']); } $cat->id = $row['articlecategoriesID']; $cat->alias = $row['Alias']; $cat->r = $row['r']; $cat->l = $row['l']; $cat->level = $row['level']; $cat->customSort = $row['customSort']; self::$instancesByAlias[$cat->alias] = $cat; self::$instancesById[$cat->id] = $cat; } } } $categories = array(); foreach ($ids as $id) { $categories[] = self::getInstance($id); } return $categories; }
/** * create Article instance from data supplied in $articleData * * @param array $articleData * @return Article */ private static function createInstance(array $articleData) { $article = new self(); // set identification $article->alias = $articleData['Alias']; $article->id = $articleData['articlesID']; // set category $article->category = ArticleCategory::getInstance($articleData['articlecategoriesID']); /* * set admin information (cast type explicitly to ensure lookup by adminID) * exceptions with invalid user ids are caught and ignored */ if ($articleData['createdBy']) { try { $article->createdBy = User::getInstance((int) $articleData['createdBy']); } catch (\InvalidArgumentException $e) { } catch (UserException $e) { } } if ($articleData['updatedBy']) { try { $article->updatedBy = User::getInstance((int) $articleData['updatedBy']); } catch (\InvalidArgumentException $e) { } catch (UserException $e) { } } if ($articleData['publishedBy']) { try { $article->publishedBy = User::getInstance((int) $articleData['publishedBy']); } catch (\InvalidArgumentException $e) { } catch (UserException $e) { } } // set date information if (!empty($articleData['Display_from'])) { $article->displayFrom = new \DateTime($articleData['Display_from']); } if (!empty($articleData['Display_until'])) { $article->displayUntil = new \DateTime($articleData['Display_until']); } if (!empty($articleData['Article_Date'])) { $article->articleDate = new \DateTime($articleData['Article_Date']); } if (!empty($articleData['firstCreated'])) { $article->firstCreated = new \DateTime($articleData['firstCreated']); } if (!empty($articleData['lastUpdated'])) { $article->lastUpdated = new \DateTime($articleData['lastUpdated']); } // flags and sort $article->published = $articleData['published']; $article->customFlags = $articleData['customFlags']; $article->customSort = $articleData['customSort']; // set various text fields $article->setHeadline($articleData['Headline']); $article->setData($articleData); // backup values to check whether record was changed $article->previouslySavedValues = new \stdClass(); $article->previouslySavedValues->headline = $article->headline; $article->previouslySavedValues->category = $article->category; $article->previouslySavedValues->data = $article->data; $article->previouslySavedValues->displayFrom = $article->displayFrom; $article->previouslySavedValues->displayUntil = $article->displayUntil; $article->previouslySavedValues->articleDate = $article->articleDate; $article->previouslySavedValues->published = $article->published; $article->previouslySavedValues->customFlags = $article->customFlags; $article->previouslySavedValues->customSort = $article->customSort; return $article; }