Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public function executeExport(sfWebRequest $request)
- {
- /*check access*/
- if($request->hasParameter('projects_id'))
- {
- $this->forward404Unless($this->projects = Doctrine_Core::getTable('Projects')->createQuery()->addWhere('id=?',$request->getParameter('projects_id'))->fetchOne(), sprintf('Object projects does not exist (%s).', $request->getParameter('projects_id')));
- $this->checkProjectsAccess($this->projects);
- $this->checkTasksAccess('view',false,$this->projects);
- }
- else
- {
- $this->checkTasksAccess('view');
- }
- $this->columns = array(
- 'Projects' => t::__('Project Name'),
- 'id' => t::__('Id'),
- 'TasksGroups' => t::__('Group'),
- 'Versions' => t::__('Version'),
- 'ProjectsPhases' => t::__('Phase'),
- 'TasksPriority' => t::__('Priority'),
- 'TasksLabels' => t::__('Label'),
- 'name' => t::__('Name'),
- 'TasksStatus' => t::__('Status'),
- 'TasksTypes' => t::__('Type'),
- 'assigned_to' => t::__('Assigned To'),
- 'Users' => t::__('Created By'),
- 'estimated_time' => t::__('Est. Time'),
- 'work_hours' => t::__('Work Hours'),
- 'start_date' => t::__('Start Date'),
- 'due_date' => t::__('Due Date'),
- 'progress' => t::__('Progress'),
- 'created_at' => t::__('Created At'),
- );
- $extra_fields = ExtraFieldsList::getFieldsByType('tasks',$this->getUser(),false,array('all'=>true));
- foreach($extra_fields as $v)
- {
- $this->columns['extra_field_' . $v['id']]=$v['name'];
- }
- $this->columns['url']=t::__('Url');
- if($fields = $request->getParameter('fields'))
- {
- $separator = "\t";
- $format = $request->getParameter('format','.csv');
- $filename = $request->getParameter('filename','tasks');
- header("Content-type: Application/octet-stream");
- header("Content-disposition: attachment; filename=" . $filename . "." . $format);
- header("Pragma: no-cache");
- header("Expires: 0");
- $content = '';
- foreach($fields as $f)
- {
- $content .= str_replace(array("\n\r","\r","\n",$separator),' ',$this->columns[$f]) . $separator;
- }
- $content .= "\n";
- if($format=='csv')
- {
- echo chr( 0xFF ) . chr( 0xFE ) . mb_convert_encoding( $content, 'UTF-16LE', 'UTF-8' );
- }
- else
- {
- echo $content;
- }
- if(strlen($request->getParameter('selected_items')==0)) exit();
- $q = Doctrine_Core::getTable('Tasks')->createQuery('t')
- ->leftJoin('t.TasksPriority tp')
- ->leftJoin('t.TasksStatus ts')
- ->leftJoin('t.TasksLabels tl')
- ->leftJoin('t.TasksTypes tt')
- ->leftJoin('t.TasksGroups tg')
- ->leftJoin('t.ProjectsPhases pp')
- ->leftJoin('t.Versions v')
- ->leftJoin('t.Projects p')
- ->leftJoin('t.Users')
- )
- ->whereIn('t.id',explode(',',$request->getParameter('selected_items')));
- if($request->hasParameter('projects_id'))
- {
- $q->addWhere('projects_id=?',$request->getParameter('projects_id'));
- if(Users::hasAccess('view_own','tasks',$this->getUser(),$request->getParameter('projects_id')))
- {
- $q->addWhere("find_in_set('" . $this->getUser()->getAttribute('id') . "',t.assigned_to) or t.created_by='" . $this->getUser()->getAttribute('id') . "'");
- }
- }
- else
- {
- if(Users::hasAccess('view_own','projects',$this->getUser()))
- {
- $q->addWhere("find_in_set('" . $this->getUser()->getAttribute('id') . "',team) or p.created_by='" . $this->getUser()->getAttribute('id') . "'");
- }
- if(Users::hasAccess('view_own','tasks',$this->getUser()))
- {
- $q->addWhere("find_in_set('" . $this->getUser()->getAttribute('id') . "',t.assigned_to) or t.created_by='" . $this->getUser()->getAttribute('id') . "'");
- }
- }
- $tree_order = false;
- if($request->hasParameter('projects_id'))
- {
- $q = app::addListingOrder($q,'tasks',$this->getUser(), (int)$request->getParameter('projects_id'));
- }
- else
- {
- $q->orderBy('LTRIM(p.name), ts.group desc, ts.sort_order, LTRIM(ts.name), LTRIM(t.name)');
- }
- $tasks = $q->fetchArray();
- $totals = array();
- $projects_totals = array();
- $current_project_id = 0;
- foreach($tasks as $t)
- {
- $content = '';
- //
- if($current_project_id==0) $current_project_id = $t['projects_id'];
- if($current_project_id!=$t['projects_id'])
- {
- //adding totals
- if(isset($projects_totals[$current_project_id]))
- {
- foreach($fields as $f)
- {
- $v = '';
- if(in_array($f,array('estimated_time','work_hours')))
- {
- $v = $projects_totals[$current_project_id][$f];
- }
- elseif(strstr($f,'extra_field_'))
- {
- if(isset($projects_totals[$current_project_id][str_replace('extra_field_','',$f)])) $v = $projects_totals[$current_project_id][str_replace('extra_field_','',$f)];
- }
- $content .= str_replace(array("\n\r","\r","\n",$separator),' ',$v) . $separator;
- }
- $content .= "\n\n";
- }
- $current_project_id=$t['projects_id'];
- }
- //
- $name_adding = '';
- $ex_values = ExtraFieldsList::getValuesList($extra_fields,$t['id']);
- foreach($fields as $f)
- {
- $v = '';
- if($f=='name')
- {
- $v=$name_adding . $t[$f];
- }
- elseif(in_array($f,array('id','description','estimated_time','work_hours')))
- {
- if($f=='work_hours')
- {
- $v = TasksComments::getTotalWorkHours($t['id']);
- }
- else
- {
- $v=$t[$f];
- }
- if(in_array($f,array('estimated_time','work_hours')))
- {
- if(!isset($totals[$f])) $totals[$f] = 0;
- if(!isset($projects_totals[$t['projects_id']][$f])) $projects_totals[$t['projects_id']][$f] = 0;
- $totals[$f]+=$v;
- $projects_totals[$t['projects_id']][$f]+=$v;
- }
- }
- elseif(in_array($f,array('start_date','due_date','created_at')))
- {
- $v=app::dateTimeFormat($t[$f]);
- }
- elseif($f=='progress')
- {
- $v = (int)$t['progress'] . '%';
- }
- elseif(strstr($f,'extra_field_'))
- {
- if($ex = Doctrine_Core::getTable('ExtraFields')->find(str_replace('extra_field_','',$f)))
- {
- $v = ExtraFieldsList::renderFieldValueByType($ex,$ex_values,array(),true);
- if(in_array($ex->getType(),array('number','formula')))
- {
- if(!isset($totals[$ex->getId()])) $totals[$ex->getId()] = 0;
- if(!isset($projects_totals[$t['projects_id']][$ex->getId()])) $projects_totals[$t['projects_id']][$ex->getId()] = 0;
- $totals[$ex->getId()]+= $v;
- $projects_totals[$t['projects_id']][$ex->getId()]+=$v;
- }
- $v = str_replace('<br>',', ',$v);
- }
- }
- elseif($f=='assigned_to')
- {
- $v = Users::getNameById($t[$f],', ');
- }
- elseif($f=='url')
- {
- $v = app::public_url('tasksComments/index?projects_id=' . $t['projects_id'] . '&tasks_id=' . $t['id']);
- }
- elseif($f=='actual_time')
- {
- if($t['work_hours']>0 and $t['togo_hours']==0)
- {
- $v = $t['work_hours'];
- }
- else
- {
- $v = (float)$t['estimated_time']-(float)$t['work_hours']+(float)$t['togo_hours'];
- }
- if(!isset($totals[$f])) $totals[$f] = 0;
- $totals[$f]+=$v;
- $projects_totals[$t['projects_id']][$f]+=$v;
- }
- else
- {
- $v=app::getArrayName($t,$f);
- }
- $content .= str_replace(array("\n\r","\r","\n",$separator),' ',$v) . $separator;
- }
- $content .= "\n";
- if($format=='csv')
- {
- echo chr( 0xFF ) . chr( 0xFE ) . mb_convert_encoding( $content, 'UTF-16LE', 'UTF-8' );
- }
- else
- {
- echo $content;
- }
- }
- $content = '';
- //adding totals
- if(isset($projects_totals[$current_project_id]) and !$request->hasParameter('projects_id'))
- {
- foreach($fields as $f)
- {
- $v = '';
- if(in_array($f,array('estimated_time','work_hours')))
- {
- $v = $projects_totals[$current_project_id][$f];
- }
- elseif(strstr($f,'extra_field_'))
- {
- if(isset($projects_totals[$current_project_id][str_replace('extra_field_','',$f)])) $v = $projects_totals[$current_project_id][str_replace('extra_field_','',$f)];
- }
- $content .= str_replace(array("\n\r","\r","\n",$separator),' ',$v) . $separator;
- }
- $content .= "\n\n";
- }
- foreach($fields as $f)
- {
- $v = '';
- if(in_array($f,array('estimated_time','work_hours')))
- {
- $v = $totals[$f];
- }
- elseif(strstr($f,'extra_field_'))
- {
- if(isset($totals[str_replace('extra_field_','',$f)])) $v = $totals[str_replace('extra_field_','',$f)];
- }
- $content .= str_replace(array("\n\r","\r","\n",$separator),' ',$v) . $separator;
- }
- $content .= "\n";
- if($format=='csv')
- {
- echo chr( 0xFF ) . chr( 0xFE ) . mb_convert_encoding( $content, 'UTF-16LE', 'UTF-8' );
- }
- else
- {
- echo $content;
- }
- exit();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement