PageRenderTime 21ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/app/protected/modules/products/data/ProductsDemoDataMaker.php

https://bitbucket.org/zurmo/zurmo/
PHP | 162 lines | 104 code | 10 blank | 48 comment | 7 complexity | 977d7c82e9920feecf40083019308884 MD5 | raw file
Possible License(s): AGPL-3.0, BSD-3-Clause, GPL-2.0, LGPL-3.0, LGPL-2.1, BSD-2-Clause
  1. <?php
  2. /*********************************************************************************
  3. * Zurmo is a customer relationship management program developed by
  4. * Zurmo, Inc. Copyright (C) 2015 Zurmo Inc.
  5. *
  6. * Zurmo is free software; you can redistribute it and/or modify it under
  7. * the terms of the GNU Affero General Public License version 3 as published by the
  8. * Free Software Foundation with the addition of the following permission added
  9. * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
  10. * IN WHICH THE COPYRIGHT IS OWNED BY ZURMO, ZURMO DISCLAIMS THE WARRANTY
  11. * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
  12. *
  13. * Zurmo is distributed in the hope that it will be useful, but WITHOUT
  14. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  15. * FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
  16. * details.
  17. *
  18. * You should have received a copy of the GNU Affero General Public License along with
  19. * this program; if not, see http://www.gnu.org/licenses or write to the Free
  20. * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  21. * 02110-1301 USA.
  22. *
  23. * You can contact Zurmo, Inc. with a mailing address at 27 North Wacker Drive
  24. * Suite 370 Chicago, IL 60606. or at email address contact@zurmo.com.
  25. *
  26. * The interactive user interfaces in original and modified versions
  27. * of this program must display Appropriate Legal Notices, as required under
  28. * Section 5 of the GNU Affero General Public License version 3.
  29. *
  30. * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
  31. * these Appropriate Legal Notices must retain the display of the Zurmo
  32. * logo and Zurmo copyright notice. If the display of the logo is not reasonably
  33. * feasible for technical reasons, the Appropriate Legal Notices must display the words
  34. * "Copyright Zurmo Inc. 2015. All rights reserved".
  35. ********************************************************************************/
  36. /**
  37. * Class that builds demo products.
  38. */
  39. class ProductsDemoDataMaker extends DemoDataMaker
  40. {
  41. protected $ratioToLoad = 1;
  42. /**
  43. * Gets the dependencies before creating products data
  44. * @return array
  45. */
  46. public static function getDependencies()
  47. {
  48. return array('productTemplates');
  49. }
  50. /**
  51. * @param object $demoDataHelper
  52. */
  53. public function makeAll(& $demoDataHelper)
  54. {
  55. assert('$demoDataHelper instanceof DemoDataHelper');
  56. assert('$demoDataHelper->isSetRange("Contact")');
  57. assert('$demoDataHelper->isSetRange("Account")');
  58. assert('$demoDataHelper->isSetRange("Opportunity")');
  59. assert('$demoDataHelper->isSetRange("User")');
  60. $products = array();
  61. $productRandomData = self::getProductsRandomData();
  62. for ($i = 0; $i < count($productRandomData['names']); $i++)
  63. {
  64. $product = new Product();
  65. $product->contact = $demoDataHelper->getRandomByModelName('Contact');
  66. $product->account = $demoDataHelper->getRandomByModelName('Account');
  67. $product->opportunity = $demoDataHelper->getRandomByModelName('Opportunity');
  68. $product->owner = $demoDataHelper->getRandomByModelName('User');
  69. $name = $productRandomData['names'][$i];
  70. $this->populateModelData($product, $i, $name);
  71. $saved = $product->save();
  72. assert('$saved');
  73. $products[] = $product->id;
  74. }
  75. $demoDataHelper->setRangeByModelName('Product', $products[0], $products[count($products)-1]);
  76. }
  77. public function populateModelData(& $model, $counter, $name)
  78. {
  79. assert('$model instanceof Product');
  80. parent::populateModel($model);
  81. $productTemplateName = self::getProductTemplateForProduct($name);
  82. $allTemplates = ProductTemplate::getAll();
  83. foreach ($allTemplates as $template)
  84. {
  85. if ($template->name == $productTemplateName)
  86. {
  87. $templateId = $template->id;
  88. }
  89. }
  90. $productTemplate = ProductTemplate::getById($templateId);
  91. $model->name = $name;
  92. $model->quantity = mt_rand(1, 95);
  93. $model->productTemplate = $productTemplate;
  94. $model->stage->value = 'Open';
  95. $model->priceFrequency = $productTemplate->priceFrequency;
  96. $model->sellPrice->value= $productTemplate->sellPrice->value;
  97. $model->type = $productTemplate->type;
  98. }
  99. public static function getProductTemplateForProduct($product)
  100. {
  101. $productTemplateMapping = array(
  102. 'Amazing Kid Sample' => 'Amazing Kid',
  103. 'You Can Do Anything Sample' => 'You Can Do Anything',
  104. 'A Bend in the River November Issue' => 'A Bend in the River',
  105. 'A Gift of Monotheists October Issue' => 'A Gift of Monotheists',
  106. 'Enjoy Once in a Lifetime Music' => 'Once in a Lifetime'
  107. );
  108. if (!array_key_exists($product, $productTemplateMapping))
  109. {
  110. $productNameSubstr = explode('-P', $product);
  111. if ((strpos($product, 'Laptop') !== false) ||
  112. (strpos($product, 'Camera') !== false) ||
  113. (strpos($product, 'Handycam') !== false))
  114. {
  115. return $productNameSubstr[0];
  116. }
  117. }
  118. return $productTemplateMapping[$product];
  119. }
  120. /**
  121. * Gets the products random data
  122. * @return array
  123. */
  124. public static function getProductsRandomData()
  125. {
  126. $productNames = array(
  127. 'names' => array(
  128. 'Amazing Kid Sample',
  129. 'You Can Do Anything Sample',
  130. 'A Bend in the River November Issue',
  131. 'A Gift of Monotheists October Issue',
  132. 'Enjoy Once in a Lifetime Music'
  133. )
  134. );
  135. $productTemplates = ProductTemplate::getAll();
  136. foreach ($productTemplates as $template)
  137. {
  138. if ((strpos($template->name, 'Laptop') !== false) ||
  139. (strpos($template->name, 'Camera') !== false) ||
  140. (strpos($template->name, 'Handycam') !== false))
  141. {
  142. for ($i = 1; $i < 3; $i++)
  143. {
  144. $randomString = substr(str_shuffle("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"), 0, 2);
  145. $productNames['names'][] = $template->name . '-P' . $randomString;
  146. }
  147. }
  148. }
  149. return $productNames;
  150. }
  151. }
  152. ?>