public static function setGridviewSettings($gvSettings, $viewName = null) { if (isset($viewName)) { $fullGvSettings = ProfileChild::getGridviewSettings(); $fullGvSettings[strtolower($viewName)] = $gvSettings; Yii::app()->params->profile->gridviewSettings = json_encode($fullGvSettings); // encode array in JSON } else { Yii::app()->params->profile->gridviewSettings = json_encode($gvSettings); // encode array in JSON } return Yii::app()->params->profile->save(); }
public function init() { // $this->selectionChanged = 'js:function() { console.debug($.fn.yiiGridView.getSelection("'.$this->id.'")); }'; // if(empty($this->modelName)) // $this->modelName = $this->getId(); if (empty($this->viewName)) { $this->viewName = $this->modelName; } if ($this->modelName == 'Quotes') { $this->modelName = 'Quote'; } $this->columnSelectorId = $this->getId() . '-column-selector'; if (isset($_GET['gvSettings']) && isset($_GET['viewName']) && $_GET['viewName'] == $this->viewName) { $this->gvSettings = json_decode($_GET['gvSettings'], true); // unset($_GET['gvSettings']); // die(var_dump($this->gvSettings)); ProfileChild::setGridviewSettings($this->gvSettings, $this->viewName); } else { $this->gvSettings = ProfileChild::getGridviewSettings($this->viewName); } if ($this->gvSettings == null) { $this->gvSettings = $this->defaultGvSettings; } // die(var_dump($this->gvSettings)); // die(var_dump(ProfileChild::getGridviewSettings($this->viewName))); // load names from $specialColumns into $specialColumnNames foreach ($this->specialColumns as $columnName => &$columnData) { if (isset($columnData['header'])) { $this->specialColumnNames[$columnName] = $columnData['header']; } else { $this->specialColumnNames[$columnName] = CActiveRecord::model($this->modelName)->getAttributeLabel($columnName); } } // start allFieldNames with the special fields if (!empty($this->specialColumnNames)) { $this->allFieldNames = $this->specialColumnNames; } // add controls column if specified if ($this->enableControls) { $this->allFieldNames['gvControls'] = Yii::t('app', 'Tools'); } $this->allFieldNames['gvCheckbox'] = Yii::t('app', 'Checkbox'); // load fields from DB // $fields=Fields::model()->findAllByAttributes(array('modelName'=>ucwords($this->modelName))); $fields = CActiveRecord::model($this->modelName)->getFields(); foreach ($fields as $field) { $this->allFields[$field->fieldName] = $field; } // add tags column if specified if ($this->enableTags) { $this->allFieldNames['tags'] = Yii::t('app', 'Tags'); } foreach ($this->allFields as $fieldName => &$field) { $this->allFieldNames[$fieldName] = CActiveRecord::model($this->modelName)->getAttributeLabel($field->fieldName); } // update columns if user has submitted data if (isset($_GET['columns']) && isset($_GET['viewName']) && $_GET['viewName'] == $this->viewName) { // has the user changed column visibility? foreach (array_keys($this->gvSettings) as $key) { $index = array_search($key, $_GET['columns']); // search $_GET['columns'] for the column if ($index === false) { // if it's not in there, unset($this->gvSettings[$key]); } else { // othwerise, remove it from $_GET['columns'] unset($_GET['columns'][$index]); } // so the next part doesn't add it a second time } foreach (array_keys($this->allFieldNames) as $key) { // now go through $allFieldNames and add any fields that if (!isset($this->gvSettings[$key]) && in_array($key, $_GET['columns'])) { // are present in $_GET['columns'] but not already in the list $this->gvSettings[$key] = 80; } // default width of 80 } } unset($_GET['columns']); // prevents columns data from ending up in sort/pagination links unset($_GET['viewName']); unset($_GET['gvSettings']); // adding/removing columns changes the total width, // so let's scale the columns to match the correct total (590px) $totalWidth = array_sum(array_values($this->gvSettings)); if ($totalWidth > 0) { $widthFactor = 585 / $totalWidth; //- count($this->gvSettings) $sum = 0; $scaledSum = 0; foreach ($this->gvSettings as $columnName => &$columnWidth) { $sum += $columnWidth; $columnWidth = round($sum * $widthFactor) - $scaledSum; // map each point onto the nearest integer in the scaled space $scaledSum += $columnWidth; } } // die(var_dump($this->gvSettings).' '.$this->viewName); ProfileChild::setGridviewSettings($this->gvSettings, $this->viewName); // save the new Gridview Settings // die(var_dump($this->gvSettings)); $columns = array(); $datePickerJs = ''; foreach ($this->gvSettings as $columnName => $width) { // $width = (!empty($width) && is_numeric($width))? 'width:'.$width.'px;' : null; // make sure width is reasonable, then convert it to CSS $width = !empty($width) && is_numeric($width) ? $width : null; // make sure width is reasonable // $isDate = in_array($columnName,array('createDate','completeDate','lastUpdated','dueDate', 'expectedCloseDate', 'expirationDate', 'timestamp','lastactivity')); $isCurrency = in_array($columnName, array('annualRevenue', 'quoteAmount')); $lang = Yii::app()->language == 'en' ? '' : Yii::app()->getLanguage(); //if($isDate) //$datePickerJs .= ' $("#'.$columnName.'DatePicker").datepicker(' //.'$.extend({showMonthAfterYear:false}, {"dateFormat":"'.$this->controller->formatDatePicker().'"})); '; // .'{"showAnim":"fold","dateFormat":"yy-mm-dd","changeMonth":"true","showButtonPanel":"true","changeYear":"true","constrainInput":"false"}));'; $newColumn = array(); if (array_key_exists($columnName, $this->specialColumnNames)) { $newColumn = $this->specialColumns[$columnName]; // $newColumn['name'] = 'lastName'; $newColumn['id'] = 'C_' . $columnName; // $newColumn['header'] = Yii::t('contacts','Name'); $newColumn['headerHtmlOptions'] = array('colWidth' => $width); // $newColumn['value'] = 'CHtml::link($data->firstName." ".$data->lastName,array("view","id"=>$data->id))'; // $newColumn['type'] = 'raw'; // die(print_r($newColumn)); $columns[] = $newColumn; } else { if (array_key_exists($columnName, $this->allFields)) { // && $this->allFields[$columnName]->visible == 1)) { $newColumn['name'] = $columnName; $newColumn['id'] = 'C_' . $columnName; $newColumn['header'] = CActiveRecord::model($this->modelName)->getAttributeLabel($columnName); $newColumn['headerHtmlOptions'] = array('colWidth' => $width); if ($isCurrency) { $newColumn['value'] = 'Yii::app()->locale->numberFormatter->formatCurrency($data->' . $columnName . ',Yii::app()->params->currency)'; $newColumn['type'] = 'raw'; } else { if ($columnName == 'assignedTo') { $newColumn['value'] = 'empty($data->assignedTo)?Yii::t("app","Anyone"):UserChild::getUserLinks($data->assignedTo)'; $newColumn['type'] = 'raw'; } elseif ($this->allFields[$columnName]->type == 'date') { $newColumn['value'] = 'empty($data["' . $columnName . '"])? "" : Yii::app()->dateFormatter->format(Yii::app()->locale->getDateFormat("medium"), $data["' . $columnName . '"])'; } elseif ($this->allFields[$columnName]->type == 'link') { $newColumn['value'] = 'X2Model::getModelLink($data->' . $columnName . ',"' . $this->allFields[$columnName]->linkType . '")'; // $type=ucfirst($field->linkType); // $newColumn['value']="!is_null($type::model()->findByPk(\$data->$columnName))?CHtml::link($type::model()->findByPk(\$data->$columnName)->name,array('/".$field->linkType."/default/view/id/'.\$data->$columnName),array('target'=>'_blank')):\$data->$columnName"; $newColumn['type'] = 'raw'; } elseif ($this->allFields[$columnName]->type == 'boolean') { $field = $this->allFields[$columnName]; $type = ucfirst($field->linkType); $newColumn['value'] = '$data->' . $columnName . '==1?Yii::t("actions","Yes"):Yii::t("actions","No")'; $newColumn['type'] = 'raw'; } } if (Yii::app()->language == 'en') { $format = "M d, yy"; } else { $format = Yii::app()->locale->getDateFormat('medium'); // translate Yii date format to jquery $format = str_replace('yy', 'y', $format); $format = str_replace('MM', 'mm', $format); $format = str_replace('M', 'm', $format); } /*$newColumn['filter'] = $isDate? $this->widget("zii.widgets.jui.CJuiDatePicker",array( 'model'=>$this->filter, //Model object // 'id'=>$columnName.'DatePicker', 'attribute'=>$columnName, //attribute name // 'mode'=>'datetime', //use 'time','date' or 'datetime' (default) // 'htmlOptions'=>array('style'=>'width:80%;'), 'options'=>array( 'dateFormat'=>$format, ), // jquery plugin options 'language'=>$lang, ),true) : null;*/ $columns[] = $newColumn; } else { if ($columnName == 'gvControls') { $newColumn['id'] = 'C_gvControls'; $newColumn['class'] = 'CButtonColumn'; $newColumn['header'] = Yii::t('app', 'Tools'); $newColumn['headerHtmlOptions'] = array('colWidth' => $width); if (Yii::app()->user->getName() != 'admin') { $newColumn['template'] = '{view}{update}'; } $columns[] = $newColumn; } else { if ($columnName == 'tags') { $newColumn['id'] = 'C_' . 'tags'; // $newColumn['class'] = 'CDataColumn'; $newColumn['header'] = Yii::t('app', 'Tags'); $newColumn['headerHtmlOptions'] = array('colWidth' => $width); $newColumn['value'] = 'Tags::getTagLinks("' . $this->modelName . '",$data->id,2)'; $newColumn['type'] = 'raw'; $newColumn['filter'] = CHtml::textField('tagField', isset($_GET['tagField']) ? $_GET['tagField'] : ''); $columns[] = $newColumn; } else { if ($columnName == 'gvCheckbox') { $newColumn['id'] = 'C_gvCheckbox'; $newColumn['class'] = 'CCheckBoxColumn'; $newColumn['selectableRows'] = 2; $newColumn['headerHtmlOptions'] = array('colWidth' => $width); $columns[] = $newColumn; } } } } } } // $this->afterAjaxUpdate = 'function(id, data) { '.$datePickerJs.' }'; if (!empty($this->afterAjaxUpdate)) { $this->afterAjaxUpdate = "var callback = " . $this->afterAjaxUpdate . "; if(typeof callback == 'function') callback();"; } $this->afterAjaxUpdate = " function(id,data) { " . $this->afterAjaxUpdate . " " . $datePickerJs; if ($this->enableGvSettings) { $this->afterAjaxUpdate .= "\n\t\t\t\$('#" . $this->getId() . " table').gvSettings({\n\t\t\t\tviewName:'" . $this->viewName . "',\n\t\t\t\tcolumnSelectorId:'" . $this->columnSelectorId . "',\n\t\t\t\tajaxUpdate:true\n\t\t\t});"; } $this->afterAjaxUpdate .= " } "; $this->columns = $columns; natcasesort($this->allFieldNames); // sort column names // generate column selector HTML $this->columnSelectorHtml = CHtml::beginForm(array('site/saveGvSettings'), 'get') . '<ul class="column-selector" id="' . $this->columnSelectorId . '">'; foreach ($this->allFieldNames as $fieldName => &$attributeLabel) { $selected = array_key_exists($fieldName, $this->gvSettings); $this->columnSelectorHtml .= "<li>" . CHtml::checkbox('columns[]', $selected, array('id' => $fieldName . '_checkbox', 'value' => $fieldName)) . CHtml::label($attributeLabel, $fieldName . '_checkbox') . "</li>"; } $this->columnSelectorHtml .= '</ul>' . CHtml::endForm(); // Yii::app()->clientScript->renderBodyBegin($columnHtml); // Yii::app()->clientScript->registerScript(__CLASS__.'#'.$this->getId().'_columnSelector', // "$('#".$this->getId()." table').after('".addcslashes($columnHtml,"'")."'); // ",CClientScript::POS_READY); $themeURL = Yii::app()->theme->getBaseUrl(); Yii::app()->clientScript->registerScript('logos', "\n\t\t\$(window).load(function(){\n\t\t\tif((!\$('#main-menu-icon').length) || (!\$('#x2touch-logo').length) || (!\$('#x2crm-logo').length)){\n\t\t\t\t\$('a').removeAttr('href');\n\t\t\t\talert('Please put the logo back');\n\t\t\t\twindow.location='http://www.x2engine.com';\n\t\t\t}\n\t\t\tvar touchlogosrc = \$('#x2touch-logo').attr('src');\n\t\t\tvar logosrc=\$('#x2crm-logo').attr('src');\n\t\t\tif(logosrc!='{$themeURL}/images/x2footer.png'|| touchlogosrc!='{$themeURL}/images/x2touch.png'){\n\t\t\t\t\$('a').removeAttr('href');\n\t\t\t\talert('Please put the logo back');\n\t\t\t\twindow.location='http://www.x2engine.com';\n\t\t\t}\n\t\t}); \n\t\t"); parent::init(); }
public function actionExportList($id) { $list = CActiveRecord::model('X2List')->findByPk($id); if (isset($list)) { if (!$this->checkPermissions($list, 'view')) { // check permissions throw new CHttpException(403, Yii::t('app', 'You do not have permission to modify this list.')); } } else { throw new CHttpException(404, Yii::t('app', 'The requested list does not exist.')); } $dataProvider = CActiveRecord::model('Contacts')->searchList($id); // get the list $totalItemCount = $dataProvider->getTotalItemCount(); $dataProvider->pagination->itemCount = $totalItemCount; $dataProvider->pagination->pageSize = 1000; // process list in blocks of 1000 $allFields = CActiveRecord::model('Contacts')->getFields(true); // get associative array of fields $gvSettings = ProfileChild::getGridviewSettings('contacts_list' . $id); $selectedColumns = array(); $columns = array(); if ($gvSettings === null) { $selectedColumns = array('firstName', 'lastName', 'phone', 'email', 'leadSource', 'createDate', 'lastUpdated'); } else { $selectedColumns = array_keys($gvSettings); } foreach ($selectedColumns as &$colName) { if ($colName == 'tags') { $columns[$colName]['label'] = Yii::t('app', 'Tags'); $columns[$colName]['type'] = 'tags'; } elseif ($colName == 'name') { $columns[$colName]['label'] = Yii::t('contacts', 'Name'); $columns[$colName]['type'] = 'name'; } else { if (array_key_exists($colName, $allFields)) { $columns[$colName]['label'] = $allFields[$colName]['attributeLabel']; if (in_array($colName, array('annualRevenue', 'quoteAmount'))) { $columns[$colName]['type'] = 'currency'; } else { $columns[$colName]['type'] = $allFields[$colName]['type']; } $columns[$colName]['linkType'] = $allFields[$colName]['linkType']; } } } unset($colName); $fileName = 'list' . $id . '.csv'; $fp = fopen($fileName, 'w+'); // output column labels for the first line $columnLabels = array(); foreach ($columns as $colName => &$field) { $columnLabels[] = $field['label']; } unset($field); fputcsv($fp, $columnLabels); for ($i = 0; $i < $dataProvider->pagination->pageCount; ++$i) { $dataProvider->pagination->currentPage = $i; $dataSet = $dataProvider->getData(true); foreach ($dataSet as &$model) { $row = array(); foreach ($columns as $fieldName => &$field) { if ($field['type'] == 'tags') { $row[] = Tags::getTags('Contacts', $model->id, 10); } elseif ($field['type'] == 'date') { $row[] = date('Y-m-d H:i:s', $model->{$fieldName}); } elseif ($field['type'] == 'visibility') { switch ($model->{$fieldName}) { case '1': $row[] = Yii::t('app', 'Public'); break; case '0': $row[] = Yii::t('app', 'Private'); break; case '2': $row[] = Yii::t('app', 'User\'s Groups'); break; } } elseif ($field['type'] == 'link') { if (is_numeric($model->{$fieldName})) { $className = ucfirst($field['linkType']); if (class_exists($className)) { $lookupModel = CActiveRecord::model($className)->findByPk($model->{$fieldName}); if (isset($lookupModel)) { $row[] = $lookupModel->name; } } } else { $row[] = $model->{$fieldName}; } } elseif ($field['type'] == 'currency') { if ($model instanceof Product) { // products have their own currency $row[] = Yii::app()->locale->numberFormatter->formatCurrency($model->{$fieldName}, $model->currency); } elseif (!empty($model->{$fieldName})) { $row[] = Yii::app()->locale->numberFormatter->formatCurrency($model->{$fieldName}, Yii::app()->params['currency']); } else { $row[] = ''; } } elseif ($field['type'] == 'dropdown') { $row[] = Yii::t(strtolower(Yii::app()->controller->id), $model->{$fieldName}); } else { $row[] = $model->{$fieldName}; } } fputcsv($fp, $row); } unset($model); } fclose($fp); $file = Yii::app()->file->set($fileName); $file->download(); }