<?php
function elements_modsnippet_39($scriptProperties= array()) {
global $modx;
if (is_array($scriptProperties)) {
extract($scriptProperties, EXTR_SKIP);
}
$trosk = $modx->getService('trosk','Trosk',$modx->getOption('doodles.core_path',null,$modx->getOption('core_path').'components/trosk/').'model/trosk/',$scriptProperties);
if (!($trosk instanceof Trosk)) return '';

		$projectId=$_REQUEST['proj_id'];
		
		$output=$trosk->renderTrosk($projectId,'html');
		return $output['output'];

		$children=$modx->getChildIds(2,1);
		function build($children, &$modx, $num, $projectId){
			$tpl='fem.trosk.renderSavedStavka';
			$modx=& $modx;
			$j=0;
			$i=0;
			foreach ($children as $id) {
				$decadents=$modx->getChildIds($id,15);
				if(empty($decadents)){
					continue;
				} 
				$criteria=$modx->newQuery('items');
				$criteria->where(array(
					'original_id:IN' => $decadents,
					'project_id'=>$projectId,
				));
				$ima=$modx->getCount('items',$criteria);
				if($ima>0){
					$childs=$modx->getchildIds($id,1);
					$cat=$modx->getObject('modResource',$id);
					$criteria=$modx->newQuery('items');
					$criteria->where(array(
						'original_id:IN' => $childs,
						'project_id'=>$projectId,
					));
					$stavke=$modx->getCollection('items',$criteria);
					$stavkeOutput='';
					if($j>0){
						$num=substr($num,0,-2);
					}
					$j++;
					$i++;
					$num.=$i.'.';
					if(!empty($stavke)) {
						$s=0;
						foreach ($stavke as $stavka) {
							$s++;
							$numStavka=$num.$s.'.';
							$stavkaUrl=$modx->makeUrl(25,'',array(
								'proj_id' => $projectId,
								'stav_id' => $stavka->id,
								'del' => 1,
							));
							$editUrl=$modx->makeUrl(26,'',array(
								'proj_id' => $projectId,
								'edit_id' => $stavka->id,
								'category_id' => $stavka->original_id,
							));
							$stavka->fromArray(array(
								'num'=>$numStavka,
								'stavkaUrl'=>$stavkaUrl,
								'editUrl'=>$editUrl,
							));
							$stavkaArray=$stavka->toArray();
							$stavkeOutput.=$modx->getChunk($tpl,$stavkaArray);
						}
					}
					$output.=$num.' '.$cat->pagetitle.'<br>'.$cat->content.$stavkeOutput.build($childs, $modx, $num, $projectId);
					//$j=0;
				} else {$i=0; continue;}
			}
			return $output;
		}
		$num='';
		$j=0;
	   	return build($children,$modx,$num,$projectId);
}
