Skip to content

Commit

Permalink
Merge pull request ovanschie#7 from ovanschie/dev
Browse files Browse the repository at this point in the history
0.5
  • Loading branch information
ovanschie committed Apr 28, 2015
2 parents 34c3111 + 6321c32 commit e98c173
Show file tree
Hide file tree
Showing 56 changed files with 431 additions and 132 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,52 @@ protected function _prepareForm()

$form = new Varien_Data_Form();

$fieldset = $form->addFieldset('product_insert', array(
'legend' => $this->__('Product')
// category
$category = $form->addFieldset('category_insert', array(
'legend' => $this->__('Categories')
));

$category->addField('categories_parent', 'select', array(
'label' => $this->__('Parent'),
'required' => true,
'name' => 'categories_parent',
'values' => Mage::getModel('ovs_magefaker/source_category')->toOptionArray(false),
'value' => Mage::getModel('ovs_magefaker/source_category')->getFirstValue(),
'after_element_html' => 'Will append new categories to this parent',
));

$category->addField('categories_insert', 'select', array(
'label' => $this->__('Number of categories to insert'),
'name' => 'categories_insert',
'value' => '0',
'values' => array(
'0' => '0',
'1' => '1',
'5' => '5',
'10' => '10',
'25' => '25',
'50' => '50',
'100' => '100',
'150' => '150',
'250' => '250',
'500' => '500'
)
));

// product
$product = $form->addFieldset('product_insert', array(
'legend' => $this->__('Products')
));

$fieldset->addField('products_category', 'multiselect', array(
$product->addField('products_category', 'multiselect', array(
'label' => $this->__('Select categories'),
'required' => true,
'name' => 'products_category',
'values' => Mage::getModel('ovs_magefaker/source_category')->toOptionArray(false),
'value' => Mage::getModel('ovs_magefaker/source_category')->getFirstValue()
));

$fieldset->addField('products_insert', 'select', array(
$product->addField('products_insert', 'select', array(
'label' => $this->__('Number of products to insert'),
'name' => 'products_insert',
'value' => '0',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,29 @@ protected function _prepareForm()

$form = new Varien_Data_Form();

$fieldset = $form->addFieldset('product_remove', array(
'legend' => $this->__('Product')
// categories remove
$category = $form->addFieldset('category_remove', array(
'legend' => $this->__('Categories')
));

$fieldset->addField('products_remove', 'checkbox', array(
$category->addField('categories_remove', 'checkbox', array(
'label' => $this->__('Remove all MageFaker categories'),
'name' => 'categories_remove',
'value' => 'categories_remove',
'after_element_html' => '</td><td><small><em>' . $this->__("This will remove all categories with the 'magefaker-' prefix in url") . '</em></small>'
));


// products remove
$products = $form->addFieldset('product_remove', array(
'legend' => $this->__('Products')
));

$products->addField('products_remove', 'checkbox', array(
'label' => $this->__('Remove all MageFaker products'),
'name' => 'products_remove',
'value' => 'products_remove',
'after_element_html' => '</td><td><small><em>' . $this->__("This will remove all products with the 'magefaker-' prefix") . '</em></small>'
'after_element_html' => '</td><td><small><em>' . $this->__("This will remove all products with the 'magefaker-' prefix in sku") . '</em></small>'
));

$form->setUseContainer(false);
Expand Down
140 changes: 104 additions & 36 deletions app/code/community/Ovs/Magefaker/Model/Faker.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@ public function insertProducts($count, $categories){
$faker = new Faker\Generator();
$faker->addProvider(new Faker\Provider\en_US\Person($faker));
$faker->addProvider(new Faker\Provider\Lorem($faker));
$faker->addProvider(new Faker\Provider\Product($faker));
$faker->addProvider(new Faker\Provider\Ecommerce($faker));

$rating_options = array(
1 => array(1,2,3,4,5),
2 => array(6,7,8,9,10),
3 => array(11,12,13,14,15)
1 => array(1, 2, 3, 4, 5),
2 => array(6, 7, 8, 9, 10),
3 => array(11, 12, 13, 14, 15)
);

for($i = 0; $i < $count; $i++) {
Expand All @@ -42,47 +42,50 @@ public function insertProducts($count, $categories){

try {
$product
->setWebsiteIds(array(1))//website ID the product is assigned to, as an array
->setAttributeSetId(4)//ID of a attribute set named 'default'
->setTypeId('simple')//product type
->setCreatedAt(strtotime('now'))//product creation time
->setSku($sku)//SKU
->setName($name)//product name
->setWebsiteIds(array(1))
->setAttributeSetId(4) // default set
->setTypeId('simple')
->setCreatedAt(strtotime('now'))

->setSku($sku)
->setName($name)
->setUrlKey($name . '-' . $sku)
->setCategoryIds($categories)
->setWeight($faker->weight)
->setStatus(1)//product status (1 - enabled, 2 - disabled)
->setTaxClassId(0)//tax class (0 - none, 1 - default, 2 - taxable, 4 - shipping)
->setVisibility(Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH)//catalog and search visibility
->setNewsFromDate(strtotime('now'))//product set as new from
->setNewsToDate(strtotime("+1 week"))//product set as new to
->setCountryOfManufacture('NL')//country of manufacture (2-letter country code)

->setPrice($price)//price in form 11.22
->setCost(($price * 0.66))//price in form 11.22
->setMsrpEnabled(1)//enable MAP
->setMsrpDisplayActualPriceType(4)//display actual price (1 - on gesture, 2 - in cart, 3 - before order confirmation, 4 - use config)
->setMsrp($price)//Manufacturer's Suggested Retail Price

->setStatus(1) // product status (1 - enabled, 2 - disabled)
->setTaxClassId(0) //tax class (0 - none, 1 - default, 2 - taxable, 4 - shipping)
->setVisibility(Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH)
->setNewsFromDate(strtotime('now'))
->setNewsToDate(strtotime("+1 week"))
->setCountryOfManufacture('NL')

->setPrice($price)
->setCost(($price * 0.66))
->setMsrpEnabled(1)
->setMsrpDisplayActualPriceType(4) // display actual price (1 - on gesture, 2 - in cart, 3 - before order confirmation, 4 - use config)
->setMsrp($price)

->setMetaTitle($name)
->setMetaKeyword($faker->metaKeys)
->setMetaDescription($faker->metaDescription)
->setDescription($faker->description)
->setShortDescription($faker->shortDescription)
->setMediaGallery(array('images' => array(), 'values' => array()))//media gallery initialization
->addImageToMediaGallery($faker->productImage, array('image', 'thumbnail', 'small_image'), false, false)//assigning image, thumb and small image to media gallery
->addImageToMediaGallery($faker->productImage, array(), false, false)//assigning image, thumb and small image to media gallery
->addImageToMediaGallery($faker->productImage, array(), false, false)//assigning image, thumb and small image to media gallery

->setMediaGallery(array('images' => array(), 'values' => array()))
->addImageToMediaGallery($faker->productImage, array('image', 'thumbnail', 'small_image'), false, false)
->addImageToMediaGallery($faker->productImage, array(), false, false)
->addImageToMediaGallery($faker->productImage, array(), false, false)

->setStockData(array(
'use_config_manage_stock' => 1, //'Use config settings' checkbox
'manage_stock' => 1, //manage stock
'min_sale_qty' => 1, //Minimum Qty Allowed in Shopping Cart
'max_sale_qty' => 99, //Maximum Qty Allowed in Shopping Cart
'is_in_stock' => 1, //Stock Availability
'qty' => 999 //qty
'use_config_manage_stock' => 1,
'manage_stock' => 1,
'min_sale_qty' => 1,
'max_sale_qty' => 99,
'is_in_stock' => 1,
'qty' => 999
)
)
->setCategoryIds($categories) //assign product to categories
->setUrlKey($name . '-' . $sku);
);

$product->save();

Expand All @@ -106,7 +109,7 @@ public function insertProducts($count, $categories){
$review->save();

foreach($rating_options as $rating_id => $option_ids) {
$stars = mt_rand(0, 4);
$stars = mt_rand(1, 3);

Mage::getModel('rating/rating')
->setRatingId($rating_id)
Expand Down Expand Up @@ -153,4 +156,69 @@ public function removeProducts(){
return true;
}

/**
* Generates categories
*
* @param $count
* @return bool
*/
public function insertCategories($count, $parentId){

Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);

$faker = new Faker\Generator();
$faker->addProvider(new Faker\Provider\en_US\Person($faker));
$faker->addProvider(new Faker\Provider\Lorem($faker));
$faker->addProvider(new Faker\Provider\Ecommerce($faker));

for($i = 0; $i < $count; $i++) {

try{
$name = $faker->categoryName();
$parentCategory = Mage::getModel('catalog/category')->load($parentId);

$category = Mage::getModel('catalog/category');
$category->setName($name);
$category->setUrlKey('magefaker-' . $faker->categoryUrl($name));
$category->setImage($faker->categoryImage);
$category->setIsActive(1);
$category->setDisplayMode('PRODUCTS');
$category->setIsAnchor(1);
$category->setStoreId(Mage_Core_Model_App::ADMIN_STORE_ID);
$category->setPath($parentCategory->getPath());
$category->save();

} catch(Exception $e) {
Mage::logException($e);
return false;
}

}

return true;
}

/**
* Removes categories with magefaker prefix
*
* @return bool
*/
public function removeCategories(){
try{
$categories = Mage::getModel('catalog/category')
->getCollection()
->addAttributeToFilter('url_key', array('like' => '%magefaker-%'))
->load();

foreach($categories as $category){
$category->delete();
}

} catch (Exception $e) {
Mage::logException($e);
return false;
}

return true;
}
}
111 changes: 87 additions & 24 deletions app/code/community/Ovs/Magefaker/Model/Source/Category.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,32 +5,95 @@
*/
class Ovs_Magefaker_Model_Source_Category extends Mage_Core_Model_Abstract{

public function toOptionArray($addEmpty = true){
$tree = Mage::getResourceModel('catalog/category_tree');

$collection = Mage::getResourceModel('catalog/category_collection');

$collection->addAttributeToSelect('name')
->load();

$options = array();

if ($addEmpty) {
$options[] = array(
'label' => Mage::helper('adminhtml')->__('-- Please Select a Category --'),
'value' => ''
);
}
foreach ($collection as $category) {
$options[] = array(
'label' => $category->getName(),
'value' => $category->getId()
);
}

return $options;
protected $options = array();

/**
* @param bool $addEmpty
* @return array
* @throws Mage_Core_Exception
*/
public function toOptionArray($addEmpty = true){

if ($addEmpty) {
$this->options[] = array(
'label' => Mage::helper('adminhtml')->__('-- Please Select a Category --'),
'value' => ''
);
}

$depth = 6;
$parentId = 1;

$category = Mage::getModel('catalog/category');
$categories = $category->getCategories($parentId, $depth, TRUE, FALSE, TRUE);

foreach ($categories as $node) {

if ($node->hasChildren()) {
$this->options[] = array(
'label' => $node->getName(),
'style' => 'font-weight:bold',
'value' => $node->getEntityId()
);

$this->_getChildOptions($node->getChildren());
}
else{
$this->options[] = array(
'label' => $node->getName(),
'value' => $node->getEntityId()
);
}

}

return $this->options;
}

/**
* Makes options from child category
*
* @param Varien_Data_Tree_Node_Collection $nodeCollection
*/
protected function _getChildOptions(Varien_Data_Tree_Node_Collection $nodeCollection)
{

foreach ($nodeCollection as $node) {

$times = $node->getLevel() * 2;

if($times <= 4){
$times = 2;
}

$indent = 3 * $times;

if ($node->hasChildren()) {
$this->options[] = array(
'label' => $node->getName(),
'style' => 'font-weight:bold;text-indent:' . $indent . 'px',
'value' => $node->getEntityId()
);

$this->_getChildOptions($node->getChildren());
}
else{
$this->options[] = array(
'label' => $node->getName(),
'style' => 'text-indent:' . ($indent + 2) . 'px',
'value' => $node->getEntityId()
);
}

}

}

/**
* Returns first value of option array
*
* @return mixed
*/
public function getFirstValue(){
$values = $this->toOptionArray(false);
return $values[0];
Expand Down
Loading

0 comments on commit e98c173

Please sign in to comment.