function _targetMode_NodeRef(&$eventOptions, $params) { if (array_key_exists('target', $params) && array_key_exists('field', $params)) { $localOpt = array('target', 'modelName'); $defParams = array('target' => null, 'modelName' => false, 'field' => null); $opt = array_merge($defParams, $params); $this->Node = ClassRegistry::init('Node'); $nOpt = array('fields' => array('foreign_key', 'model')); if (!empty($opt['modelName'])) { $nOpt['conditions']['Node.model'] = $opt['modelName']; } $nodes = $this->Node->getNodes($opt['target'], $nOpt); if ($opt['field'] == 'id') { return Set::extract('{n}.Node.foreign_key'); } else { App::import('Lib', 'SetMulti'); $nodes = SetMulti::group($nodes, 'Node.model', array('valPath' => 'Node.foreign_key')); $targets = array(); foreach ($nodes as $model => $keys) { $subOpt = array_diff_key($opt, array_flip($localOpt)); $subOpt['target'] = $keys; $subOpt['modelName'] = $model; $targets = array_merge($targets, $this->_targetMode_ForeignKey($eventOptions, $subOpt)); } return $targets; } } return array(); }
function extractSubItemData($productAndOptions, $order = null) { //debug($productAndOptions); $currency = !empty($order['ShopOrder']['currency']) ? $order['ShopOrder']['currency'] : ShopConfig::load('currency'); $extract_data = array('shop_product_subproduct_id' => 'ShopProductSubproduct.id', 'shop_subproduct_id' => 'ShopSubproduct.id', 'descr' => array('ShopSubproduct.label'), 'nb' => array('nb'), 'id' => array('id'), 'item_price' => array('ShopSubproduct.currency_prices.' . $currency, 'ShopSubproduct.price'), 'item_operator' => array('ShopSubproduct.operator'), 'type' => 'ShopSubproduct.type', 'ShopSubproduct' => 'ShopSubproduct'); if (!empty($order['ShopOrder']['confirm'])) { $extract_data = Set::merge(array('descr' => array('descr'), 'item_operator' => array('item_operator'), 'item_price' => array('item_price')), $extract_data); } $orderItemMode = isset($productAndOptions['item_price']); if ($orderItemMode) { $p =& $productAndOptions; } else { $p = $this->extractOrderItemData($p2 = $productAndOptions, $order); } $cur_price = $p['item_price']; $p['item_alone_price'] = $cur_price; App::import('Lib', 'Shop.ShopConfig'); $types = ShopConfig::getSubProductTypes(); $subItems = null; if (!empty($types) && !empty($p['SubItem'])) { $this->ShopSubproduct = ClassRegistry::init('Shop.ShopSubproduct'); $ids = array(); //============ normalize data ============// $subItems = $this->_getNormalizedSubItem($types, $p['SubItem'], $ids); //debug($ids); //============ fetch SubProducts ============// $this->ShopSubproduct->ShopProductSubproduct->Behaviors->attach('Containable'); $this->ShopSubproduct->ShopProductSubproduct->contain(array('ShopSubproduct')); $subProduct = $this->ShopSubproduct->ShopProductSubproduct->find('all', array('conditions' => array('ShopProductSubproduct.shop_product_id' => $p['product_id'], 'ShopProductSubproduct.shop_subproduct_id' => $ids))); $subProduct = SetMulti::group($subProduct, 'ShopSubproduct.id', array('singleArray' => false)); //debug($subProduct); foreach ($subItems as $type => &$items) { foreach ($items as &$subItem) { if (!empty($subProduct[$subItem['id']])) { $subItem = array_merge($subProduct[$subItem['id']], $subItem); } unset($subItem); } } } if (!empty($productAndOptions['ShopOrdersSubitem'])) { $subItems = SetMulti::group($productAndOptions['ShopOrdersSubitem'], 'ShopSubproduct.type'); } if (!empty($types) && !empty($subItems)) { //============ merge data ============// $finalSubItems = array(); foreach ($subItems as $type => &$items) { foreach ($items as &$subItem) { $data = SetMulti::extractHierarchicMulti($extract_data, $subItem); if (!empty($subItem['parent'])) { $data['parent'] =& $subItem['parent']; } SetMulti::replaceRef($subItem, $data); $finalSubItems[] =& $subItem; unset($subItem); } unset($items); } //debug($finalSubItems); return $finalSubItems; } return null; }
function getIneritedFields(&$model, $data, $options = array()) { $defOpt = array('fields' => null, 'fetchAssociations' => true); $opt = array_merge($defOpt, $options); //debug($opt); if (!empty($this->settings[$model->alias]['fields']) && is_array($data)) { App::import('Lib', 'SetMulti'); if (isset($data[$model->alias])) { $myData =& $data[$model->alias]; } else { $myData =& $data; } $originalData = $myData; $fieldsOpt = $this->_formatFieldsOpt($this->settings[$model->alias]['fields']); if (!empty($opt['fields'])) { $fieldsOpt = array_intersect_key($fieldsOpt, array_flip($opt['fields'])); } $fieldsOpt = array_intersect_key($fieldsOpt, array_merge(array_filter($myData, 'is_null'), array_filter($myData, 'is_array'))); if (!empty($fieldsOpt)) { $fieldsOpt = SetMulti::group($fieldsOpt, 'alias'); //debug($fieldsOpt); foreach ($fieldsOpt as $alias => $iopt) { $hdata = null; //debug($model->{$alias}->name ."==".$model->name); if (isset($model->{$alias}) && $model->{$alias}->name == $model->name && $model->Behaviors->attached('Tree')) { //tree inerit $d = $myData; if (!isset($d['lft']) || !isset($d['rght'])) { $d = $model->read(null, $myData['id']); } $model->recursive = -1; $tmp = $this->settings[$model->alias]['fetch']; $this->settings[$model->alias]['fetch'] = false; $res = $model->find('all', array('fields' => array_merge(array('id'), array_keys($iopt)), 'conditions' => array('lft' > $d['lft'], 'rght' < $d['rght']), 'order' => 'lft DESC')); //debug($res); $this->settings[$model->alias]['fetch'] = $tmp; $hdata = array(); foreach ($res as $item) { $hdata = array_merge($hdata, array_filter($item[$model->alias])); } //debug($hdata); } else { if (isset($data[$alias])) { $hdata = $data[$alias]; } elseif (isset($data[$model->alias][$alias])) { $hdata = $data[$model->alias][$alias]; } //debug($hdata); if (isset($model->{$alias}) && $model->{$alias}->Behaviors->attached('Inheritor') && !empty($hdata)) { $hdata = $model->{$alias}->getIneritedFields($hdata, array('fields' => array_keys($iopt), 'fetchAssociations' => false)); } } foreach ($iopt as $key => $val) { if (!isset($hdata[$key])) { $myData[$key] = null; } elseif (is_array($myData[$key])) { if (!empty($hdata[$key])) { $myData[$key] = SetMulti::merge2($myData[$key], $hdata[$key]); } } else { $myData[$key] = $hdata[$key]; } } if ($opt['fetchAssociations']) { if (!empty($model->lastAssociations['belongsTo'])) { $associationFields = SetMulti::extractKeepKey('foreignKey', $model->lastAssociations['belongsTo']); $toFetch = array_intersect($associationFields, array_keys($iopt)); foreach ($toFetch as $aalias => $field) { if ($originalData[$field] != $myData[$field]) { $findOpt = $model->lastAssociations['belongsTo'][$aalias]; if (isset($model->{$aalias})) { $amodel = $model->{$aalias}; } else { $amodel = ClassRegistry::init($findOpt['className']); } unset($findOpt['className'], $findOpt['foreignKey']); $findOpt['conditions'][$amodel->alias . '.' . $amodel->primaryKey] = $myData[$field]; //debug($findOpt); $res = $amodel->find('first', $findOpt); $amodel->Behaviors->attach('Util'); $res = $amodel->dataToContainedRelations($res); $data[$aalias] = $res; } } } } } } } return $data; }
function codesExists($codes, $full = false, $availableCoupon = false) { if (empty($codes)) { return null; } $multi = is_array($codes); if (!$multi) { $codes = array($codes); } $res = array_combine($codes, array_fill(0, count($codes), false)); $findOpt = array('conditions' => array('code' => $codes), 'recursive' => -1); if ($full) { App::import('Lib', 'Shop.SetMulti'); $existingPromo = $this->find('all', $findOpt); if (!empty($existingPromo)) { $existingPromo = SetMulti::group($existingPromo, 'ShopPromotion.code', array('singleArray' => true)); } } else { $findOpt['fields'] = array('id', 'code'); $existingPromo = $this->find('list', $findOpt); if (!empty($existingPromo)) { $existingPromo = array_combine($existingPromo, array_fill(0, count($existingPromo), true)); } } if (!empty($existingPromo)) { $res = array_merge($res, $existingPromo); } if (count(array_filter($res)) != count($res)) { $findOpt = array('fields' => array('id', 'code'), 'conditions' => array('code' => $codes)); if ($availableCoupon) { $findOpt['conditions'][] = array('or' => array('ShopCoupon.status not' => array('used', 'reserved'), 'ShopCoupon.status' => null)); } if ($full) { App::import('Lib', 'Shop.SetMulti'); $findOpt['contain'] = array('ShopPromotion'); $existingCoupon = $this->ShopCoupon->find('all', $findOpt); if (!empty($existingCoupon)) { $existingCoupon = SetMulti::group($existingCoupon, 'ShopCoupon.code', array('singleArray' => false)); } } else { $findOpt['fields'] = array('id', 'code'); $findOpt['recursive'] = -1; $existingCoupon = $this->ShopCoupon->find('list', $findOpt); if (!empty($existingCoupon)) { $existingCoupon = array_combine($existingCoupon, array_fill(0, count($existingCoupon), true)); } } if (!empty($existingCoupon)) { $res = array_merge($res, $existingCoupon); } } if (!$multi && !empty($res)) { return $res[0]; } return $res; }
function getAllRelated($products = null, $opt = array()) { $defOpt = array('minField' => false); $opt = array_merge($defOpt, $opt); $relatedModels = array(); foreach ($products as $key => $product) { if (empty($product['Related']) && empty($product[$this->alias]['Related'])) { $relatedRef = $this->getRelatedRef($product); $relatedRef['key'] = $key; if (empty($relatedModels[$relatedRef['model']])) { $relatedModel = $this->getRelatedClass($relatedRef); //if(!empty($relatedModel->data[$relatedModel->alias]['id']) && $relatedModel->data[$relatedModel->alias]['id'] == $relatedRef['foreign_id']){ // $products[$key]['Related'] = $relatedModel->data[$relatedModel->alias]; //}else{ if ($relatedModel) { $relatedModels[$relatedRef['model']]['class'] = $relatedModel; $relatedModels[$relatedRef['model']][] = $relatedRef; } //} } else { $relatedModels[$relatedRef['model']][] = $relatedRef; } } } foreach ($relatedModels as $alias => $refs) { $relatedModel = $refs['class']; $tmp = $relatedModel->recursive; unset($refs['class']); App::import('Lib', 'Shop.SetMulti'); $relatedModel->recursive = -1; $ids = Set::extract('/foreign_id', $refs); $findOpt = array('conditions' => array($relatedModel->alias . '.id' => $ids)); if ($opt['minField']) { $fields = $this->dynamicFieldsExtractData($relatedModel, $opt); $fields = array_values(Set::flatten($fields)); $fields = SetMulti::pregFilter('/^Related\\./', $fields); $fields = str_replace('Related.', $relatedModel->alias . '.', $fields); $fields[] = $relatedModel->alias . '.id'; $cpy = $fields; $fields = array(); foreach ($cpy as $field) { if ($relatedModel->hasField(str_replace($relatedModel->alias . '.', '', $field))) { $fields[] = $field; } } //debug($fields); $findOpt['fields'] = $fields; } $manyRelated = $relatedModel->find('all', $findOpt); $relatedModel->recursive = $tmp; $manyRelated = SetMulti::group($manyRelated, $relatedModel->alias . '.id', array('singleArray' => false)); //debug($manyRelated); foreach ($refs as $ref) { if (!empty($manyRelated[$ref['foreign_id']])) { $products[$ref['key']] = (array) $products[$ref['key']]; $products[$ref['key']]['Related'] = $manyRelated[$ref['foreign_id']][$relatedModel->alias]; } } } return $products; }
function getInvalidationData($nodes) { $this->load(); $opts = array('inheritParent' => false, 'mode' => 'all', 'fields' => array('id', 'model', 'foreign_key'), 'conditions' => array('model NOT' => null), 'order' => 'model'); $NodeLink = ClassRegistry::init('NodeLink'); $Invalidation = ClassRegistry::init('Invalidation'); /*if(!empty($this->loadedItems)){ //reset($this->loadedItems); //$minTime = current($this->loadedItems); //debug($minTime); $loadedCond = array(); foreach($this->loadedItems as $node_id => $time){ $loadedCond[] = array( $Invalidation->alias.'.node_id'=>$node_id, $Invalidation->alias.'.time >'=>$time ); } $opts = set::merge($opts,array( 'fields' => Array( "GROUP_CONCAT(IFNULL (".$Invalidation->alias.".field,'all')) AS Fields" ), 'joins' => array('1000-invalidation'=>array( 'type'=>'left', 'table'=>$Invalidation->useTable, 'alias' =>$Invalidation->alias, 'conditions' => array( $Invalidation->alias.'.node_id = AllNode.id', array('or'=>$loadedCond) ) )), 'conditions' => array( array('or'=>array( $Invalidation->alias.'.node_id IS NOT NULL', 'not'=>array('AllNode.id'=>array_keys($this->loadedItems)) )) ) )); }*/ //debug($opts); $allNodes = $NodeLink->getLinked($nodes, 'invalidation', $opts); //debug($allNodes); if (!empty($allNodes)) { /////////// Get invalidations /////////// App::import('Lib', 'SetMulti'); $allNodeId = $this->_getAllNodeIds($allNodes); $excludeCheck = array(); $invalids = array(); if (!$this->disableCache) { $excludeCheck = array_intersect_key($this->loadedItems, array_flip($allNodeId)); $bydate = SetMulti::flip($excludeCheck); //debug($bydate); $findOpt = array('fields' => array($Invalidation->alias . '.id', $Invalidation->alias . '.node_id')); foreach ($bydate as $time => $nids) { $findOpt['conditions']['or'][] = array($Invalidation->alias . '.node_id' => $nids, $Invalidation->alias . '.time >' => $time); } $invalids = $Invalidation->find('all', $findOpt); if (!empty($invalids)) { $invalids = SetMulti::group($invalids, $Invalidation->alias . '.node_id'); } } //debug($invalids); //debug($excludeCheck); /////////// remove unneeded /////////// $toGet = array(); foreach ($allNodes as $node) { $nid = $node['Node']['id']; if ((!isset($excludeCheck[$nid]) || isset($invalids[$nid])) && !empty($node['Node']['model'])) { $model = $node['Node']['model']; if (empty($toGet[$model])) { $toGet[$model] = array('ids' => array(), 'globalFields' => 'all', 'fields' => array()); } $id = $node['Node']['foreign_key']; $toGet[$model]['ids'][] = $id; $toGet[$model]['fields'][$id] = 'all'; } } //debug($toGet); $aro = array($this->controller->User->myNodeRef($this->controller->user['User']['id'])); //debug($aro); $allItems = array(); foreach ($toGet as $modelName => $opt) { $model = ClassRegistry::init($modelName); $model->create(); $findOptions = array('conditions' => array($model->alias . '.id' => $opt['ids'])); if ($opt['globalFields'] != 'all') { $findOptions['restrict'] = $opt['globalFields']; } //debug($findOptions); $items = $model->triggerAction('linkRead', array($findOptions), $aro); if (!empty($items)) { $allItems = array_merge($allItems, $items); } } //debug($allItems); return $allItems; } return null; }
function setPromo($codes, $order) { if (!is_array($order)) { $order = array('ShopOrder' => array('id' => $order)); } $order_id = $order['ShopOrder']['id']; $codeMapping = $this->ShopOrder->ShopPromotion->codesExists($codes, true, true); $codeMapping = array_filter($codeMapping); if (!empty($codeMapping)) { $promoWithCode = SetMulti::group(SetMulti::flatten($codeMapping, array('level' => 1)), 'ShopPromotion.id', array('singleArray' => false)); } if (!empty($order['ShopProduct'])) { $products = $order['ShopProduct']; } else { $products = $this->ShopOrder->ShopOrdersItem->find('all', array('conditions' => array('ShopOrdersItem.order_id' => $order_id))); } $coupons = array(); foreach ($products as $product) { $promos = array(); if (!empty($product['ShopProduct']['ShopPromotion'])) { $promos = $product['ShopProduct']['ShopPromotion']; } elseif (!empty($product['ShopPromotion'])) { $promos = $product['ShopPromotion']; } foreach ($promos as $promo) { $mapped = null; if (isset($promoWithCode[$promo['id']])) { $mapped = $promoWithCode[$promo['id']]; } if ($promo['code_needed']) { $applicable = !empty($mapped); } if ($applicable && $promo['limited_coupons']) { if (!empty($mapped['ShopCoupon']['id'])) { $applicable = true; } elseif (!$promo['coupon_code_needed']) { $coupon = $this->ShopOrder->ShopCoupon->find('first', array('conditions' => array('shop_promotion_id' => $promo['id'], 'or' => array('ShopCoupon.status not' => array('used', 'reserved'), 'ShopCoupon.status' => null)))); $applicable = !empty($coupon); $mapped['ShopCoupon'] = $coupon['ShopCoupon']; } else { $applicable = false; } } if ($applicable) { if (!empty($mapped['ShopCoupon']['id'])) { $coupon = $mapped['ShopCoupon']; } else { $coupon = array('active' => true, 'shop_promotion_id' => $promo['id']); } $coupon['shop_order_id'] = $order_id; $coupon['status'] = 'reserved'; $coupons['unique-' . $promo['id']] = $coupon; } } } //debug($coupons); foreach ($coupons as $coupon) { $this->ShopOrder->ShopCoupon->create(); $this->ShopOrder->ShopCoupon->save($coupon); } //debug($products); }
function admin_excel($id = null) { App::import('Vendor', 'Newsletter.PHPExcel', array('file' => 'PHPExcel/IOFactory.php')); //echo getcwd(); //$excel = new PHPExcel(); $objReader = PHPExcel_IOFactory::createReader('Excel2007'); //$objPHPExcel = $objReader->load(); $objPHPExcel = $objReader->load(APP . 'plugins' . DS . 'newsletter' . DS . 'vendors' . DS . 'template.xlsx'); //var_dump($excel); $readIdsFindOpt = array('fields' => array('Sended.id', 'Sended.id'), 'conditions' => array('NewsletterSended.newsletter_id' => $id), 'joins' => array(array('alias' => 'Evnt', 'table' => $this->NewsletterEvent->useTable, 'type' => 'INNER', 'conditions' => array('NewsletterSended.id = Evnt.sended_id', 'not' => array('Evnt.action' => 'bounce'))), array('alias' => 'Sended', 'table' => $this->NewsletterSended->useTable, 'type' => 'INNER', 'conditions' => array('NewsletterSended.email = Sended.email', 'Sended.newsletter_id' => $id))), 'group' => 'Sended.id', 'recursive' => -1); $readIds = $this->NewsletterSended->find('list', $readIdsFindOpt); //debug($readIds); $urlsFindOpt = array('fields' => array('NewsletterEvent.url', 'NewsletterSended.email'), 'conditions' => array('NewsletterEvent.sended_id' => $readIds, 'not' => array('NewsletterEvent.action' => 'bounce'), 'NewsletterEvent.url IS NOT NULL'), 'order' => 'NewsletterSended.email', 'group' => 'NewsletterEvent.url', 'contain' => 'NewsletterSended'); $this->NewsletterEvent->Behaviors->attach('Containable'); App::import('Lib', 'Newsletter.SetMulti'); $urls_by_email = SetMulti::group($this->NewsletterEvent->find('all', $urlsFindOpt), 'NewsletterSended.email', array('valPath' => 'NewsletterEvent.url')); // debug($urls_by_email); $nbEventsFindOpt = array('fields' => array('NewsletterSended.email', 'count(Evnt.id) as `count`'), 'conditions' => array('NewsletterSended.id' => $readIds), 'joins' => array(array('alias' => 'Evnt', 'table' => $this->NewsletterEvent->useTable, 'type' => 'LEFT', 'conditions' => array('NewsletterSended.id = Evnt.sended_id', 'not' => array('Evnt.action' => 'bounce')))), 'group' => 'NewsletterSended.email', 'recursive' => -1); App::import('Lib', 'Newsletter.SetMulti'); $nbEvents = $this->NewsletterSended->find('all', $nbEventsFindOpt); // debug($nbEvents); $notReadsFindOpt = array('fields' => array('NewsletterSended.id', 'NewsletterSended.email'), 'conditions' => array('not' => array('NewsletterSended.id' => $readIds)), 'group' => 'NewsletterSended.email', 'recursive' => -1); $notReads = $this->NewsletterSended->find('list', $notReadsFindOpt); //debug($notReads); // $this->render(false); // return; $row_sheet_index = 0; $row_index = 0; $cc = 0; foreach ($objPHPExcel->getWorksheetIterator() as $worksheet) { if ($cc == 0) { $worksheet->setTitle("Courriels ouvert"); $row_sheet_index = 0; foreach ($nbEvents as $email) { $mail = $email['NewsletterSended']['email']; $worksheet->setCellValueByColumnAndRow(0, $row_sheet_index + 2, $mail); $worksheet->setCellValueByColumnAndRow(1, $row_sheet_index + 2, $email['0']['count']); if (!empty($urls_by_email[$mail])) { foreach ($urls_by_email[$mail] as $i => $url) { $worksheet->setCellValueByColumnAndRow(2 + $i, $row_sheet_index + 2, $url); } } $row_sheet_index++; } } else { $row_sheet_index = 0; $worksheet->setTitle("Courriels non-ouvert"); foreach ($notReads as $email) { $worksheet->setCellValueByColumnAndRow(0, $row_sheet_index + 2, $email); $row_sheet_index++; } } $cc++; //break; //$worksheet->setCellValueByColumnAndRow($key,$row_sheet_index + 5, $arr[$row_index][$val]); } header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'); header('Content-Disposition: attachment;filename="export.xlsx"'); header('Cache-Control: max-age=0'); $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007'); $objWriter->save('php://output'); exit; }
function getMinimalData(&$model, $fields, $data = null, $findOptions = array(), $priorityRes = true) { if (is_null($data)) { $data = $model->data; } //debug($data); //////// get fields and contain //////// $contain = array(); $findFields = array(); foreach ($fields as &$field) { if (substr($field, 0, strlen($model->alias) + 1) == $model->alias . '.') { $findFields[] = $field; $field = $model->alias . '.' . $field; } elseif (($pos = strrpos($field, '.')) == false) { $findFields[] = $model->alias . '.' . $field; } else { $path = substr($field, 0, $pos); $depth = count(explode('.', $path)); $f = substr($field, $pos + 1); if ($depth > 1) { $contain = Set::insert($contain, $path . '.fields', array('id', $f)); } else { $contain = Set::insert($contain, $path, array()); if (!in_array($path . '.id', $findFields)) { $findFields[] = $path . '.id'; } $findFields[] = $field; } } } if (!in_array($model->alias . '.id', $findFields)) { $findFields[] = $model->alias . '.id'; } //////// normalize Data //////// list($data, $oldFormat) = $model->unifiedResult($data, null, null, true); App::import('Lib', 'SetMulti'); $data = SetMulti::group($data, $model->alias . '.id', array('singleArray' => false)); //debug($data); //////// get ids //////// $ids = array(); foreach ($data as $entry) { foreach ($fields as $field) { if (!Set::check($entry, $field)) { $ids[] = $entry[$model->alias]['id']; continue 2; } } } //////// query //////// $findOpt = array('fields' => $findFields, 'conditions' => array($model->alias . '.id' => $ids), 'contain' => $contain); $findOpt = array_merge($findOpt, $findOptions); //debug($findOpt); $completion = $model->find('all', $findOpt); //debug($completion); //////// merge data //////// foreach ($completion as $c) { $id = $c[$model->alias]['id']; if ($priorityRes) { $data[$id] = Set::merge($data[$id], $c); } else { $data[$id] = Set::merge($c, $data[$id]); } } $data = array_values($data); $data = $this->unifiedResult($model, $data, null, $oldFormat); //debug($data); return $data; }