public function findAllByElementAndSubspecialty($element_type_id, $subspecialty_id = null, $include_descendents = true)
{
$criteria = new \CDbCriteria();
$criteria->select = 't.*';
$criteria->distinct = true;
$element_type_ids = array($element_type_id);
if ($include_descendents) {
$element_type = \ElementType::model()->findByPk($element_type_id);
foreach ($element_type->getDescendents() as $descendent) {
$element_type_ids[] = $descendent->id;
}
}
$criteria->addInCondition('attribute_element.element_type_id', $element_type_ids);
if ($subspecialty_id) {
$criteria->addCondition('subspecialty_id = :subspecialty_id OR subspecialty_id IS NULL');
$criteria->params[':subspecialty_id'] = $subspecialty_id;
} else {
$criteria->addCondition('subspecialty_id IS NULL');
}
$criteria->join = 'JOIN ophciexamination_attribute_element attribute_element ON attribute_element.id = t.attribute_element_id
JOIN ophciexamination_attribute attribute ON attribute_element.attribute_id = attribute.id';
$criteria->order = 'attribute.display_order,attribute_element.attribute_id,t.display_order,t.id';
$all_attribute_options = OphCiExamination_AttributeOption::model()->findAll($criteria);
$attributes = array();
$attribute = null;
$attribute_options = array();
foreach ($all_attribute_options as $attribute_option) {
if (!$attribute || $attribute->id != $attribute_option->attribute_element->attribute_id) {
if ($attribute) {
$attribute->attribute_options = array_values($attribute_options);
$attribute_options = array();
$attributes[] = $attribute;
}
$attribute = $attribute_option->attribute_element->attribute;
}
$attribute_options[] = $attribute_option;
}
if ($attribute) {
$attribute->attribute_options = array_values($attribute_options);
$attributes[] = $attribute;
}
return $attributes;
}