Skip to content

Commit

Permalink
Fixes for Images, delete ingredients, ordering
Browse files Browse the repository at this point in the history
  • Loading branch information
nazgul26 committed Aug 29, 2022
1 parent 9e44a18 commit 4f9ca34
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 37 deletions.
40 changes: 25 additions & 15 deletions src/Controller/RecipesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,18 +86,20 @@ public function view($id = null, $servings=null)
],
'Units' => [
'fields' => ['name', 'abbreviation']
]
],
'sort' => ['IngredientMappings.sort_order' => 'ASC']
],
'RelatedRecipes' => [
'Recipes' => [
'fields' => ['id', 'name', 'directions'],
'IngredientMappings' => [
'Ingredients' => [
'fields' => ['name']
],
'Units' => [
'fields' => ['name', 'abbreviation']
]
],
'Units' => [
'fields' => ['name', 'abbreviation']
],
'sort' => ['IngredientMappings.sort_order' => 'ASC']
]
]
],
Expand Down Expand Up @@ -143,7 +145,8 @@ public function edit($id = null)
],
'Units' => [
'fields' => ['name', 'abbreviation']
]
],
'sort' => ['IngredientMappings.sort_order' => 'ASC']
],
'RelatedRecipes' => [
'Recipes' => [
Expand All @@ -154,7 +157,8 @@ public function edit($id = null)
],
'Units' => [
'fields' => ['name', 'abbreviation']
]
],
'sort' => ['IngredientMappings.sort_order' => 'ASC']
]
]
],
Expand All @@ -170,21 +174,18 @@ public function edit($id = null)
$recipe->user_id = $this->Auth->user('id');
if ($this->Recipes->save($recipe)) {
$this->Flash->success(__('The recipe has been saved.'));

return $this->redirect(['action' => 'index']);
} else {
$this->Flash->error(__('The recipe could not be saved. Please, try again.'));
}

/*
NOTE: Helpful debug info
$x = $recipe->getErrors();
//NOTE: Helpful debug info
/*$x = $recipe->getErrors();
if ($x) {
debug($recipe);
debug($x);
return false;
}
*/
$this->Flash->error(__('The recipe could not be saved. Please, try again.'));
}*/
}
$ethnicities = $this->Recipes->Ethnicities->find('list', ['limit' => 200, 'order' => ['Ethnicities.name']]);
$baseTypes = $this->Recipes->BaseTypes->find('list', ['limit' => 200, 'order' => ['BaseTypes.name']]);
Expand All @@ -211,6 +212,15 @@ public function delete($id = null)
return $this->redirect(['action' => 'index']);
}

public function removeIngredientMapping($recipeId, $mappingId) {
$entity = $this->Recipes->IngredientMappings->get($mappingId);
if ($this->Recipes->IngredientMappings->delete($entity)) {
$this->Flash->success(__('The ingredient has been removed.'));
} else {
$this->Flash->error(__('The ingredient could not be removed. Please, try again.'));
}
}

public function findByBase($baseId) {

$this->filterConditions['Recipes.base_type_id'] = $baseId;
Expand Down
4 changes: 2 additions & 2 deletions src/Model/Table/AttachmentsTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@ public function validationDefault(Validator $validator)
->notEmptyString('name');

$validator
->scalar('attachment')
->maxLength('attachment', 255)
//->scalar('attachment')
//->maxLength('attachment', 255)
->requirePresence('attachment', 'create')
->notEmptyString('attachment');

Expand Down
21 changes: 10 additions & 11 deletions src/Template/Recipes/edit.ctp
Original file line number Diff line number Diff line change
Expand Up @@ -167,8 +167,6 @@ $recipeId = isset($recipe->id) ? $recipe->id : "";
return; // Not good if we get here
}

console.log("Regex:" + regMatch);

