/** * Return the number of articles of the given type. * @param string $p_type * Article Type * @return int */ public static function GetNumArticlesOfType($p_type) { $articleType = new ArticleType($p_type); if (!$articleType->exists()) { return false; } return $articleType->getNumArticles(); } // fn GetNumArticlesOfType
/** * Processes list constraints passed in an array. * * @param array $p_constraints * @return array */ protected function ProcessConstraints(array $p_constraints) { $parameters = array(); $state = 1; $attribute = null; $articleTypeName = null; $operator = null; $value = null; $context = CampTemplate::singleton()->context(); foreach ($p_constraints as $index=>$word) { switch ($state) { case self::CONSTRAINT_ATTRIBUTE_NAME: // reading the parameter name $attribute = strtolower($word); if (!array_key_exists($attribute, ArticlesList::$s_parameters)) { // not a static field; is it a article type name? self::ReadArticleTypes(); if (array_key_exists($attribute, self::$s_articleTypes)) { $articleTypeName = self::$s_articleTypes[$attribute]; $state = self::CONSTRAINT_DYNAMIC_FIELD; break; } // not an article type name; is it a dynamic field name? $dynamicFields = self::GetDynamicFields($articleTypeName, $attribute); if (count($dynamicFields) > 0) { if (count($dynamicFields) == 1) { $type = $dynamicFields[0]->getGenericType(); } else { $type = 'string'; } $state = self::CONSTRAINT_OPERATOR; break; } // unknown attribute CampTemplate::singleton()->trigger_error("invalid attribute $word in statement list_articles, constraints parameter"); return false; } else { $type = ArticlesList::$s_parameters[$attribute]['type']; } if ($attribute == 'keyword') { $operator = new Operator('is', 'string'); $state = self::CONSTRAINT_VALUE; } elseif ($attribute == 'matchalltopics' || $attribute == 'matchanytopic') { if ($attribute == 'matchalltopics') { $operator = new Operator('is', 'boolean'); $comparisonOperation = new ComparisonOperation($attribute, $operator, 'true'); $parameters[] = $comparisonOperation; } $state = self::CONSTRAINT_ATTRIBUTE_NAME; } else { $state = self::CONSTRAINT_OPERATOR; } $this->m_ignoreIssue = $this->m_ignoreIssue || $attribute == 'issue'; $this->m_ignoreSection = $this->m_ignoreSection || $attribute == 'section'; if ($attribute == 'onfrontpage' || $attribute == 'onsection') { if (($index + 1) < count($p_constraints)) { try { $operator = new Operator($p_constraints[$index+1], 'switch'); } catch (InvalidOperatorException $e) { $operator = new Operator('is', 'switch'); $comparisonOperation = new ComparisonOperation($attribute, $operator, 'on'); $parameters[] = $comparisonOperation; $state = self::CONSTRAINT_ATTRIBUTE_NAME; } } else { $operator = new Operator('is', 'switch'); $comparisonOperation = new ComparisonOperation($attribute, $operator, 'on'); $parameters[] = $comparisonOperation; $state = self::CONSTRAINT_ATTRIBUTE_NAME; } } break; case self::CONSTRAINT_DYNAMIC_FIELD: $attribute = strtolower($word); $dynamicFields = self::GetDynamicFields($articleTypeName, $attribute); if (count($dynamicFields) > 0) { $type = $dynamicFields[0]->getGenericType(); $state = self::CONSTRAINT_OPERATOR; break; } CampTemplate::singleton()->trigger_error("invalid dynamic field $word in statement list_articles, constraints parameter"); return false; case self::CONSTRAINT_OPERATOR: // reading the operator try { $operator = new Operator($word, $type); } catch (InvalidOperatorException $e) { CampTemplate::singleton()->trigger_error("invalid operator $word of parameter constraints.$attribute in statement list_articles"); return false; } $state = self::CONSTRAINT_VALUE; break; case self::CONSTRAINT_VALUE: // reading the value to compare against $metaClassName = 'Meta'.ucfirst($type); try { $valueObj = new $metaClassName($word); } catch (InvalidValueException $e) { CampTemplate::singleton()->trigger_error("invalid value $word of parameter constraints.$attribute in statement list_articles"); return false; } if ($attribute == 'type') { $word = trim($word); $articleType = new ArticleType($word); if (!$articleType->exists()) { CampTemplate::singleton()->trigger_error("invalid value $word of parameter constraints.$attribute in statement list_articles"); return false; } $value = $word; } elseif ($attribute == 'topic') { $topicObj = new Topic($word); if (!$topicObj->exists()) { CampTemplate::singleton()->trigger_error("invalid value $word of parameter constraints.$attribute in statement list_articles"); return false; } else { $value = $topicObj->getTopicId(); } } elseif ($attribute == 'author') { if (strtolower($word) == '__current') { $value = $context->article->author->name; } else { $value = $word; } } elseif ($type == 'switch') { $value = (int)(strtolower($word) == 'on'); } else { $value = $word; } if (!is_null($articleTypeName)) { $attribute = "$articleTypeName.$attribute"; } $comparisonOperation = new ComparisonOperation($attribute, $operator, $value); $parameters[] = $comparisonOperation; $state = self::CONSTRAINT_ATTRIBUTE_NAME; $attribute = null; $articleTypeName = null; $type = null; $value = null; break; } } if ($state != self::CONSTRAINT_ATTRIBUTE_NAME) { CampTemplate::singleton()->trigger_error("unexpected end of constraints parameter in list_articles"); return false; } if (!$this->m_ignoreIssue && $context->issue->defined) { $this->m_constraints[] = new ComparisonOperation('NrIssue', new Operator('is', 'integer'), $context->issue->number); } if (!$this->m_ignoreSection && $context->section->defined) { $this->m_constraints[] = new ComparisonOperation('NrSection', new Operator('is', 'integer'), $context->section->number); } return $parameters; }
if (!$valid) { $correct = false; $errorMsgs[] = getGS('The $1 field may only contain letters and underscore (_) character.', '</B>' . getGS('Name') . '</B>'); } if ($correct) { $old_articleType = new ArticleType($f_oldName); if (!$old_articleType->exists()) { $correct = false; $errorMsgs[] = getGS('The article type $1 does not exist.', '<B>'.htmlspecialchars($f_oldName).'</B>'); } } if ($correct) { $articleType = new ArticleType($f_name); if ($articleType->exists()) { $correct = false; $errorMsgs[] = getGS('The article type $1 already exists.', '<B>'. htmlspecialchars($f_name). '</B>'); } } if ($correct) { $old_articleType->rename($f_name); camp_html_goto_page("/$ADMIN/article_types/"); } } $crumbs = array(); $crumbs[] = array(getGS("Configure"), ""); $crumbs[] = array(getGS("Article Types"), "/$ADMIN/article_types/"); $crumbs[] = array(getGS("Rename article type '$1'", $f_oldName), "");
/** * Create article type if does not exist * * @param string $typeName * @return ArticleType */ private function getArticleType($typeName) { $requiredFields = array('guid' => 'text', 'version' => 'text', 'urgency' => 'text', 'copyright' => 'text', 'provider' => 'text', 'description' => 'body', 'dateline' => 'text', 'byline' => 'text', 'creditline' => 'text', 'inlinecontent' => 'body'); $type = new \ArticleType($typeName); if (!$type->exists()) { $type->create(); } $missingFields = array_diff(array_keys($requiredFields), $this->getFieldNames($type)); foreach ($missingFields as $fieldName) { $field = new \ArticleTypeField($type->getTypeName(), $fieldName); $field->create($requiredFields[$fieldName]); } return $type; }
/** * Processes list constraints passed in an array. * * @param array $p_constraints * @return array */ protected function ProcessConstraints(array $p_constraints) { $parameters = array(); $state = 1; $attribute = null; $articleTypeName = null; $operator = null; $value = null; $switchTypeHint = false; $context = CampTemplate::singleton()->context(); $constraintsCount = count($p_constraints); for ($index = 0; $index < $constraintsCount; $index++) { $word = $p_constraints[$index]; switch ($state) { case self::CONSTRAINT_ATTRIBUTE_NAME: // reading the parameter name $attribute = strtolower($word); if (!array_key_exists($attribute, ArticlesList::$s_parameters)) { // not a static field; is it a article type name? self::ReadArticleTypes(); if (array_key_exists($attribute, self::$s_articleTypes)) { $articleTypeName = self::$s_articleTypes[$attribute]; $state = self::CONSTRAINT_DYNAMIC_FIELD; break; } // not an article type name; is it a dynamic field name? $dynamicFields = self::GetDynamicFields($articleTypeName, $attribute); if (count($dynamicFields) > 0) { if (count($dynamicFields) == 1) { $type = $dynamicFields[0]->getGenericType(); } else { $type = null; foreach ($dynamicFields as $field) { $switchTypeHint = $switchTypeHint || $field->getType() == ArticleTypeField::TYPE_SWITCH; if (is_null($type)) { $type = $field->getGenericType(); } elseif ($type != $field->getGenericType()) { $type = 'string'; } } } $state = self::CONSTRAINT_OPERATOR; break; } // unknown attribute CampTemplate::singleton()->trigger_error("invalid attribute {$word} in statement list_articles, constraints parameter"); return false; } else { $type = ArticlesList::$s_parameters[$attribute]['type']; } if ($attribute == 'keyword') { $operator = new Operator('is', 'string'); $state = self::CONSTRAINT_VALUE; } elseif ($attribute == 'matchalltopics' || $attribute == 'matchanytopic') { if ($attribute == 'matchalltopics') { $operator = new Operator('is', 'boolean'); $comparisonOperation = new ComparisonOperation($attribute, $operator, 'true'); $parameters[] = $comparisonOperation; } $state = self::CONSTRAINT_ATTRIBUTE_NAME; } else { $state = self::CONSTRAINT_OPERATOR; } $this->m_ignoreIssue = $this->m_ignoreIssue || $attribute == 'issue'; $this->m_ignoreSection = $this->m_ignoreSection || $attribute == 'section'; if ($attribute == 'onfrontpage' || $attribute == 'onsection') { if ($index + 1 < count($p_constraints)) { try { $operator = new Operator($p_constraints[$index + 1], 'switch'); } catch (InvalidOperatorException $e) { $operator = new Operator('is', 'switch'); $comparisonOperation = new ComparisonOperation($attribute, $operator, 'on'); $parameters[] = $comparisonOperation; $state = self::CONSTRAINT_ATTRIBUTE_NAME; } } else { $operator = new Operator('is', 'switch'); $comparisonOperation = new ComparisonOperation($attribute, $operator, 'on'); $parameters[] = $comparisonOperation; $state = self::CONSTRAINT_ATTRIBUTE_NAME; } } break; case self::CONSTRAINT_DYNAMIC_FIELD: $attribute = strtolower($word); $dynamicFields = self::GetDynamicFields($articleTypeName, $attribute); if (count($dynamicFields) > 0) { $type = $dynamicFields[0]->getGenericType(); $state = self::CONSTRAINT_OPERATOR; break; } CampTemplate::singleton()->trigger_error("invalid dynamic field {$word} in statement list_articles, constraints parameter"); return false; case self::CONSTRAINT_OPERATOR: // reading the operator try { $operator = new Operator($word, $type); } catch (InvalidOperatorException $e) { CampTemplate::singleton()->trigger_error("invalid operator {$word} of parameter constraints.{$attribute} in statement list_articles"); return false; } $state = self::CONSTRAINT_VALUE; break; case self::CONSTRAINT_VALUE: // reading the value to compare against if ($attribute == 'publish_datetime' && $index + 1 < count($p_constraints)) { // add time to date if (preg_match('/^[0-2][0-9]:[0-5][0-9](:[0-5][0-9])?$/', $p_constraints[$index + 1])) { $word .= ' ' . $p_constraints[$index + 1]; $index++; // skip next value } } $metaClassName = 'Meta' . ucfirst($type); try { $valueObj = new $metaClassName($word); } catch (InvalidValueException $e) { CampTemplate::singleton()->trigger_error("invalid value {$word} of parameter constraints.{$attribute} in statement list_articles"); return false; } if ($attribute == 'type') { $word = trim($word); $articleType = new ArticleType($word); if (!$articleType->exists()) { CampTemplate::singleton()->trigger_error("invalid value {$word} of parameter constraints.{$attribute} in statement list_articles"); return false; } $value = $word; } elseif ($attribute == 'topic') { $context = CampTemplate::singleton()->context(); $locale = $context->language->code; $topicService = \Zend_Registry::get('container')->getService('topic'); $topic = $topicService->getTopicBy($word, $locale); if (!$topic) { CampTemplate::singleton()->trigger_error("invalid value {$word} of parameter constraints.{$attribute} in statement list_articles"); return false; } $value = $topic->getTopicId(); } elseif ($attribute == 'author') { if (strtolower($word) == '__current') { $value = $context->article->author->name; } else { $value = $word; } } elseif ($type == 'switch') { $value = (int) (strtolower($word) == 'on'); } else { $wordLower = strtolower($word); if ($switchTypeHint && ($wordLower == 'on' || $wordLower == 'off')) { $value = (int) (strtolower($word) == 'on'); } else { $value = $word; } } if (!is_null($articleTypeName)) { $attribute = "{$articleTypeName}.{$attribute}"; } $comparisonOperation = new ComparisonOperation($attribute, $operator, $value); $parameters[] = $comparisonOperation; $state = self::CONSTRAINT_ATTRIBUTE_NAME; $attribute = null; $articleTypeName = null; $type = null; $value = null; break; } } if ($state != self::CONSTRAINT_ATTRIBUTE_NAME) { CampTemplate::singleton()->trigger_error("unexpected end of constraints parameter in list_articles"); return false; } if (!$this->m_ignoreIssue && $context->issue->defined) { $this->m_constraints[] = new ComparisonOperation('NrIssue', new Operator('is', 'integer'), $context->issue->number); } if (!$this->m_ignoreSection && $context->section->defined) { $this->m_constraints[] = new ComparisonOperation('NrSection', new Operator('is', 'integer'), $context->section->number); } return $parameters; }