/** * Print out array of models for the jqGrid rows. */ public function actionGridData() { if (!Yii::app()->request->isPostRequest) { throw new CHttpException(400, Yii::t('http', 'Invalid request. Please do not repeat this request again.')); exit; } // specify request details $jqGrid = $this->processJqGridRequest(); // specify filter parameters $company = isset($_GET['company']) ? $_GET['company'] : null; if ($company !== 'all' && !ctype_digit($company)) { $company = 'all'; } $priority = isset($_GET['priority']) ? $_GET['priority'] : null; if ($priority !== 'all' && $priority !== (string) Project::PRIORITY_HIGHEST && $priority !== (string) Project::PRIORITY_HIGH && $priority !== (string) Project::PRIORITY_MEDIUM && $priority !== (string) Project::PRIORITY_LOW && $priority !== (string) Project::PRIORITY_LOWEST) { $priority = 'all'; } $state = isset($_GET['state']) ? $_GET['state'] : null; if ($state !== 'all' && $state !== 'closed' && $state !== 'open') { $state = 'all'; } // criteria $criteria = new CDbCriteria(); $criteria->group = "`t`.`id`"; // required by together() $criteria->select = "`t`.closeDate, `t`.hourlyRate, `t`.openDate, `t`.priority, `t`.title"; //$criteria->select="`t`.`closeDate`, `t`.`hourlyRate`, `t`.`openDate`, `t`.`priority`, `t`.`title`"; // uncomment in yii-1.1.2 if ($jqGrid['searchField'] !== null && $jqGrid['searchString'] !== null && $jqGrid['searchOper'] !== null) { $field = array('closeDate' => "`t`.`closeDate`", 'hourlyRate' => "`t`.`hourlyRate`", 'openDate' => "`t`.`openDate`", 'priority' => "`t`.`priority`", 'title' => "`t`.`title`", 'company' => "`Project_Company`.`title`"); $operation = $this->getJqGridOperationArray(); $keywordFormula = $this->getJqGridKeywordFormulaArray(); if (isset($field[$jqGrid['searchField']]) && isset($operation[$jqGrid['searchOper']])) { $criteria->condition = '(' . $field[$jqGrid['searchField']] . ' ' . $operation[$jqGrid['searchOper']] . ' :keyword)'; $criteria->params = array(':keyword' => str_replace('keyword', $jqGrid['searchString'], $keywordFormula[$jqGrid['searchOper']])); // search by special field types if ($jqGrid['searchField'] === 'createTime' && ($keyword = strtotime($jqGrid['searchString'])) !== false) { $criteria->params = array(':keyword' => str_replace('keyword', $keyword, $keywordFormula[$jqGrid['searchOper']])); if (date('H:i:s', $keyword) === '00:00:00') { // visitor is looking for a precision by day, not by second $criteria->condition = '(TO_DAYS(FROM_UNIXTIME(' . $field[$jqGrid['searchField']] . ',"%Y-%m-%d")) ' . $operation[$jqGrid['searchOper']] . ' TO_DAYS(FROM_UNIXTIME(:keyword,"%Y-%m-%d")))'; } } } } if ($company !== 'all') { $criteria->addCondition("`Project_Company`.`id`=:companyId"); $criteria->params[':companyId'] = $company; } if ($priority === (string) Project::PRIORITY_HIGHEST) { $criteria->addCondition("`t`.`priority`=:priorityHighest"); $criteria->params[':priorityHighest'] = Project::PRIORITY_HIGHEST; } else { if ($priority === (string) Project::PRIORITY_HIGH) { $criteria->addCondition("`t`.`priority`=:priorityHigh"); $criteria->params[':priorityHigh'] = Project::PRIORITY_HIGH; } else { if ($priority === (string) Project::PRIORITY_MEDIUM) { $criteria->addCondition("`t`.`priority`=:priorityMedium"); $criteria->params[':priorityMedium'] = Project::PRIORITY_MEDIUM; } else { if ($priority === (string) Project::PRIORITY_LOW) { $criteria->addCondition("`t`.`priority`=:priorityLow"); $criteria->params[':priorityLow'] = Project::PRIORITY_LOW; } else { if ($priority === (string) Project::PRIORITY_LOWEST) { $criteria->addCondition("`t`.`priority`=:priorityLowest"); $criteria->params[':priorityLowest'] = Project::PRIORITY_LOWEST; } } } } } if ($state === 'closed') { $criteria->addCondition("`t`.`closeDate` IS NOT NULL"); } else { if ($state === 'open') { $criteria->addCondition("(`t`.`closeDate` IS NULL OR TO_DAYS('" . MDate::formatToDb(time(), 'date') . "') < TO_DAYS(`t`.`closeDate`))"); } } if (User::isClient()) { $criteria->addCondition("`Company_User2Company`.`userId`=:clientId AND `Company_User2Company`.`position`=:clientPosition"); $criteria->params[':clientId'] = Yii::app()->user->id; $criteria->params[':clientPosition'] = Company::OWNER; } if (User::isConsultant()) { $criteria->addCondition("`Task_Consultant`.`id`=:consultantId"); $criteria->params[':consultantId'] = Yii::app()->user->id; } // pagination $with = array(); if (strpos($criteria->condition, 'Project_Company') !== false) { $with[] = 'allCompany'; } if (strpos($criteria->condition, 'Company_User2Company') !== false) { $with[] = 'allCompany.allUser2Company'; } if (strpos($criteria->condition, 'Task_Consultant') !== false) { $with[] = 'allTask.allConsultant'; } if (count($with) >= 1) { $pages = new CPagination(Project::model()->with($with)->count($criteria)); } else { $pages = new CPagination(Project::model()->count($criteria)); } $pages->pageSize = $jqGrid['pageSize'] !== null ? $jqGrid['pageSize'] : self::GRID_PAGE_SIZE; $pages->applyLimit($criteria); // sort $sort = new CSort('Project'); $sort->attributes = array('closeDate' => array('asc' => "`t`.`closeDate`", 'desc' => "`t`.`closeDate` desc", 'label' => Project::model()->getAttributeLabel('Closed')), 'hourlyRate' => array('asc' => "`t`.`hourlyRate`", 'desc' => "`t`.`hourlyRate` desc", 'label' => Project::model()->getAttributeLabel('Rate')), 'openDate' => array('asc' => "`t`.`openDate`", 'desc' => "`t`.`openDate` desc", 'label' => Project::model()->getAttributeLabel('Opened')), 'priority' => array('asc' => "`t`.`priority`", 'desc' => "`t`.`priority` desc", 'label' => Project::model()->getAttributeLabel('priority')), 'title' => array('asc' => "`t`.`title`", 'desc' => "`t`.`title` desc", 'label' => Project::model()->getAttributeLabel('title')), 'company' => array('asc' => "`Project_Company`.`title`", 'desc' => "`Project_Company`.`title` desc", 'label' => Company2Project::model()->getAttributeLabel('companyId'))); $sort->defaultOrder = "`t`.`closeDate` ASC, `t`.`priority` ASC, `t`.`createTime` DESC"; $sort->applyOrder($criteria); // find all $with = array('allCompany' => array('select' => 'title')); if (strpos($criteria->condition, 'Company_User2Company') !== false) { $with['allCompany.allUser2Company'] = array('select' => 'id'); } if (strpos($criteria->condition, 'Task_Consultant') !== false) { $with['allTask.allConsultant'] = array('select' => 'id'); } $together = strpos($criteria->condition, 'Project_Company') !== false || strpos($criteria->order, 'Project_Company') !== false || strpos($criteria->condition, 'Company_User2Company') !== false || strpos($criteria->condition, 'Task_Consultant') !== false; if ($together) { $models = Project::model()->with($with)->together()->findAll($criteria); } else { $models = Project::model()->with($with)->findAll($criteria); } // create resulting data array $data = array('page' => $pages->getCurrentPage() + 1, 'total' => $pages->getPageCount(), 'records' => $pages->getItemCount(), 'rows' => array()); foreach ($models as $model) { $data['rows'][] = array('id' => $model->id, 'cell' => array(isset($model->allCompany[0]->id) ? CHtml::link(CHtml::encode($model->allCompany[0]->title), array('company/show', 'id' => $model->allCompany[0]->id)) : '', CHtml::encode($model->title), CHtml::encode($model->hourlyRate), CHtml::encode($model->getAttributeView('priority')), CHtml::encode(MDate::format($model->openDate, 'medium', null)), CHtml::encode(MDate::format($model->closeDate, 'medium', null)), CHtml::link('<span class="ui-icon ui-icon-zoomin"></span>', array('show', 'id' => $model->id), array('class' => 'w3-ig w3-link-icon w3-border-1px-transparent w3-first ui-corner-all', 'title' => Yii::t('link', 'Show'))) . CHtml::link('<span class="ui-icon ui-icon-pencil"></span>', array('update', 'id' => $model->id), array('class' => 'w3-ig w3-link-icon w3-border-1px-transparent w3-last ui-corner-all', 'title' => Yii::t('link', 'Edit'))))); } $this->printJson($data); }
/** * @return array relational rules. */ public function relations() { return array('allCompany2Project' => array(self::HAS_MANY, 'Company2Project', 'companyId', 'order' => "`t`.`projectPriority` ASC, `t`.`id` ASC", 'alias' => 'Company_Company2Project'), 'allLocation' => array(self::MANY_MANY, 'Location', Location2Record::model()->tableName() . '(recordId,locationId)', 'on' => "`allLocation_Company_Location`.`record`='Company'", 'order' => "`allLocation_Company_Location`.`locationPriority` ASC, `allLocation_Company_Location`.`id` ASC", 'alias' => 'Company_Location'), 'allLocation2Record' => array(self::HAS_MANY, 'Location2Record', 'recordId', 'on' => "`t`.`record`='Company'", 'order' => "`t`.`locationPriority` ASC, `t`.`id` ASC", 'alias' => 'Company_Location2Record'), 'allProject' => array(self::MANY_MANY, 'Project', Company2Project::model()->tableName() . '(companyId,projectId)', 'order' => "`allProject_Company_Project`.`projectPriority` ASC, `allProject_Company_Project`.`id` ASC", 'alias' => 'Company_Project'), 'allTask' => array(self::HAS_MANY, 'Task', 'companyId', 'order' => "`t`.`id` ASC", 'alias' => 'Company_Task'), 'allUser' => array(self::MANY_MANY, 'User', User2Company::model()->tableName() . '(companyId,userId)', 'order' => "`allUser_Company_User`.`userPriority` ASC, `allUser_Company_User`.`id` ASC", 'alias' => 'Company_User'), 'allUser2Company' => array(self::HAS_MANY, 'User2Company', 'companyId', 'order' => "`Company_User2Company`.`userPriority` ASC, `Company_User2Company`.`id` ASC", 'alias' => 'Company_User2Company')); }
/** * Grid of all models. */ public function actionGrid() { // specify filter parameters $company = isset($_GET['company']) ? $_GET['company'] : null; if ($company !== 'all' && !ctype_digit($company)) { $company = 'all'; } $consultant = isset($_GET['consultant']) ? $_GET['consultant'] : null; if ($consultant !== 'all' && $consultant !== 'me' && $consultant !== 'none' && !ctype_digit($consultant)) { $consultant = 'all'; } if (Yii::app()->user->checkAccess(User::CONSULTANT) && $consultant !== 'none') { $consultant = 'me'; } $priority = isset($_GET['priority']) ? $_GET['priority'] : null; if ($priority !== 'all' && $priority !== (string) Task::PRIORITY_HIGHEST && $priority !== (string) Task::PRIORITY_HIGH && $priority !== (string) Task::PRIORITY_MEDIUM && $priority !== (string) Task::PRIORITY_LOW && $priority !== (string) Task::PRIORITY_LOWEST) { $priority = 'all'; } $project = isset($_GET['project']) ? $_GET['project'] : null; if ($project !== 'all' && !ctype_digit($project)) { $project = 'all'; } $state = isset($_GET['state']) ? $_GET['state'] : null; if (Yii::app()->user->checkAccess(User::CLIENT) && $state === null) { $state = 'all'; } if ($state !== 'all' && $state !== 'closed' && $state !== 'open' && $state !== 'overdue') { $state = 'open'; } // criteria $criteria = new CDbCriteria(); $criteria->group = "`t`.`id`"; // required by together() $criteria->select = "`t`.dueDate, `t`.priority, `t`.status, `t`.title"; //$criteria->select="`t`.`dueDate`, `t`.`priority`, `t`.`status`, `t`.`title`"; // uncomment in yii-1.1.2 if ($company !== 'all') { $criteria->addCondition("`Task_Company`.`id`=:companyId"); $criteria->params[':companyId'] = $company; } if ($consultant !== 'all') { if ($consultant === 'none') { $criteria->addCondition("NOT EXISTS (SELECT 1 FROM `" . User2Task::model()->tableName() . "` `u2t` WHERE `u2t`.`taskId`=`t`.`id` AND `u2t`.`role`=:consultantRole)"); $criteria->params[':consultantRole'] = User2Task::CONSULTANT; } else { if ($consultant === 'me') { $criteria->addCondition("`Task_Consultant`.`id`=:consultantId"); $criteria->params[':consultantId'] = Yii::app()->user->id; } else { $criteria->addCondition("`Task_Consultant`.`id`=:consultantId"); $criteria->params[':consultantId'] = $consultant; } } } if ($priority === (string) Task::PRIORITY_HIGHEST) { $criteria->addCondition("`t`.`priority`=:priorityHighest"); $criteria->params[':priorityHighest'] = Task::PRIORITY_HIGHEST; } else { if ($priority === (string) Task::PRIORITY_HIGH) { $criteria->addCondition("`t`.`priority`=:priorityHigh"); $criteria->params[':priorityHigh'] = Task::PRIORITY_HIGH; } else { if ($priority === (string) Task::PRIORITY_MEDIUM) { $criteria->addCondition("`t`.`priority`=:priorityMedium"); $criteria->params[':priorityMedium'] = Task::PRIORITY_MEDIUM; } else { if ($priority === (string) Task::PRIORITY_LOW) { $criteria->addCondition("`t`.`priority`=:priorityLow"); $criteria->params[':priorityLow'] = Task::PRIORITY_LOW; } else { if ($priority === (string) Task::PRIORITY_LOWEST) { $criteria->addCondition("`t`.`priority`=:priorityLowest"); $criteria->params[':priorityLowest'] = Task::PRIORITY_LOWEST; } } } } } if ($project !== 'all') { $criteria->addCondition("`Task_Project`.`id`=:projectId"); $criteria->params[':projectId'] = $project; } if ($state === 'closed') { $criteria->addCondition("((`t`.`closeDate` IS NOT NULL AND TO_DAYS('" . MDate::formatToDb(time(), 'date') . "') <= TO_DAYS(`t`.`closeDate`))") . " OR `t`.`status`='" . Task::CANCELLED . "' OR `t`.`status`='" . Task::COMPLETED . "')"; } else { if ($state === 'open') { $criteria->addCondition("((`t`.`closeDate` IS NULL OR TO_DAYS('" . MDate::formatToDb(time(), 'date') . "') < TO_DAYS(`t`.`closeDate`))" . " AND `t`.`status`!='" . Task::CANCELLED . "' AND `t`.`status`!='" . Task::COMPLETED . "')"); } else { if ($state === 'overdue') { $criteria->addCondition("(((`t`.`closeDate` IS NULL OR TO_DAYS('" . MDate::formatToDb(time(), 'date') . "') < TO_DAYS(`t`.`closeDate`))" . " AND `t`.`status`!='" . Task::CANCELLED . "' AND `t`.`status`!='" . Task::COMPLETED . "') AND TO_DAYS('" . MDate::formatToDb(time(), 'date') . "') >= TO_DAYS(`t`.`dueDate`))"); } } } if (Yii::app()->user->checkAccess(User::CLIENT)) { $criteria->addCondition("`Company_User2Company`.`userId`=:clientId AND `Company_User2Company`.`position`=:clientPosition"); $criteria->params[':clientId'] = Yii::app()->user->id; $criteria->params[':clientPosition'] = Company::OWNER; } // pagination $with = array(); if (strpos($criteria->condition, 'Task_Company') !== false) { $with[] = 'company'; } if (strpos($criteria->condition, 'Task_Consultant') !== false) { $with[] = 'allConsultant'; } if (strpos($criteria->condition, 'Task_Project') !== false) { $with[] = 'project'; } if (strpos($criteria->condition, 'Company_User2Company') !== false) { $with[] = 'company.allUser2Company'; } if (count($with) >= 1) { $pages = new CPagination(Task::model()->with($with)->count($criteria)); } else { $pages = new CPagination(Task::model()->count($criteria)); } $pages->pageSize = self::GRID_PAGE_SIZE; $pages->applyLimit($criteria); // sort $sort = new CSort('Task'); $sort->attributes = array('dueDate' => array('asc' => "`t`.`dueDate`", 'desc' => "`t`.`dueDate` desc", 'label' => Task::model()->getAttributeLabel('Due')), 'priority' => array('asc' => "`t`.`priority`", 'desc' => "`t`.`priority` desc", 'label' => Task::model()->getAttributeLabel('Pr')), 'status' => array('asc' => "`t`.`status`", 'desc' => "`t`.`status` desc", 'label' => Task::model()->getAttributeLabel('status')), 'title' => array('asc' => "`t`.`title`", 'desc' => "`t`.`title` desc", 'label' => Task::model()->getAttributeLabel('title')), 'company' => array('asc' => "`Task_Company`.`title`", 'desc' => "`Task_Company`.`title` desc", 'label' => Task::model()->getAttributeLabel('companyId')), 'consultant' => array('asc' => "`Task_Consultant`.`screenName`", 'desc' => "`Task_Consultant`.`screenName` desc", 'label' => User::model()->getAttributeLabel('screenName')), 'project' => array('asc' => "`Task_Project`.`title`", 'desc' => "`Task_Project`.`title` desc", 'label' => Task::model()->getAttributeLabel('projectId'))); $sort->defaultOrder = "`t`.`closeDate` ASC, `t`.`priority` ASC, `t`.`dueDate` DESC"; $sort->applyOrder($criteria); // find all $with = array('allConsultant' => array('select' => 'screenName'), 'company' => array('select' => 'title'), 'project' => array('select' => 'title')); if (strpos($criteria->condition, 'Company_User2Company') !== false) { $with['company.allUser2Company'] = array('select' => 'id'); } $together = strpos($criteria->condition, 'Task_Consultant') !== false || strpos($criteria->order, 'Task_Consultant') !== false || strpos($criteria->condition, 'Company_User2Company') !== false || strpos($criteria->order, 'Company_User2Company') !== false; if ($together) { $models = Task::model()->with($with)->together()->findAll($criteria); } else { $models = Task::model()->with($with)->findAll($criteria); } // filters data $filters = array('company' => $company, 'project' => $project, 'consultant' => $consultant, 'priority' => $priority, 'state' => $state); $allCompany = array(array('text' => Yii::t('t', 'All'), 'url' => Yii::app()->createUrl($this->id . '/' . $this->action->id, array_merge($filters, array('company' => 'all'))), 'active' => $company === 'all')); $companyLinkText = $company === 'all' ? Yii::t('t', 'All companies') : ' '; $criteria = new CDbCriteria(); $criteria->select = "`t`.`id`, `t`.`title`, COUNT(`task`.`id`) as countTask"; if (Yii::app()->user->checkAccess(User::CONSULTANT)) { $criteria->join = "INNER JOIN `" . Company2Project::model()->tableName() . "` `c2p` ON `c2p`.`companyId`=`t`.`id`" . " INNER JOIN `" . Project::model()->tableName() . "` `project` ON `project`.`id`=`c2p`.`projectId`" . " INNER JOIN `" . Task::model()->tableName() . "` `task` ON `task`.`projectId`=`project`.`id`" . " INNER JOIN `" . User2Task::model()->tableName() . "` `u2t` ON `u2t`.`taskId`=`task`.`id`"; $criteria->condition = "`u2t`.`userId`=:consultantId AND `u2t`.`role`=:consultantRole"; $criteria->params[':consultantId'] = Yii::app()->user->id; $criteria->params[':consultantRole'] = User2Task::CONSULTANT; } else { if (Yii::app()->user->checkAccess(User::CLIENT)) { $criteria->join = "INNER JOIN `" . Company2Project::model()->tableName() . "` `c2p` ON `c2p`.`companyId`=`t`.`id`" . " INNER JOIN `" . Project::model()->tableName() . "` `project` ON `project`.`id`=`c2p`.`projectId`" . " INNER JOIN `" . Task::model()->tableName() . "` `task` ON `task`.`projectId`=`project`.`id`" . " INNER JOIN `" . User2Company::model()->tableName() . "` `u2c` ON `u2c`.`companyId`=`t`.`id`"; $criteria->condition = "`u2c`.`userId`=:clientId AND `u2c`.`position`=:clientPosition"; $criteria->params[':clientId'] = Yii::app()->user->id; $criteria->params[':clientPosition'] = Company::OWNER; } else { $criteria->join = "INNER JOIN `" . Company2Project::model()->tableName() . "` `c2p` ON `c2p`.`companyId`=`t`.`id`" . " INNER JOIN `" . Project::model()->tableName() . "` `project` ON `project`.`id`=`c2p`.`projectId`" . " INNER JOIN `" . Task::model()->tableName() . "` `task` ON `task`.`projectId`=`project`.`id`"; } } $criteria->group = "`t`.`id`"; $criteria->order = "`t`.`title` ASC"; $criteria->limit = 3000; foreach (Company::model()->findAll($criteria) as $model) { $allCompany[] = array('text' => CHtml::encode($model->title), 'url' => Yii::app()->createUrl($this->id . '/' . $this->action->id, array_merge($filters, array('company' => $model->id))), 'active' => $company === (string) $model->id); $i = count($allCompany) - 1; if ($allCompany[$i]['text'] === '') { $allCompany[$i]['text'] = Yii::t('t', '[no title]'); } $allCompany[$i]['text'] .= ' (' . $model->countTask . ')'; if ($company === (string) $model->id) { $companyLinkText = $model->title === '' ? Yii::t('t', '[no title]') : $model->title; } } $allConsultant = array(array('text' => Yii::t('t', 'All'), 'url' => Yii::app()->createUrl($this->id . '/' . $this->action->id, array_merge($filters, array('consultant' => 'all'))), 'active' => $consultant === 'all'), array('text' => Yii::t('t', 'Unassigned[tasks]'), 'url' => Yii::app()->createUrl($this->id . '/' . $this->action->id, array_merge($filters, array('consultant' => 'none'))), 'active' => $consultant === 'none')); $consultantLinkText = $consultant === 'all' ? Yii::t('t', 'All leaders') : ' '; if ($consultant === 'none') { $consultantLinkText = Yii::t('t', 'Unassigned[tasks]'); } if (Yii::app()->user->checkAccess(User::CONSULTANT)) { $allConsultant[0] = array('text' => Yii::t('t', 'My tasks'), 'url' => Yii::app()->createUrl($this->id . '/' . $this->action->id, array_merge($filters, array('consultant' => 'me'))), 'active' => $consultant === 'me'); if ($consultant === 'me') { $consultantLinkText = Yii::t('t', 'My tasks'); } } else { $criteria = new CDbCriteria(); $criteria->select = "`t`.`id`, `t`.`screenName`, COUNT(`task`.`id`) as countTask"; if (Yii::app()->user->checkAccess(User::CLIENT)) { $criteria->join = "INNER JOIN `" . User2Task::model()->tableName() . "` `u2t` ON `u2t`.`userId`=`t`.`id`" . " INNER JOIN `" . Task::model()->tableName() . "` `task` ON `task`.`id`=`u2t`.`taskId`" . " INNER JOIN `" . Company::model()->tableName() . "` `company` ON `company`.`id`=`task`.`companyId`" . " INNER JOIN `" . User2Company::model()->tableName() . "` `u2c` ON `u2c`.`companyId`=`company`.`id`"; $criteria->condition = "`u2t`.`role`=:consultantRole AND `u2c`.`userId`=:clientId AND `u2c`.`position`=:clientPosition"; $criteria->params[':consultantRole'] = User2Task::CONSULTANT; $criteria->params[':clientId'] = Yii::app()->user->id; $criteria->params[':clientPosition'] = Company::OWNER; } else { $criteria->join = "INNER JOIN `" . User2Task::model()->tableName() . "` `u2t` ON `u2t`.`userId`=`t`.`id` INNER JOIN `" . Task::model()->tableName() . "` `task` ON `task`.`id`=`u2t`.`taskId`"; $criteria->condition = "`u2t`.`role`=:consultantRole"; $criteria->params[':consultantRole'] = User2Task::CONSULTANT; } $criteria->group = "`t`.`id`"; $criteria->order = "`t`.`screenName`"; $criteria->limit = 3000; foreach (User::model()->findAll($criteria) as $model) { $allConsultant[] = array('text' => CHtml::encode($model->screenName), 'url' => Yii::app()->createUrl($this->id . '/' . $this->action->id, array_merge($filters, array('consultant' => $model->id))), 'active' => $consultant === (string) $model->id); $i = count($allConsultant) - 1; if ($allConsultant[$i]['text'] === '') { $allConsultant[$i]['text'] = Yii::t('t', '[no name]'); } $allConsultant[$i]['text'] .= ' (' . $model->countTask . ')'; if ($consultant === (string) $model->id) { $consultantLinkText = $model->screenName === '' ? Yii::t('t', '[no name]') : $model->screenName; } } } $allProject = array(array('text' => Yii::t('t', 'All'), 'url' => Yii::app()->createUrl($this->id . '/' . $this->action->id, array_merge($filters, array('project' => 'all'))), 'active' => $project === 'all')); $projectLinkText = $project === 'all' ? Yii::t('t', 'All projects') : ' '; $criteria = new CDbCriteria(); $criteria->select = "`t`.id, `t`.title, COUNT(`task`.`id`) as countTask"; //$criteria->select="`t`.`id`, `t`.`title`, COUNT(`task`.`id`) as countTask"; // uncomment in yii-1.1.2 if (Yii::app()->user->checkAccess(User::CONSULTANT)) { $criteria->join = "INNER JOIN `" . Task::model()->tableName() . "` `task` ON `task`.`projectId`=`t`.`id`" . " INNER JOIN `" . User2Task::model()->tableName() . "` `u2t` ON `u2t`.`taskId`=`task`.`id`"; $criteria->condition = "`u2t`.`userId`=:consultantId AND `u2t`.`role`=:consultantRole"; $criteria->params[':consultantId'] = Yii::app()->user->id; $criteria->params[':consultantRole'] = User2Task::CONSULTANT; } else { if (Yii::app()->user->checkAccess(User::CLIENT)) { $criteria->join = "INNER JOIN `" . Task::model()->tableName() . "` `task` ON `task`.`projectId`=`t`.`id`" . " INNER JOIN `" . Company::model()->tableName() . "` `company` ON `company`.`id`=`task`.`companyId`" . " INNER JOIN `" . User2Company::model()->tableName() . "` `u2c` ON `u2c`.`companyId`=`company`.`id`"; $criteria->condition = "`u2c`.`userId`=:clientId AND `u2c`.`position`=:clientPosition"; $criteria->params[':clientId'] = Yii::app()->user->id; $criteria->params[':clientPosition'] = Company::OWNER; } else { $criteria->join = "INNER JOIN `" . Task::model()->tableName() . "` `task` ON `task`.`projectId`=`t`.`id`"; } } $criteria->group = "`t`.`id`"; $criteria->order = "`Project_Company`.`titleAbbr` ASC, `t`.`title` ASC"; $criteria->limit = 3000; foreach (Project::model()->with(array('allCompany' => array('select' => 'titleAbbr')))->together()->findAll($criteria) as $model) { $allProject[] = array('text' => CHtml::encode((isset($model->allCompany[0]->titleAbbr) ? $model->allCompany[0]->titleAbbr . ' -- ' : '') . $model->title), 'url' => Yii::app()->createUrl($this->id . '/' . $this->action->id, array_merge($filters, array('project' => $model->id))), 'active' => $project === (string) $model->id); $i = count($allProject) - 1; if ($allProject[$i]['text'] === '') { $allProject[$i]['text'] = Yii::t('t', '[no title]'); } $allProject[$i]['text'] .= ' (' . $model->countTask . ')'; if ($project === (string) $model->id) { $projectLinkText = $model->title === '' ? Yii::t('t', '[no title]') : $model->title; } } $allPriority = array(array('text' => Yii::t('t', 'All'), 'url' => Yii::app()->createUrl($this->id . '/' . $this->action->id, array_merge($filters, array('priority' => 'all'))), 'active' => $priority === 'all'), array('text' => Yii::t('t', 'Highest[priority]'), 'url' => Yii::app()->createUrl($this->id . '/' . $this->action->id, array_merge($filters, array('priority' => Task::PRIORITY_HIGHEST))), 'active' => $priority === (string) Task::PRIORITY_HIGHEST), array('text' => Yii::t('t', 'High[priority]'), 'url' => Yii::app()->createUrl($this->id . '/' . $this->action->id, array_merge($filters, array('priority' => Task::PRIORITY_HIGH))), 'active' => $priority === (string) Task::PRIORITY_HIGH), array('text' => Yii::t('t', 'Medium[priority]'), 'url' => Yii::app()->createUrl($this->id . '/' . $this->action->id, array_merge($filters, array('priority' => Task::PRIORITY_MEDIUM))), 'active' => $priority === (string) Task::PRIORITY_MEDIUM), array('text' => Yii::t('t', 'Low[priority]'), 'url' => Yii::app()->createUrl($this->id . '/' . $this->action->id, array_merge($filters, array('priority' => Task::PRIORITY_LOW))), 'active' => $priority === (string) Task::PRIORITY_LOW), array('text' => Yii::t('t', 'Lowest[priority]'), 'url' => Yii::app()->createUrl($this->id . '/' . $this->action->id, array_merge($filters, array('priority' => Task::PRIORITY_LOWEST))), 'active' => $priority === (string) Task::PRIORITY_LOWEST)); switch ($priority) { case 'all': $priorityLinkText = Yii::t('t', 'All priorities'); break; case (string) Task::PRIORITY_HIGHEST: $priorityLinkText = Yii::t('t', 'Highest[priority]'); break; case (string) Task::PRIORITY_HIGH: $priorityLinkText = Yii::t('t', 'High[priority]'); break; case (string) Task::PRIORITY_MEDIUM: $priorityLinkText = Yii::t('t', 'Medium[priority]'); break; case (string) Task::PRIORITY_LOW: $priorityLinkText = Yii::t('t', 'Low[priority]'); break; case (string) Task::PRIORITY_LOWEST: $priorityLinkText = Yii::t('t', 'Lowest[priority]'); break; default: $priorityLinkText = ' '; } $allState = array(array('text' => Yii::t('t', 'All'), 'url' => Yii::app()->createUrl($this->id . '/' . $this->action->id, array_merge($filters, array('state' => 'all'))), 'active' => $state === 'all'), array('text' => Yii::t('t', 'Open[tasks]'), 'url' => Yii::app()->createUrl($this->id . '/' . $this->action->id, array_merge($filters, array('state' => 'open'))), 'active' => $state === 'open'), array('text' => Yii::t('t', 'Closed[tasks]'), 'url' => Yii::app()->createUrl($this->id . '/' . $this->action->id, array_merge($filters, array('state' => 'closed'))), 'active' => $state === 'closed'), array('text' => Yii::t('t', 'Overdue[tasks]'), 'url' => Yii::app()->createUrl($this->id . '/' . $this->action->id, array_merge($filters, array('state' => 'overdue'))), 'active' => $state === 'overdue')); switch ($state) { case 'all': $stateLinkText = Yii::t('t', 'All states[task]'); break; case 'closed': $stateLinkText = Yii::t('t', 'Closed[tasks]'); break; case 'open': $stateLinkText = Yii::t('t', 'Open[tasks]'); break; case 'overdue': $stateLinkText = Yii::t('t', 'Overdue[tasks]'); break; default: $stateLinkText = ' '; } // rows for the static grid $gridRows = array(); foreach ($models as $model) { $gridRows[] = array(array('content' => isset($model->company->id) ? CHtml::link(CHtml::encode($model->company->title), array('company/show', 'id' => $model->company->id)) : ''), array('content' => isset($model->project->id) ? CHtml::link(CHtml::encode($model->project->title), array('project/show', 'id' => $model->project->id)) : ''), array('content' => CHtml::encode($model->getAttributeView('priority'))), array('content' => CHtml::encode($model->title)), array('content' => isset($model->allConsultant[0]->id) ? CHtml::link(CHtml::encode($model->allConsultant[0]->screenName), array('user/show', 'id' => $model->allConsultant[0]->id)) : ''), array('align' => 'right', 'content' => CHtml::encode(MDate::format($model->dueDate, 'medium', null)), 'title' => CHtml::encode(MDate::format($model->dueDate, 'full', null))), array('content' => CHtml::encode($model->getAttributeView('status'))), array('content' => CHtml::link('<span class="ui-icon ui-icon-zoomin"></span>', array('show', 'id' => $model->id), array('class' => 'w3-ig w3-link-icon w3-border-1px-transparent w3-first ui-corner-all', 'title' => Yii::t('link', 'Show'))) . CHtml::link('<span class="ui-icon ui-icon-pencil"></span>', array('update', 'id' => $model->id), array('class' => 'w3-ig w3-link-icon w3-border-1px-transparent w3-last ui-corner-all', 'title' => Yii::t('link', 'Edit'))))); } // render the view file $this->render($this->action->id, array('models' => $models, 'pages' => $pages, 'sort' => $sort, 'company' => $company, 'consultant' => $consultant, 'priority' => $priority, 'project' => $project, 'state' => $state, 'filters' => $filters, 'allCompany' => $allCompany, 'companyLinkText' => $companyLinkText, 'allConsultant' => $allConsultant, 'consultantLinkText' => $consultantLinkText, 'allPriority' => $allPriority, 'priorityLinkText' => $priorityLinkText, 'allProject' => $allProject, 'projectLinkText' => $projectLinkText, 'allState' => $allState, 'stateLinkText' => $stateLinkText, 'gridRows' => $gridRows)); }
/** * @return array relational rules. */ public function relations() { return array('allCompany' => array(self::MANY_MANY, 'Company', Company2Project::model()->tableName() . '(projectId,companyId)', 'order' => "`allCompany_Project_Company`.`companyPriority` ASC, `allCompany_Project_Company`.`id` ASC", 'alias' => 'Project_Company'), 'allCompany2Project' => array(self::HAS_MANY, 'Company2Project', 'projectId', 'order' => "`t`.`companyPriority` ASC, `t`.`id` ASC", 'alias' => 'Project_Company2Project'), 'allConsultant' => array(self::MANY_MANY, 'User', User2Project::model()->tableName() . '(projectId,userId)', 'on' => "`allConsultant_Project_Consultant`.`role`='" . User2Project::CONSULTANT . "'", 'order' => "`allConsultant_Project_Consultant`.`userPriority` ASC, `allConsultant_Project_Consultant`.`id` ASC", 'alias' => 'Project_Consultant'), 'allConsultant2Project' => array(self::HAS_MANY, 'User2Project', 'projectId', 'on' => "`t`.`role`='" . User2Project::CONSULTANT . "'", 'order' => "`t`.`userPriority` ASC, `t`.`id` ASC", 'alias' => 'Project_Consultant2Project'), 'allManager' => array(self::MANY_MANY, 'User', User2Project::model()->tableName() . '(projectId,userId)', 'on' => "`allManager_Project_Manager`.`role`='" . User2Project::MANAGER . "'", 'order' => "`allManager_Project_Manager`.`userPriority` ASC, `allManager_Project_Manager`.`id` ASC", 'alias' => 'Project_Manager'), 'allManager2Project' => array(self::HAS_MANY, 'User2Project', 'projectId', 'on' => "`t`.`role`='" . User2Project::MANAGER . "'", 'order' => "`t`.`userPriority` ASC, `t`.`id` ASC", 'alias' => 'Project_Manager2Project'), 'allTask' => array(self::HAS_MANY, 'Task', 'projectId', 'order' => "`t`.`id` ASC", 'alias' => 'Project_Task'), 'allUser' => array(self::MANY_MANY, 'User', User2Project::model()->tableName() . '(projectId,userId)', 'order' => "`allUser_Project_User`.`userPriority` ASC, `allUser_Project_User`.`id` ASC", 'alias' => 'Project_User'), 'allUser2Project' => array(self::HAS_MANY, 'User2Project', 'projectId', 'order' => "`t`.`userPriority` ASC, `t`.`id` ASC", 'alias' => 'Project_User2Project')); }
/** * Grid of all models. */ public function actionGrid() { // specify filter parameters $company = isset($_GET['company']) ? $_GET['company'] : null; if ($company !== 'all' && !ctype_digit($company)) { $company = 'all'; } $consultant = isset($_GET['consultant']) ? $_GET['consultant'] : null; if ($consultant !== 'all' && $consultant !== 'me' && !ctype_digit($consultant)) { $consultant = 'all'; } if (Yii::app()->user->checkAccess(User::CONSULTANT)) { $consultant = 'me'; } $manager = isset($_GET['manager']) ? $_GET['manager'] : null; if ($manager !== 'all' && !ctype_digit($manager)) { $manager = 'all'; } $project = isset($_GET['project']) ? $_GET['project'] : null; if ($project !== 'all' && !ctype_digit($project)) { $project = 'all'; } $task = isset($_GET['task']) ? $_GET['task'] : null; if ($task !== 'all' && !ctype_digit($task)) { $task = 'all'; } // criteria $criteria = new CDbCriteria(); $criteria->group = "`t`.`id`"; // required by together() $criteria->select = "`t`.billedMinute, `t`.spentMinute, `t`.timeDate, `t`.title"; //$criteria->select="`t`.`billedMinute`, `t`.`spentMinute`, `t`.`timeDate`, `t`.`title`"; // uncomment in yii-1.1.2 if ($company !== 'all') { $criteria->addCondition("`Task_Company`.`id`=:companyId"); $criteria->params[':companyId'] = $company; } if ($consultant !== 'all') { if ($consultant === 'me') { $criteria->addCondition("`Time_Consultant`.`id`=:consultantId"); $criteria->params[':consultantId'] = Yii::app()->user->id; } else { $criteria->addCondition("`Time_Consultant`.`id`=:consultantId"); $criteria->params[':consultantId'] = $consultant; } } if ($manager !== 'all') { $criteria->addCondition("`Time_Manager`.`id`=:managerId"); $criteria->params[':managerId'] = $manager; } if ($project !== 'all') { $criteria->addCondition("`Task_Project`.`id`=:projectId"); $criteria->params[':projectId'] = $project; } if ($task !== 'all') { $criteria->addCondition("`Time_Task`.`id`=:taskId"); $criteria->params[':taskId'] = $task; } if (Yii::app()->user->checkAccess(User::CONSULTANT)) { $criteria->addCondition("`Company_User2Company`.`userId`=:clientId AND `Company_User2Company`.`position`=:clientPosition"); $criteria->params[':clientId'] = Yii::app()->user->id; $criteria->params[':clientPosition'] = Company::OWNER; } // pagination $with = array(); if (strpos($criteria->condition, 'Task_Company') !== false) { $with[] = 'task.company'; } if (strpos($criteria->condition, 'Time_Consultant') !== false) { $with[] = 'consultant'; } if (strpos($criteria->condition, 'Time_Manager') !== false) { $with[] = 'manager'; } if (strpos($criteria->condition, 'Task_Project') !== false) { $with[] = 'task.project'; } if (strpos($criteria->condition, 'Time_Task') !== false) { $with[] = 'task'; } if (strpos($criteria->condition, 'Company_User2Company') !== false) { $with[] = 'task.company.allUser2Company'; } if (count($with) >= 1) { $pages = new CPagination(Time::model()->with($with)->count($criteria)); } else { $pages = new CPagination(Time::model()->count($criteria)); } $pages->pageSize = self::GRID_PAGE_SIZE; $pages->applyLimit($criteria); // sort $sort = new CSort('Time'); $sort->attributes = array('billedMinute' => array('asc' => "`t`.`billedMinute`", 'desc' => "`t`.`billedMinute` desc", 'label' => Time::model()->getAttributeLabel('Bld')), 'spentMinute' => array('asc' => "`t`.`spentMinute`", 'desc' => "`t`.`spentMinute` desc", 'label' => Time::model()->getAttributeLabel('Spt')), 'timeDate' => array('asc' => "`t`.`timeDate`", 'desc' => "`t`.`timeDate` desc", 'label' => Time::model()->getAttributeLabel('Date')), 'title' => array('asc' => "`t`.`title`", 'desc' => "`t`.`title` desc", 'label' => Time::model()->getAttributeLabel('title')), 'consultant' => array('asc' => "`Time_Consultant`.`screenName`", 'desc' => "`Time_Consultant`.`screenName` desc", 'label' => Time::model()->getAttributeLabel('consultantId')), 'manager' => array('asc' => "`Time_Manager`.`screenName`", 'desc' => "`Time_Manager`.`screenName` desc", 'label' => Time::model()->getAttributeLabel('managerId')), 'task' => array('asc' => "`Time_Task`.`title`", 'desc' => "`Time_Task`.`title` desc", 'label' => Time::model()->getAttributeLabel('taskId')), 'company' => array('asc' => "`Task_Company`.`title`", 'desc' => "`Task_Company`.`title` desc", 'label' => Task::model()->getAttributeLabel('companyId')), 'project' => array('asc' => "`Task_Project`.`title`", 'desc' => "`Task_Project`.`title` desc", 'label' => Task::model()->getAttributeLabel('projectId'))); $sort->defaultOrder = "`t`.`timeDate` DESC, `t`.`createTime` DESC"; $sort->applyOrder($criteria); // find all $with = array('consultant' => array('select' => 'screenName'), 'manager' => array('select' => 'screenName'), 'task' => array('select' => 'title'), 'task.company' => array('select' => 'title'), 'task.project' => array('select' => 'title')); if (strpos($criteria->condition, 'Company_User2Company') !== false) { $with['task.company.allUser2Company'] = array('select' => 'id'); } $together = strpos($criteria->condition, 'Company_User2Company') !== false; if ($together) { $models = Time::model()->with($with)->together()->findAll($criteria); } else { $models = Time::model()->with($with)->findAll($criteria); } // filters data $filters = array('company' => $company, 'project' => $project, 'task' => $task, 'manager' => $manager, 'consultant' => $consultant); $allCompany = array(array('text' => Yii::t('t', 'All'), 'url' => Yii::app()->createUrl($this->id . '/' . $this->action->id, array_merge($filters, array('company' => 'all'))), 'active' => $company === 'all')); $companyLinkText = $company === 'all' ? Yii::t('t', 'All companies') : ' '; $criteria = new CDbCriteria(); $criteria->select = "`t`.`id`, `t`.`title`, COUNT(`time`.`id`) as countTime"; if (Yii::app()->user->checkAccess(User::CONSULTANT)) { $criteria->join = "INNER JOIN `" . Company2Project::model()->tableName() . "` `c2p` ON `c2p`.`companyId`=`t`.`id`" . " INNER JOIN `" . Project::model()->tableName() . "` `project` ON `project`.`id`=`c2p`.`projectId`" . " INNER JOIN `" . Task::model()->tableName() . "` `task` ON `task`.`projectId`=`project`.`id`" . " INNER JOIN `" . Time::model()->tableName() . "` `time` ON `time`.`taskId`=`task`.`id`"; $criteria->condition = "`time`.`consultantId`=:consultantId"; $criteria->params[':consultantId'] = Yii::app()->user->id; } else { if (Yii::app()->user->checkAccess(User::CLIENT)) { $criteria->join = "INNER JOIN `" . Company2Project::model()->tableName() . "` `c2p` ON `c2p`.`companyId`=`t`.`id`" . " INNER JOIN `" . Project::model()->tableName() . "` `project` ON `project`.`id`=`c2p`.`projectId`" . " INNER JOIN `" . Task::model()->tableName() . "` `task` ON `task`.`projectId`=`project`.`id`" . " INNER JOIN `" . Time::model()->tableName() . "` `time` ON `time`.`taskId`=`task`.`id`" . " INNER JOIN `" . User2Company::model()->tableName() . "` `u2c` ON `u2c`.`companyId`=`t`.`id`"; $criteria->condition = "`u2c`.`userId`=:clientId AND `u2c`.`position`=:clientPosition"; $criteria->params[':clientId'] = Yii::app()->user->id; $criteria->params[':clientPosition'] = Company::OWNER; } else { $criteria->join = "INNER JOIN `" . Company2Project::model()->tableName() . "` `c2p` ON `c2p`.`companyId`=`t`.`id`" . " INNER JOIN `" . Project::model()->tableName() . "` `project` ON `project`.`id`=`c2p`.`projectId`" . " INNER JOIN `" . Task::model()->tableName() . "` `task` ON `task`.`projectId`=`project`.`id`" . " INNER JOIN `" . Time::model()->tableName() . "` `time` ON `time`.`taskId`=`task`.`id`"; } } $criteria->group = "`t`.`id`"; $criteria->order = "`t`.`title` ASC"; $criteria->limit = 3000; foreach (Company::model()->findAll($criteria) as $model) { $allCompany[] = array('text' => CHtml::encode($model->title), 'url' => Yii::app()->createUrl($this->id . '/' . $this->action->id, array_merge($filters, array('company' => $model->id))), 'active' => $company === (string) $model->id); $i = count($allCompany) - 1; if ($allCompany[$i]['text'] === '') { $allCompany[$i]['text'] = Yii::t('t', '[no title]'); } $allCompany[$i]['text'] .= ' (' . $model->countTime . ')'; if ($company === (string) $model->id) { $companyLinkText = $model->title === '' ? Yii::t('t', '[no title]') : $model->title; } } $allConsultant = array(array('text' => Yii::t('t', 'All'), 'url' => Yii::app()->createUrl($this->id . '/' . $this->action->id, array_merge($filters, array('consultant' => 'all'))), 'active' => $consultant === 'all')); $consultantLinkText = $consultant === 'all' ? Yii::t('t', 'All consultants') : ' '; if (Yii::app()->user->checkAccess(User::CONSULTANT)) { $allConsultant[0] = array('text' => Yii::t('t', 'My time'), 'url' => Yii::app()->createUrl($this->id . '/' . $this->action->id, array_merge($filters, array('consultant' => 'me'))), 'active' => $consultant === 'me'); if ($consultant === 'me') { $consultantLinkText = Yii::t('t', 'My time'); } } else { $criteria = new CDbCriteria(); $criteria->select = "`t`.`id`, `t`.`screenName`, COUNT(`time`.`id`) as countTime"; if (Yii::app()->user->checkAccess(User::CLIENT)) { $criteria->join = "INNER JOIN `" . Time::model()->tableName() . "` `time` ON `time`.`consultantId`=`t`.`id`" . " INNER JOIN `" . Task::model()->tableName() . "` `task` ON `task`.`id`=`time`.`taskId`" . " INNER JOIN `" . Company::model()->tableName() . "` `company` ON `company`.`id`=`task`.`companyId`" . " INNER JOIN `" . User2Company::model()->tableName() . "` `u2c` ON `u2c`.`companyId`=`company`.`id`"; $criteria->condition = "`u2c`.`userId`=:clientId AND `u2c`.`position`=:clientPosition"; $criteria->params[':clientId'] = Yii::app()->user->id; $criteria->params[':clientPosition'] = Company::OWNER; } else { $criteria->join = "INNER JOIN `" . Time::model()->tableName() . "` `time` ON `time`.`consultantId`=`t`.`id`"; } $criteria->group = "`time`.`consultantId`"; $criteria->order = "`t`.`screenName`"; $criteria->limit = 3000; foreach (User::model()->findAll($criteria) as $model) { $allConsultant[] = array('text' => CHtml::encode($model->screenName), 'url' => Yii::app()->createUrl($this->id . '/' . $this->action->id, array_merge($filters, array('consultant' => $model->id))), 'active' => $consultant === (string) $model->id); $i = count($allConsultant) - 1; if ($allConsultant[$i]['text'] === '') { $allConsultant[$i]['text'] = Yii::t('t', '[no name]'); } $allConsultant[$i]['text'] .= ' (' . $model->countTime . ')'; if ($consultant === (string) $model->id) { $consultantLinkText = $model->screenName === '' ? Yii::t('t', '[no name]') : $model->screenName; } } } $allManager = array(array('text' => Yii::t('t', 'All'), 'url' => Yii::app()->createUrl($this->id . '/' . $this->action->id, array_merge($filters, array('manager' => 'all'))), 'active' => $manager === 'all')); $managerLinkText = $manager === 'all' ? Yii::t('t', 'All managers') : ' '; $criteria = new CDbCriteria(); $criteria->select = "`t`.`id`, `t`.`screenName`, COUNT(`time`.`id`) as countTime"; if (Yii::app()->user->checkAccess(User::CONSULTANT)) { $criteria->join = "INNER JOIN `" . Time::model()->tableName() . "` `time` ON `time`.`managerId`=`t`.`id`"; $criteria->condition = "`time`.`consultantId`=:consultantId"; $criteria->params[':consultantId'] = Yii::app()->user->id; } else { if (Yii::app()->user->checkAccess(User::CLIENT)) { $criteria->join = "INNER JOIN `" . Time::model()->tableName() . "` `time` ON `time`.`managerId`=`t`.`id`" . " INNER JOIN `" . Task::model()->tableName() . "` `task` ON `task`.`id`=`time`.`taskId`" . " INNER JOIN `" . Company::model()->tableName() . "` `company` ON `company`.`id`=`task`.`companyId`" . " INNER JOIN `" . User2Company::model()->tableName() . "` `u2c` ON `u2c`.`companyId`=`company`.`id`"; $criteria->condition = "`u2c`.`userId`=:clientId AND `u2c`.`position`=:clientPosition"; $criteria->params[':clientId'] = Yii::app()->user->id; $criteria->params[':clientPosition'] = Company::OWNER; } else { $criteria->join = "INNER JOIN `" . Time::model()->tableName() . "` `time` ON `time`.`managerId`=`t`.`id`"; } } $criteria->group = "`time`.`managerId`"; $criteria->order = "`t`.`screenName`"; $criteria->limit = 3000; foreach (User::model()->findAll($criteria) as $model) { $allManager[] = array('text' => CHtml::encode($model->screenName), 'url' => Yii::app()->createUrl($this->id . '/' . $this->action->id, array_merge($filters, array('manager' => $model->id))), 'active' => $manager === (string) $model->id); $i = count($allManager) - 1; if ($allManager[$i]['text'] === '') { $allManager[$i]['text'] = Yii::t('t', '[no name]'); } $allManager[$i]['text'] .= ' (' . $model->countTime . ')'; if ($manager === (string) $model->id) { $managerLinkText = $model->screenName === '' ? Yii::t('t', '[no name]') : $model->screenName; } } $allProject = array(array('text' => Yii::t('t', 'All'), 'url' => Yii::app()->createUrl($this->id . '/' . $this->action->id, array_merge($filters, array('project' => 'all'))), 'active' => $project === 'all')); $projectLinkText = $project === 'all' ? Yii::t('t', 'All projects') : ' '; $criteria = new CDbCriteria(); $criteria->select = "`t`.id, `t`.title, COUNT(`time`.`id`) as countTime"; //$criteria->select="`t`.`id`, `t`.`title`, COUNT(`time`.`id`) as countTime"; // uncomment in yii-1.1.2 if (Yii::app()->user->checkAccess(User::CONSULTANT)) { $criteria->join = "INNER JOIN `" . Task::model()->tableName() . "` `task` ON `task`.`projectId`=`t`.`id`" . " INNER JOIN `" . Time::model()->tableName() . "` `time` ON `time`.`taskId`=`task`.`id`"; $criteria->condition = "`time`.`consultantId`=:consultantId"; $criteria->params[':consultantId'] = Yii::app()->user->id; } else { if (Yii::app()->user->checkAccess(User::CLIENT)) { $criteria->join = "INNER JOIN `" . Task::model()->tableName() . "` `task` ON `task`.`projectId`=`t`.`id`" . " INNER JOIN `" . Time::model()->tableName() . "` `time` ON `time`.`taskId`=`task`.`id`" . " INNER JOIN `" . Company::model()->tableName() . "` `company` ON `company`.`id`=`task`.`companyId`" . " INNER JOIN `" . User2Company::model()->tableName() . "` `u2c` ON `u2c`.`companyId`=`company`.`id`"; $criteria->condition = "`u2c`.`userId`=:clientId AND `u2c`.`position`=:clientPosition"; $criteria->params[':clientId'] = Yii::app()->user->id; $criteria->params[':clientPosition'] = Company::OWNER; } else { $criteria->join = "INNER JOIN `" . Task::model()->tableName() . "` `task` ON `task`.`projectId`=`t`.`id`" . " INNER JOIN `" . Time::model()->tableName() . "` `time` ON `time`.`taskId`=`task`.`id`"; } } $criteria->group = "`t`.`id`"; $criteria->order = "`Project_Company`.`titleAbbr` ASC, `t`.`title` ASC"; $criteria->limit = 3000; foreach (Project::model()->with(array('allCompany' => array('select' => 'titleAbbr')))->together()->findAll($criteria) as $model) { $allProject[] = array('text' => CHtml::encode((isset($model->allCompany[0]->titleAbbr) ? $model->allCompany[0]->titleAbbr . ' -- ' : '') . $model->title), 'url' => Yii::app()->createUrl($this->id . '/' . $this->action->id, array_merge($filters, array('project' => $model->id))), 'active' => $project === (string) $model->id); $i = count($allProject) - 1; if ($allProject[$i]['text'] === '') { $allProject[$i]['text'] = Yii::t('t', '[no title]'); } $allProject[$i]['text'] .= ' (' . $model->countTime . ')'; if ($project === (string) $model->id) { $projectLinkText = $model->title === '' ? Yii::t('t', '[no title]') : $model->title; } } $allTask = array(array('text' => Yii::t('t', 'All'), 'url' => Yii::app()->createUrl($this->id . '/' . $this->action->id, array_merge($filters, array('task' => 'all'))), 'active' => $task === 'all')); $taskLinkText = $task === 'all' ? Yii::t('t', 'All tasks') : ' '; $criteria = new CDbCriteria(); $criteria->select = "`t`.id, `t`.title, COUNT(`time`.`id`) as countTime"; //$criteria->select="`t`.`id`, `t`.`title`, COUNT(`time`.`id`) as countTime"; // uncomment in yii-1.1.2 if (Yii::app()->user->checkAccess(User::CONSULTANT)) { $criteria->join = "INNER JOIN `" . Time::model()->tableName() . "` `time` ON `time`.`taskId`=`t`.`id`"; $criteria->condition = "`time`.`consultantId`=:consultantId"; $criteria->params[':consultantId'] = Yii::app()->user->id; } else { if (Yii::app()->user->checkAccess(User::CLIENT)) { $criteria->join = "INNER JOIN `" . Time::model()->tableName() . "` `time` ON `time`.`taskId`=`t`.`id`" . " INNER JOIN `" . Company::model()->tableName() . "` `company` ON `company`.`id`=`t`.`companyId`" . " INNER JOIN `" . User2Company::model()->tableName() . "` `u2c` ON `u2c`.`companyId`=`company`.`id`"; $criteria->condition = "`u2c`.`userId`=:clientId AND `u2c`.`position`=:clientPosition"; $criteria->params[':clientId'] = Yii::app()->user->id; $criteria->params[':clientPosition'] = Company::OWNER; } else { $criteria->join = "INNER JOIN `" . Time::model()->tableName() . "` `time` ON `time`.`taskId`=`t`.`id`"; } } $criteria->group = "`t`.`id`"; $criteria->order = "`Task_Project`.`title` ASC, `t`.`title` ASC"; $criteria->limit = 3000; foreach (Task::model()->with(array('project' => array('select' => 'title')))->findAll($criteria) as $model) { $allTask[] = array('text' => CHtml::encode((isset($model->project->title) ? $model->project->title . ' -- ' : '') . $model->title), 'url' => Yii::app()->createUrl($this->id . '/' . $this->action->id, array_merge($filters, array('task' => $model->id))), 'active' => $task === (string) $model->id); $i = count($allTask) - 1; if ($allTask[$i]['text'] === '') { $allTask[$i]['text'] = Yii::t('t', '[no title]'); } $allTask[$i]['text'] .= ' (' . $model->countTime . ')'; if ($task === (string) $model->id) { $taskLinkText = $model->title === '' ? Yii::t('t', '[no title]') : $model->title; } } // rows for the static grid $gridRows = array(); foreach ($models as $model) { $gridRows[] = array(array('content' => isset($model->task->company->id) ? CHtml::link(CHtml::encode($model->task->company->title), array('company/show', 'id' => $model->task->company->id)) : ''), array('content' => isset($model->task->project->id) ? CHtml::link(CHtml::encode($model->task->project->title), array('project/show', 'id' => $model->task->project->id)) : ''), array('content' => isset($model->task->id) ? CHtml::link(CHtml::encode($model->task->title), array('task/show', 'id' => $model->task->id)) : ''), array('content' => isset($model->manager->id) ? CHtml::link(CHtml::encode($model->manager->screenName), array('user/show', 'id' => $model->manager->id)) : ''), array('content' => isset($model->consultant->id) ? CHtml::link(CHtml::encode($model->consultant->screenName), array('user/show', 'id' => $model->consultant->id)) : ''), array('align' => 'right', 'content' => CHtml::encode(MDate::format($model->timeDate, 'medium', null)), 'title' => CHtml::encode(MDate::format($model->timeDate, 'full', null))), array('content' => CHtml::encode($model->getAttributeView('spentMinute'))), array('content' => CHtml::encode($model->getAttributeView('billedMinute'))), array('content' => CHtml::encode($model->title)), array('content' => Yii::app()->user->checkAccess('time/show') ? CHtml::link('<span class="ui-icon ui-icon-zoomin"></span>', array('show', 'id' => $model->id), array('class' => 'w3-ig w3-link-icon w3-border-1px-transparent w3-first ui-corner-all', 'title' => Yii::t('link', 'Show'))) . CHtml::link('<span class="ui-icon ui-icon-pencil"></span>', array('update', 'id' => $model->id), array('class' => 'w3-ig w3-link-icon w3-border-1px-transparent ui-corner-all', 'title' => Yii::t('link', 'Edit'))) . CHtml::link('<span class="ui-icon ui-icon-trash"></span>', array('delete', 'id' => $model->id), array('class' => 'w3-ig w3-link-icon w3-border-1px-transparent w3-last ui-corner-all', 'title' => Yii::t('link', 'Delete the record number {id}', array('{id}' => $model->id)))) : CHtml::link('<span class="ui-icon ui-icon-zoomin"></span>', array('show', 'id' => $model->id), array('class' => 'w3-ig w3-link-icon w3-border-1px-transparent w3-first w3-last ui-corner-all', 'title' => Yii::t('link', 'Show'))))); } // render the view file $this->render($this->action->id, array('models' => $models, 'pages' => $pages, 'sort' => $sort, 'company' => $company, 'consultant' => $consultant, 'manager' => $manager, 'project' => $project, 'task' => $task, 'filters' => $filters, 'allCompany' => $allCompany, 'companyLinkText' => $companyLinkText, 'allConsultant' => $allConsultant, 'consultantLinkText' => $consultantLinkText, 'allManager' => $allManager, 'managerLinkText' => $managerLinkText, 'allProject' => $allProject, 'projectLinkText' => $projectLinkText, 'allTask' => $allTask, 'taskLinkText' => $taskLinkText, 'gridRows' => $gridRows)); }
/** * Updates a particular model. * If update is successful, the browser will be redirected to the 'show' page. */ public function actionUpdate() { if (($model = $this->loadModel()) === null) { // model not found MUserFlash::setTopError(Yii::t('modelNotFound', $this->id)); $this->redirect($this->getGotoUrl()); } // explicitly set model scenario to be current action //$model->setScenario($this->action->id); // whether data is passed if (isset($_POST['Expense'])) { // collect user input data $model->attributes = $_POST['Expense']; if (!isset($_POST['Expense']['companyId'])) { // set company based on the project if ($model->projectId >= 1) { $criteria = new CDbCriteria(); $criteria->order = "`t`.`companyPriority` ASC, `t`.`id` ASC"; if (($company2Project = Company2Project::model()->findByAttributes(array('projectId' => $model->projectId), $criteria)) !== null) { $model->companyId = $company2Project->companyId; } else { $model->companyId = 0; } } else { $model->companyId = 0; } } // validate with the current action as scenario and save without validation if (($validated = $model->validate()) !== false && ($saved = $model->save(false)) !== false) { // set success message MUserFlash::setTopSuccess(Yii::t('hint', 'The expense record number "{expenseNumber}" has been updated.', array('{expenseNumber}' => MHtml::wrapInTag($model->id, 'strong')))); // go to the 'show' page $this->redirect(array('show', 'id' => $model->id)); } } $this->render($this->action->id, array('model' => $model)); }
<?php MParams::setPageLabel(Yii::t('page', 'View "{title}" project', array('{title}' => $model->title))); MLinkList::set('sidebar', array('links' => array(array('text' => Yii::t('link', 'Edit project'), 'url' => array('update', 'id' => $model->id), 'icon' => 'pencil', 'visible' => Yii::app()->user->checkAccess($this->id . '/update')), array('text' => Yii::t('link', 'Add a task'), 'url' => array('task/create', 'projectId' => $model->id), 'icon' => 'plus', 'visible' => Yii::app()->user->checkAccess('task/create')), array('text' => Yii::t('link', 'Add an expense'), 'url' => array('expense/create', 'projectId' => $model->id), 'icon' => 'plus', 'visible' => Yii::app()->user->checkAccess('expense/create')), array('text' => Yii::t('link', 'List of projects'), 'url' => array('list'), 'icon' => 'grip-solid-horizontal', 'visible' => false), array('text' => Yii::t('link', 'Grid of projects'), 'url' => array('grid'), 'icon' => 'calculator', 'visible' => Yii::app()->user->checkAccess($this->id . '/grid')), array('text' => Yii::t('link', 'Create a new project'), 'url' => array('create'), 'icon' => 'plus', 'visible' => Yii::app()->user->checkAccess($this->id . '/create'))))); $this->widget('application.components.WContentHeader', array('breadcrumbs' => array(array('text' => Yii::t('link', 'Projects'), 'url' => array($this->id . '/' . $this->defaultAction), 'active' => false, 'visible' => Yii::app()->user->checkAccess($this->id . '/' . $this->defaultAction)), array('url' => array($this->action->id, 'id' => $model->id), 'active' => true)))); ?> <div class="w3-detail-box ui-widget-content ui-corner-all"> <div class="w3-detail-row w3-first"> <div class="w3-detail-row-label"><?php echo CHtml::encode(Company2Project::model()->getAttributeLabel('companyId')); ?> </div> <div class="w3-detail-row-value"><?php echo isset($model->allCompany[0]->id) ? CHtml::link(CHtml::encode($model->allCompany[0]->title), array('company/show', 'id' => $model->allCompany[0]->id)) : ''; ?> </div> <div class="clear"> </div> </div> <div class="w3-detail-row"> <div class="w3-detail-row-label"><?php echo CHtml::encode(User2Project::model()->getAttributeLabel('managerId')); ?> </div> <div class="w3-detail-row-value"><?php echo isset($model->allManager[0]->id) ? CHtml::link(CHtml::encode($model->allManager[0]->screenName), array('user/show', 'id' => $model->allManager[0]->id)) : ''; ?> </div> <div class="clear"> </div> </div> <div class="w3-detail-row">
/** * Migrate from old project management system. */ public function actionIndex() { // check rights if (!Yii::app()->user->checkAccess(User::ADMINISTRATOR)) { throw new CHttpException(403, Yii::t('Yii', 'You are not authorized to perform this action.')); } // models to migrate $migrate = array('User' => false, 'Company' => false, 'CompanyPayment' => false, 'Project' => false, 'Task' => false, 'Time' => true, 'Invoice' => false, 'Expense' => false); // start $message = ''; // we won't migrate unless form is submitted if (Yii::app()->request->isPostRequest) { // default criteria $findAllCriteria = new CDbCriteria(); $findAllCriteria->order = "`id` ASC"; if ($migrate['User']) { // user $mUsers = MUser::model()->findAll($findAllCriteria); if (is_array($mUsers)) { $i = $j = $c = 0; $accessType = array('customer' => 'client', 'consultant' => 'consultant', 'manager' => 'manager', 'admin' => 'administrator'); $accessLevel = array('customer' => 2, 'consultant' => 3, 'manager' => 4, 'admin' => 5); foreach ($mUsers as $oldModel) { if (($model = User::model()->findByPk($oldModel->id)) !== null) { $model->delete(); } // old model validation if (User::model()->findByAttributes(array('email' => $oldModel->email))) { $oldModel->email = rand(10, 99) . $oldModel->email; } $closeTime = strtotime($oldModel->close_date); $isActive = empty($oldModel->close_date) || $oldModel->close_date === '0000-00-00' || $closeTime === false; // new model $model = new User('migrate'); $model->username = $oldModel->email; $model->password = md5($oldModel->password); $model->email = $oldModel->email; $model->screenName = $oldModel->name; $model->accessType = isset($accessType[$oldModel->class]) ? $accessType[$oldModel->class] : 'member'; $model->accessLevel = isset($accessLevel[$oldModel->class]) ? $accessLevel[$oldModel->class] : 1; $model->isActive = $isActive ? '1' : '0'; $model->createTime = strtotime($oldModel->last); $model->id = $oldModel->id; if ($model->save()) { $i++; if (($userDetails = UserDetails::model()->findByPk($model->id)) !== null) { $userDetails->delete(); } $userDetails = new UserDetails('migrate'); $userDetails->emailConfirmationKey = md5(uniqid(rand(), true)); $userDetails->initials = $oldModel->inits; $userDetails->occupation = $oldModel->title; $userDetails->deactivationTime = $isActive ? null : $closeTime; $userDetails->administratorNote = '[from migration]'; $userDetails->userId = $model->id; $userDetails->save(); // relation between user and company if ($oldModel->customer_id >= 1) { $user2Company = new User2Company('migrate'); $user2Company->userId = $model->id; $user2Company->companyId = $oldModel->customer_id; $user2Company->position = 'owner'; //$oldModel->title if ($user2Company->save()) { $c++; } } } $j++; } $message .= $i . ' of ' . $j . ' users' . ($i === $c ? '' : ' with ' . $c . ' company (relations)') . ' have been migrated.<br/>'; } } if ($migrate['Company']) { // company $mCustomers = MCustomer::model()->findAll($findAllCriteria); if (is_array($mCustomers)) { $i = $j = $l = 0; foreach ($mCustomers as $oldModel) { if (($model = Company::model()->findByPk($oldModel->id)) !== null) { $model->delete(); } $closeTime = strtotime($oldModel->close_date); $isActive = empty($oldModel->close_date) || $oldModel->close_date === '0000-00-00' || $closeTime === false; $model = new Company('migrate'); $model->title = $oldModel->name; $model->titleAbbr = $oldModel->inits; $model->contactName = $oldModel->contact; $model->contactEmail = $oldModel->contact_email; $model->content = '[from migration]'; $model->contentMarkup = 'text'; $model->invoiceDueDay = $oldModel->terms_days; $model->isActive = $isActive ? '1' : '0'; $model->deactivationTime = $isActive ? null : $closeTime; $model->createTime = strtotime($oldModel->last); $model->id = $oldModel->id; if ($model->save()) { $i++; // associated location $location = new Location('migrate'); $location->address1 = $oldModel->addr; $location->address2 = $oldModel->addr2; $location->city = $oldModel->city; $location->state = $oldModel->state; $location->zipcode = $oldModel->zip; $location->content = '[from migration]'; $location->contentMarkup = 'text'; $location->createTime = strtotime($oldModel->last); if ($location->save()) { // relation between company and location $location2Record = new Location2Record('migrate'); $location2Record->locationId = $location->id; $location2Record->record = get_class($model); $location2Record->recordId = $model->id; if ($location2Record->save()) { $l++; } } } $j++; } $message .= $i . ' of ' . $j . ' companies' . ($i === $l ? '' : ' with ' . $l . ' locations') . ' have been migrated.<br/>'; } } if ($migrate['CompanyPayment']) { // company payment $mCustomerPayments = MCustomerPayment::model()->findAll($findAllCriteria); if (is_array($mCustomerPayments)) { $i = $j = 0; $paymentMethod = array('cash' => 'cash', 'check' => 'check', 'credit card' => 'creditCard'); foreach ($mCustomerPayments as $oldModel) { if (($model = CompanyPayment::model()->findByPk($oldModel->id)) !== null) { $model->delete(); } $model = new CompanyPayment('migrate'); $model->companyId = $oldModel->id; $model->paymentDate = MDate::formatToDb($oldModel->payment_date, 'date'); $model->amount = $oldModel->amount; $model->paymentMethod = $paymentMethod[$oldModel->payment_method]; $model->paymentNumber = $oldModel->payment_number; $model->content = $oldModel->note . "\n" . '[from migration]'; $model->contentMarkup = 'text'; $model->id = $oldModel->id; if ($model->save()) { $i++; } $j++; } $message .= $i . ' of ' . $j . ' company payments have been migrated.<br/>'; } } if ($migrate['Project']) { // project $mProjects = MProject::model()->findAll($findAllCriteria); if (is_array($mProjects)) { $i = $j = $c = $u = 0; foreach ($mProjects as $oldModel) { if (($model = Project::model()->findByPk($oldModel->id)) !== null) { $model->delete(); } $openDateNotSet = empty($oldModel->open_date) || $oldModel->open_date === '0000-00-00' || strtotime($oldModel->open_date) === false; $closeDateNotSet = empty($oldModel->close_date) || $oldModel->close_date === '0000-00-00' || strtotime($oldModel->close_date) === false; $model = new Project('migrate'); $model->title = $oldModel->name; $model->content = $oldModel->description . "\n" . '[from migration]'; $model->contentMarkup = 'text'; $model->hourlyRate = $oldModel->rate; $model->openDate = $openDateNotSet ? null : MDate::formatToDb($oldModel->open_date, 'date'); $model->closeDate = $closeDateNotSet ? null : MDate::formatToDb($oldModel->close_date, 'date'); $model->createTime = strtotime($oldModel->last); $model->id = $oldModel->id; if ($model->save()) { $i++; // relation between project and company if ($oldModel->customer_id >= 1) { $company2Project = new Company2Project('migrate'); $company2Project->companyId = $oldModel->customer_id; $company2Project->projectId = $model->id; if ($company2Project->save()) { $c++; } } // relation between project and manager if ($oldModel->manager_id >= 1) { $user2Project = new User2Project('migrate'); $user2Project->userId = $oldModel->manager_id; $user2Project->projectId = $model->id; $user2Project->role = 'manager'; if ($user2Project->save()) { $u++; } } } $j++; } $message .= $i . ' of ' . $j . ' projects' . ($i === $c ? '' : ' with ' . $c . ' company (relations)') . ($i === $u ? '' : ' with ' . $u . ' manager (relations)') . ' have been migrated.<br/>'; } } if ($migrate['Task']) { // task $mTasks = MTask::model()->findAll($findAllCriteria); if (is_array($mTasks)) { $i = $j = $u = $m = 0; $priority = array('A' => 2, 'B' => 3, 'C' => 4, '' => 3); $status = array('' => 'completed', 'Open' => 'completed', '0' => 'notStarted', 'New' => 'notStarted', 'Done' => 'completed', 'In Progress' => 'inProgress', 'Ready to Test' => 'readyToTest'); foreach ($mTasks as $oldModel) { if (($model = Task::model()->findByPk($oldModel->id)) !== null) { $model->delete(); } $hourlyRate = null; if ($oldModel->project_id >= 1 && ($project = Project::model()->findByPk($oldModel->project_id)) !== null) { $hourlyRate = $project->hourlyRate; } $dueDateNotSet = empty($oldModel->due_date) || $oldModel->due_date === '0000-00-00' || strtotime($oldModel->due_date) === false; $openDateNotSet = empty($oldModel->open_date) || $oldModel->open_date === '0000-00-00' || strtotime($oldModel->open_date) === false; $closeDateNotSet = empty($oldModel->close_date) || $oldModel->close_date === '0000-00-00' || strtotime($oldModel->close_date) === false; $model = new Task('migrate'); $model->title = $oldModel->name; $model->content = $oldModel->description . "\n" . '[from migration]'; $model->contentMarkup = 'text'; $model->companyId = $oldModel->customer_id; $model->projectId = $oldModel->project_id; $model->estimateMinute = (int) $oldModel->hours_estimate * 60; $model->dueDate = $dueDateNotSet ? null : MDate::formatToDb($oldModel->due_date, 'date'); $model->priority = $priority[$oldModel->priority]; $model->openDate = $openDateNotSet ? null : MDate::formatToDb($oldModel->open_date, 'date'); $model->closeDate = $closeDateNotSet ? null : MDate::formatToDb($oldModel->close_date, 'date'); $model->status = $status[$oldModel->task_status]; $model->report = $oldModel->work_report; $model->reportMarkup = 'text'; $model->hourlyRate = $hourlyRate; $model->isConfirmed = '1'; $model->confirmationTime = strtotime($oldModel->last); $model->createTime = strtotime($oldModel->last); $model->id = $oldModel->id; if ($model->save()) { $i++; // relation between task and consultant if ($oldModel->leader_id >= 1) { $user2Task = new User2Task('migrate'); $user2Task->userId = $oldModel->leader_id; $user2Task->taskId = $model->id; $user2Task->role = User2Task::CONSULTANT; if ($user2Task->save()) { $u++; } } // relation between task and manager if ($model->projectId >= 1) { $criteria = new CDbCriteria(); $criteria->order = "`" . User2Project::model()->tableName() . "`.`userPriority` ASC"; $criteria->order .= ",`" . User2Project::model()->tableName() . "`.`id` ASC"; if (($user2Project = User2Project::model()->findByAttributes(array('projectId' => $model->projectId, 'role' => 'manager'), $criteria)) !== null) { $user2Task = new User2Task('migrate'); $user2Task->userId = $user2Project->userId; $user2Task->taskId = $model->id; $user2Task->role = 'manager'; if ($user2Task->save()) { $m++; } } } } $j++; } $message .= $i . ' of ' . $j . ' tasks' . ($i === $u ? '' : ' with ' . $u . ' consultant (relations)') . ($i === $m ? '' : ' with ' . $m . ' manager (relations)') . ' have been migrated.<br/>'; } } if ($migrate['Time']) { // time $mTime = MTime::model()->findAll($findAllCriteria); if (is_array($mTime)) { $i = $j = $t = 0; foreach ($mTime as $oldModel) { if (($model = Time::model()->findByPk($oldModel->id)) !== null) { $model->delete(); } $taskId = $oldModel->task_id; if (empty($taskId) && $oldModel->project_id >= 1) { $criteria = new CDbCriteria(); $criteria->order = "`" . Task::model()->tableName() . "`.`id` ASC"; if (($task = Task::model()->findByAttributes(array('projectId' => $oldModel->project_id), $criteria)) !== null) { $taskId = $task->id; } else { // auto-generate a task $companyId = 0; $criteria = new CDbCriteria(); $criteria->order = "`" . Company2Project::model()->tableName() . "`.`companyPriority` ASC"; $criteria->order .= ", `" . Company2Project::model()->tableName() . "`.`id` ASC"; if (($company2Project = Company2Project::model()->findByAttributes(array('projectId' => $oldModel->project_id), $criteria)) !== null) { $companyId = $company2Project->companyId; } $hourlyRate = null; if (($project = Project::model()->findByPk($oldModel->project_id)) !== null) { $hourlyRate = $project->hourlyRate; } $task = new Task('migrate'); $task->projectId = $oldModel->project_id; $task->companyId = $companyId; $task->title = '[Auto Generated]'; $task->status = 'completed'; $task->dueDate = MDate::formatToDb($project === null ? 1234567890 : $project->createTime, 'date'); $task->openDate = MDate::formatToDb($project === null ? 1234567890 : $project->createTime, 'date'); $task->closeDate = MDate::formatToDb($project === null ? 1234567890 : $project->createTime, 'date'); $task->hourlyRate = $hourlyRate; $task->isConfirmed = 1; $task->confirmationTime = $project === null ? 1234567890 : $project->createTime; if ($task->save()) { $t++; $taskId = $task->id; // assigned consultant if ($oldModel->user_id >= 1) { $consultant2Task = new User2Task('migrate'); $consultant2Task->userId = $oldModel->user_id; $consultant2Task->taskId = $task->id; $consultant2Task->role = User2Task::CONSULTANT; $consultant2Task->save(); } // assigned manager $criteria = new CDbCriteria(); $criteria->order = "`" . User2Project::model()->tableName() . "`.`userPriority` ASC"; $criteria->order .= ", `" . User2Project::model()->tableName() . "`.`id` ASC"; if (($user2Project = User2Project::model()->findByAttributes(array('projectId' => $oldModel->project_id, 'role' => 'manager'), $criteria)) !== null) { $manager2Task = new User2Task('migrate'); $manager2Task->userId = $user2Project->userId; $manager2Task->taskId = $task->id; $manager2Task->role = User2Task::MANAGER; $manager2Task->save(); } } } } $managerId = null; if (!empty($taskId)) { $criteria = new CDbCriteria(); $criteria->order = "`" . User2Task::model()->tableName() . "`.`userPriority` ASC"; $criteria->order .= ",`" . User2Task::model()->tableName() . "`.`id` ASC"; if (($user2Task = User2Task::model()->findByAttributes(array('taskId' => $taskId, 'role' => 'manager'), $criteria)) !== null) { $managerId = $user2Task->userId; } } $timeDateNotSet = empty($oldModel->time_date) || $oldModel->time_date === '0000-00-00' || strtotime($oldModel->time_date) === false; $model = new Time('migrate'); $model->consultantId = $oldModel->user_id; $model->taskId = $taskId; $model->spentMinute = (int) $oldModel->hours_spent * 60; $model->timeDate = $timeDateNotSet ? null : MDate::formatToDb($oldModel->time_date, 'date'); $model->title = $oldModel->note; $model->content = $oldModel->details . "\n" . '[from migration]'; $model->contentMarkup = 'text'; $model->managerId = $managerId; $model->billedMinute = (int) $oldModel->hours_billed * 60; $model->invoiceId = $oldModel->invoice_id; $model->invoiceAmount = $oldModel->invoice_amount; $model->isConfirmed = '1'; $model->confirmationTime = strtotime($oldModel->last); $model->createTime = strtotime($oldModel->last); $model->id = $oldModel->id; if ($model->save()) { $i++; } $j++; } $message .= $i . ' of ' . $j . ' time records' . ($i === $t ? '' : ' with ' . $t . ' tasks') . ' have been migrated.<br/>'; } } if ($migrate['Invoice']) { // invoice $mInvoices = MInvoice::model()->findAll($findAllCriteria); if (is_array($mInvoices)) { $i = $j = 0; foreach ($mInvoices as $oldModel) { if (($model = Invoice::model()->findByPk($oldModel->id)) !== null) { $model->delete(); } $invoiceDateNotSet = empty($oldModel->invoice_date) || $oldModel->invoice_date === '0000-00-00' || strtotime($oldModel->invoice_date) === false; $startDateNotSet = empty($oldModel->start_date) || $oldModel->start_date === '0000-00-00' || strtotime($oldModel->start_date) === false; $endDateNotSet = empty($oldModel->end_date) || $oldModel->end_date === '0000-00-00' || strtotime($oldModel->end_date) === false; $dueDateNotSet = empty($oldModel->due_date) || $oldModel->due_date === '0000-00-00' || strtotime($oldModel->due_date) === false; $model = new Invoice('migrate'); $model->invoiceDate = $invoiceDateNotSet ? null : MDate::formatToDb($oldModel->invoice_date, 'date'); $model->companyId = $oldModel->customer_id; $model->billedMinute = (int) $oldModel->hours_billed * 60; $model->amountTotal = number_format($oldModel->total, 2, '.', ''); $model->startDate = $startDateNotSet ? null : MDate::formatToDb($oldModel->start_date, 'date'); $model->endDate = $endDateNotSet ? null : MDate::formatToDb($oldModel->end_date, 'date'); $model->dueDate = $dueDateNotSet ? null : MDate::formatToDb($oldModel->due_date, 'date'); $model->amountTime = number_format($oldModel->total, 2, '.', ''); $model->amountExpense = 0; $model->content = '[from migration]'; $model->contentMarkup = 'text'; $model->createTime = $invoiceDateNotSet ? null : strtotime($oldModel->invoice_date); $model->id = $oldModel->id; if ($model->save()) { $i++; } $j++; } $message .= $i . ' of ' . $j . ' invoices have been migrated.<br/>'; } } if ($migrate['Expense']) { // expense $mExpenses = MExpense::model()->findAll($findAllCriteria); if (is_array($mExpenses)) { $i = $j = 0; $billToCompany = array('Yes' => '1', 'No' => '0'); foreach ($mExpenses as $oldModel) { if (($model = Expense::model()->findByPk($oldModel->id)) !== null) { $model->delete(); } $expenseDateNotSet = empty($oldModel->expense_date) || $oldModel->expense_date === '0000-00-00' || strtotime($oldModel->expense_date) === false; $model = new Expense('migrate'); $model->managerId = $oldModel->user_id; $model->companyId = $oldModel->customer_id; $model->projectId = $oldModel->project_id; $model->invoiceId = $oldModel->invoice_id; $model->expenseDate = $expenseDateNotSet ? null : MDate::formatToDb($oldModel->expense_date, 'date'); $model->amount = $oldModel->amount; $model->billToCompany = $billToCompany[$oldModel->bill_to_customer]; $model->content = $oldModel->note . "\n" . '[from migration]'; $model->contentMarkup = 'text'; $model->createTime = strtotime($oldModel->last); $model->id = $oldModel->id; if ($model->save()) { $i++; } $j++; } $message .= $i . ' of ' . $j . ' expenses have been migrated.<br/>'; } } // last message line $message .= 'done'; } $this->render($this->action->id, array('message' => $message)); }