// Don't delete the last table row
if (thisRow.is(":last-child")) {
return;
Expand Down Expand Up @@ -406,21 +404,22 @@ $recipeId = isset($recipe->id) ? $recipe->id : "";
echo $this->Form->control('preparation_time_id', array('empty'=>true));
echo $this->Form->control('difficulty_id', array('empty'=>true));
echo $this->Form->control('serving_size');
$imageCount = (isset($recipe) && isset($recipe->image))? count($recipe->image) : 0;

$imageCount = (isset($recipe) && isset($recipe->attachments))? count($recipe->attachments) : 0;
$newImageIndex = $imageCount-1;

echo "<div id='imageSection'>";
echo $this->Form->control('new_attachments.attachment', array('type' => 'file', 'label' => 'Add Image'));
echo $this->Form->control('new_attachments.name', array('label' => 'Caption'));
echo $this->Form->hidden('new_attachment.id');
echo $this->Form->control('attachments.'. $newImageIndex . '.attachment', array('type' => 'file', 'label' => 'Add Image'));
echo $this->Form->control('attachments.'. $newImageIndex . '.name', array('label' => 'Caption'));
echo $this->Form->hidden('attachment.'. $newImageIndex . '.id');

echo "<div id='currentImages'>";
for ($imageIndex = 0; $imageIndex < $imageCount; $imageIndex++) {

$imageName = $recipe['Image'][$imageIndex]['attachment'];
$imageDir = $recipe['Image'][$imageIndex]['dir'];
$imageName = $recipe->attachments[$imageIndex]->attachment;
$imageDir = $recipe->attachments[$imageIndex]->dir;
$imageThumb = preg_replace('/(.*)\.(.*)/i', 'thumb_${1}.$2', $imageName);
$imageCaption = $recipe['Image'][$imageIndex]['name'];
$imageId = $recipe['Image'][$imageIndex]['id'];
$imageCaption = $recipe->attachments[$imageIndex]->name;
$imageId = $recipe->attachments[$imageIndex]->id;
echo '<div class="recipeImage">';
echo $this->Form->hidden('Image.' . $imageIndex . '.id');
echo $this->Form->hidden('Image.' . $imageIndex . '.sort_order');
Expand Down
2 changes: 1 addition & 1 deletion src/Template/Recipes/remove_ingredient_mapping.ctp
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<?php echo $this->Session->flash(); ?>
<?= $this->Flash->render() ?>
2 changes: 1 addition & 1 deletion src/Template/Recipes/remove_recipe_mapping.ctp
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<?php echo $this->Session->flash(); ?>
<?= $this->Flash->render() ?>
14 changes: 7 additions & 7 deletions src/Template/Recipes/view.ctp
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ if (isset($recipe->reviews)) {
<hr/>
<br/>
<?php
$imageCount = (isset($recipe) && $recipe->image)? count($recipe->image) : 0;
$imageCount = (isset($recipe) && $recipe->attachments)? count($recipe->attachments) : 0;
if ($imageCount > 0) {
echo '<div class="float50Section">';

Expand Down Expand Up @@ -195,10 +195,10 @@ if (isset($recipe->reviews)) {
<?php
$baseUrl = Router::url('/');
if ($imageCount > 0) {
$imageName = $recipe['Image'][0]['attachment'];
$imageDir = $recipe['Image'][0]['dir'];
$imageName = $recipe->attachments[0]->attachment;
$imageDir = $recipe->attachments[0]->dir;
$imagePreview = preg_replace('/(.*)\.(.*)/i', 'preview_${1}.$2', $imageName);
$imageCaption = $recipe['Image'][0]['name'];
$imageCaption = $recipe->attachments[0]->name;

echo "<div id='selectedRecipeImage'>";
echo '<a href="#"><img src="' . $baseUrl . 'files/image/attachment/' . $imageDir . '/' .
Expand All @@ -208,11 +208,11 @@ if (isset($recipe->reviews)) {
if ($imageCount > 1) {

for ($imageIndex = 0; $imageIndex < $imageCount; $imageIndex++) {
$imageName = $recipe['Image'][$imageIndex]['attachment'];
$imageDir = $recipe['Image'][$imageIndex]['dir'];
$imageName = $recipe->attachments[$imageIndex]->attachment;
$imageDir = $recipe->attachments[$imageIndex]->dir;
$imageThumb = preg_replace('/(.*)\.(.*)/i', 'thumb_${1}.$2', $imageName);
$imagePreview = preg_replace('/(.*)\.(.*)/i', 'preview_${1}.$2', $imageName);
$imageCaption = $recipe['Image'][$imageIndex]['name'];
$imageCaption = $recipe->attachments[$imageIndex]['name'];

$previewUrl = $baseUrl . 'files/image/attachment/' . $imageDir . '/' . $imagePreview;
echo '<a href="#" onclick=\'loadImage("' . $previewUrl. '", "'. $imageCaption . '");\'><img src="' . $baseUrl . 'files/image/attachment/' . $imageDir . '/' .
Expand Down

0 comments on commit 4f9ca34

Please sign in to comment.